Write a Program control a ball using arrow keys.

 

Write a Program control a ball using arrow keys.


  1. #include<graphics.h>  
  2. #include<stdio.h>  
  3. void main()  
  4. {  
  5.     intgd=DETECT,gm,x,y,r=40;  
  6.     charch;  
  7.     initgraph(&gd,&gm,"C:/TURBOC3/BGI");  
  8.     setbkcolor(3);  
  9.     x=getmaxx()/2;  
  10.     y=getmaxy()/2;  
  11.     setfillstyle(1,RED);  
  12.     circle(x,y,r);  
  13.     floodfill(x,y,getmaxcolor());  
  14.     while((ch=getch())!=13)  
  15.       {  
  16.         switch(ch)  
  17.                      {  
  18.             case 75:    if(x>=r+1)  
  19.                                 {  
  20.                     cleardevice();  
  21.                     circle(x-=10,y,r);  
  22.                     floodfill(x,y,getmaxcolor());  
  23.                           }  
  24.                 break;  
  25.             case 72:    if(y>=r+1)  
  26.                                 {  
  27.                     cleardevice();  
  28.                     circle(x,y-=10,r);  
  29.                     floodfill(x,y,getmaxcolor());  
  30.                                   }  
  31.                 break;  
  32.             case 77:    if(x<=(getmaxx()-r-10))  
  33.                                {  
  34.                     cleardevice();  
  35.                     circle(x+=10,y,r);  
  36.                     floodfill(x,y,getmaxcolor());  
  37.                                 }  
  38.                 break;  
  39.             case 80:    if(y<=(getmaxy()-r-10))  
  40.                                {  
  41.                     cleardevice();  
  42.                     circle(x,y+=10,r);  
  43.                     floodfill(x,y,getmaxcolor());  
  44.                  }  
  45.         }  
  46.     }  
  47.     getch();  
  48. }  

Output

Computer Graphics Programs

Post a Comment

Previous Post Next Post