[C++] 纯文本查看 复制代码 #include <graphics.h> //包含EGE的头文件
#include<time.h> //包含随机数,时间函数
#include<bits/stdc++.h>
using namespace std;
int width =800;
int height =600;
int num;
struct body
{
int x,y;//坐标
};
struct snake
{
int l;//长
int v;//速度
char fx;//方向
body n[1000];
}sn;
void start()//初始化
{
srand(time(0));//随机种子函数
sn.l=4;
sn.v=20;
sn.fx='d';
for(int i=0;i<sn.l;i++)
{
sn.n[i].x=20-i;
sn.n[i].y=15;
}
initgraph(width, height); //初始化图形界面
setcolor(GREEN); //设置画图线为红色
setbkcolor(LIGHTGRAY);//设置背景颜色为白色
setfillcolor(BLUE);//设置填充颜色为红色
}
void yd()
{
for(int i=sn.l-1;i>=0;i--)
{
sn.n[i+1].x=sn.n[i].x;
sn.n[i+1].y=sn.n[i].y;
}
switch (sn.fx)
{
case 'w':
{
sn.n[0].y--;
}
break;
case 'a':
{
sn.n[0].x--;
}
break;
case 's':
{
sn.n[0].y++;
}
break;
case 'd':
{
sn.n[0].x++;
}
break;
}
}
void update()//数据更新
{
if(kbhit())
{
sn.fx= getch();
yd();
}
}
void draw()//画球
{
for(int i=0;i<sn.l;i++)
{
setfillcolor(hsv2rgb(10*i%360, 1, 1));//设置填充颜色 参数(颜色,饱和度,明亮度)
bar(sn.n[i].x*20,sn.n[i].y*20,sn.n[i].x*20+20,sn.n[i].y*20+20);
}
Sleep(10);
cleardevice();
}
int main()
{
start();
while(1)
{
update();
draw();
}
return 0;
} |