[C++] 纯文本查看 复制代码 #include <graphics.h> //包含EGE的头文件
#include<time.h> //包含随机数,时间函数
#include<bits/stdc++.h>
using namespace std;
int width =800;
int height =600;
int num;
char ch;
int wallk=50;
int wallj=100;
struct body
{
int x;
int y;
};
struct snake
{
int len;
int v;
body bo[1000];
char dir;
}sna;
void start()//初始化
{
srand(time(0));//随机种子函数
sna.v=20;
sna.len=4;
sna.dir='d';
for(int i=0;i<4;i++)
{
sna.bo[i].x=20-i;
sna.bo[i].y=15;
}
//float g=0;
initgraph(width, height); //初始化图形界面
setcolor(RED); //设置画图线为红色
setbkcolor(WHITE);//设置背景颜色为白色
setfillcolor(RED);//设置填充颜色为红色
}
void move()
{
int i;
for(i=sna.len;i>0;i--)
{
sna.bo[i].x=sna.bo[i-1].x;
sna.bo[i].y=sna.bo[i-1].y;
}
switch(sna.dir)
{
case 'W':
case 'w':
sna.bo[0].y--;break;
case 'A':
case 'a':
sna.bo[0].x--;break;
case 'S':
case 's':
sna.bo[0].y++;break;
case 'd':
case 'D':
sna.bo[0].x++;break;
}
}
void update()//数据更新
{
/*if(ball.y>=height-ball.radius)
{
ball.vy=-ball.vy;
}
if(ball.y<=ball.radius)
{
ball.vy=-ball.vy;
}
if(ball.x>=width-ball.radius)
{
ball.vx=-ball.vx;
}
if(ball.x<=ball.radius)
{
ball.vx=-ball.vx;
}*/
if(kbhit())
{
sna.dir = getch();
move();
}
}
void drawball()//画球
{
//fillellipse(ball.x, ball.y, ball.radius, ball.radius); //画一个实心圆
for(int i=0;i<sna.len;i++)
{
setfillcolor(hsv2rgb(10*i%360, 1, 1));//设置填充颜色 参数(颜色,饱和度,明亮度)
bar(sna.bo[i].x*20,sna.bo[i].y*20,sna.bo[i].x*20+20,sna.bo[i].y*20+20);
}
Sleep(10);
cleardevice();
}
int main()
{
start();
while(1)
{
update();
drawball();
}
return 0;
}
|