easyx 简单编程 水平、垂直弹跳的小球。

来源:互联网 发布:mac免费解压软件 编辑:程序博客网 时间:2024/04/29 15:49

随便写的小程序,感觉已经不会再改了。

 

 

#include <graphics.h>
#include <conio.h>

void main()
{
 initgraph(640, 480);
 int x = 320, y = 240;
 
 // 画初始图形
 setcolor(YELLOW);
 setfillstyle(WHITE);
 fillcircle(x, y,20);
    char c; 
 while(c != 27)
 {
  // 获取按键
 
  if (kbhit())
   c = getch();
  // 先擦掉上次显示的旧图形
  setcolor(BLACK);
  setfillstyle(BLACK);
  fillcircle(x, y,20);
  
  // 根据输入,计算新的坐标
  switch(c)
  {
      case 'w':
    if(y >= 20)
    {
     y -= 19;
    }
    else
     c = 's';
    break;
   

   case 's':
              if(y <= 400)
    {
     y += 19;
    }
    else
     c = 'w';
    break;

   case 'a':
    if(x >= 20)
    {
        x-=19;
    }
    else
     c = 'd';
    break;

   case 'd':
    if(x <= 600)
    {
        x+=19;
    }
    else
     c = 'a';
    break;

   case 27:
    break;
  }
  
  // 绘制新的图形
  setcolor(YELLOW);
  setfillstyle(WHITE);
  fillcircle(x, y,20);
  
  // 延时
  Sleep(10);
 }
 
 closegraph();
}