随便写的小程序,感觉已经不会再改了。
#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();
}
这是一个使用C语言编写的小程序,通过键盘的方向键控制屏幕上的黄色圆圈移动。程序利用了graphics.h和conio.h库实现图形界面及键盘输入检测功能。
861

被折叠的 条评论
为什么被折叠?



