【源码】C语言实现贪吃蛇代码详解
- 时间:
- 浏览:2825
- 来源:吧啦熊
C语言实现贪吃蛇代码详解,学会了这个代码,C语言基本就算入门了,终于不再只会hello world了。
#include <graphics.h>
#include <conio.h> //getch()
#include <stdlib.h> //Sleep()休眠 ,rand()生成随机数
#include <stdio.h>
#include <time.h>
#include <math.h>
void test()
{
circle(200,100,20); //画圆
double y = 100; //初始高度
double v = 9.8/50; //重力加速度
double speed = 0; //初始速度
while(true)
{
Sleep(20); // 单位为毫秒 1s = 1000ms
//清除轨迹
//cleardevice(); //清除整个屏幕
//设置颜色为背景色
setcolor(RGB(255,255,255));
circle(200,(int)y,20);
//以重力加速度来表示圆球下落的速度
speed += v;
y += speed;
//画圆
setcolor(RGB(255,0,0));
circle(200,(int)y,20);
//画线
line(0,420,400,420);
if(y>=400)
{
//清除圆
setcolor(RGB(255,255,255));
circle(200,(int)y,20);
y = 400;
//弹出原高度的80%
speed = -speed*0.8;
//printf("%f\n",speed);
if(speed<0 && speed>-1)
break;
}
}
//画圆
setcolor(RGB(255,0,0));
circle(200,(int)y,20);
getch(); //等待接受字符
}
void getRandLocal(int local[])//随机出现一个圆随机移动
{
srand((unsigned)time(NULL)); //为rand()随机数函数生成种子
local[0]= rand()%400+1;
local[1]= rand()%500+1;
local[2]= rand()%400+1;
local[3]= rand()%500+1;
}
void MyRandCircle()
{
int local[4];
getRandLocal(local);
//初始化图形界面
initgraph(400,500);
//设置背景颜色
setbkcolor(RGB(255,255,255));
cleardevice(); //使背景设置生效
//设置前景色为红色
setcolor(RGB(255,0,0));
setfillstyle(RGB(255,0,0));
fillcircle(local[0],local[1],20);
// circle(local[2],local[3],20);
int sx,sy,sp;
int len=(local[0]-local[2])*(local[0]-local[2])+(local[1]-local[3])*(local[1]-local[3]);
sp=10;//运动速度
sx=(local[0]-local[2])/sp;
sy=(local[1]-local[3])/sp;
while(true)
{
Sleep(100);
setcolor(RGB(255,255,255));
setfillstyle(RGB(255,255,255));
fillcircle((int)local[0],(int)local[1],20);
local[0]-=sx;
local[1]-=sy;
setcolor(RGB(255,0,0));
setfillstyle(RGB(255,0,0));
fillcircle((int)local[0],(int)local[1],20);
if((local[0]-local[2])*(local[0]-local[2])+(local[1]-local[3])*(local[1]-local[3])<400)
{
local[2]= rand()%400+1;
local[3]= rand()%500+1;
sx=(local[0]-local[2])/sp;
sy=(local[1]-local[3])/sp;
}
}
getch();
}
void Tcircle()
{
//初始化图形界面
initgraph(400,400);
//设置背景颜色
setbkcolor(RGB(255,255,255));
cleardevice(); //使背景设置生效
srand((unsigned)time(NULL));
int x=rand()%400+1;
int y=rand()%400+1;
int x0=(rand()%4+1),y0=(rand()%4+1);
//设置前景色为红色
setcolor(RGB(255,0,0));
setfillstyle(RGB(255,0,0));
fillcircle(x,y,20);
while(true)
{
Sleep(50);
setcolor(RGB(255,255,255));
setfillstyle(RGB(255,255,255));
fillcircle(x,y,20);
if(x<=20)
x0=(rand()%4+1);
if(x>=380)
x0=-(rand()%4+1);
if(y<=20)
y0=(rand()%4+1);
if(y>=380)
y0=-(rand()%4+1);
x+=x0;
y+=y0;
setcolor(RGB(255,0,0));
setfillstyle(RGB(255,0,0));
fillcircle(x,y,20);
}
}
struct Snake
{
int x[100];//蛇节点坐标
int y[100];
int node;//节点数目
int direction;//方向 上1下2左3右4
}snake;//结构体变量
//定义蛇食物
struct Food
{
int x;
int y;
}food;
//初始化蛇
struct Snake InitSnake(struct Snake snake)
{
snake.node=3;//蛇节点数初始化为3;
snake.direction=3;//默认设置蛇方向向左
for(int i=0;i<3;i++)
{
snake.x[i]=200+i*20;
snake.y[i]=200;
}
return snake;
}
//随机生成一个食物
struct Food GetFood(struct Food food)
{
srand((unsigned)time(NULL));
A: food.x=(int)(rand()%20*20);
food.y=(int)(rand()%20*20);
for(int i=0;i<snake.node;i++)//食物不能出现在蛇身的位置上
{
if(food.x==snake.x[i]&&food.y==snake.y[i])
goto A;
}
setfillstyle(YELLOW);
bar(food.x,food.y,food.x+20,food.y+20);
return food;
}
//蛇朝着既定的方向移动一步
struct Snake SnakeMove(struct Snake snake)
{
int i,x,y;
int sq=0;
if(snake.direction==0)
{
return snake;
}
for(i=0;i<snake.node;i++)
{
setfillstyle(RGB(0,0,0));
bar(snake.x[i],snake.y[i],snake.x[i]+20,snake.y[i]+20);
}
x=snake.x[0];
y=snake.y[0];
for(i=snake.node-1;i>=0;i--)
{
snake.y[i+1]=snake.y[i];
snake.x[i+1]=snake.x[i];
}
switch(snake.direction)
{
case 1://上
snake.y[0]=y-20;
if(snake.y[0]<0)
{
//向上撞墙
snake.direction=0;sq++;
}
snake.x[0]=x;
break;
case 2://下
snake.y[0]=y+20;
if(snake.y[0]>380)
{
//向下撞墙
snake.direction=0;sq++;
}
snake.x[0]=x;
break;
case 3://左
snake.x[0]=x-20;
snake.y[0]=y;
if(snake.x[0]<0)
{
//向左撞墙
snake.direction=0;sq++;
}
break;
case 4://右
snake.x[0]=x+20;
if(snake.x[0]>380)
{
//向右撞墙
snake.direction=0;sq++;
}
snake.y[0]=y;
break;
default://如果撞墙则静止
return snake;
}
if(snake.x[0]==food.x&&snake.y[0]==food.y)//如果吃到了食物,则自增一个即不丢弃最后一个
{
snake.node++;
food=GetFood(food);//重新生成食物
}
for(i=1;i<snake.node;i++)//如果撞到自己了
{
if(snake.x[0]==snake.x[i]&&snake.y[0]==snake.y[i])
{
snake.direction=0;sq++;
break;
}
}
for(i=sq;i<snake.node+sq;i++)
{
setfillstyle(RGB(255,0,0));
bar(snake.x[i],snake.y[i],snake.x[i]+20,snake.y[i]+20);
}
return snake;
}
void main()
{
NewGame:
//初始化图形界面
initgraph(400,400);
//设置背景颜色
setbkcolor(RGB(0,0,0));
cleardevice(); //使背景设置生效
srand((unsigned)time(NULL));
snake=InitSnake(snake);
//绘制蛇
setfillstyle(RGB(255,0,0));
int i;
for(i=0;i<snake.node;i++)
{
bar(snake.x[i],snake.y[i],snake.x[i]+20,snake.y[i]+20);
}
food=GetFood(food);
snake.direction=2;
int spp=400;//速度
int flag=0;
int nodeCount=snake.node;
while(true)
{
if((snake.node-3)%5==0&&nodeCount!=snake.node)
{
spp-=50;
nodeCount=snake.node;
}
if(spp<00)
spp=50;
if(flag==0)
{
Sleep(spp);
}
if(kbhit())//如果按键盘
{
fflush(stdin);
int key=getch();
if(snake.direction==0)
continue;
switch(key)
{
case 224:
flag++;
continue;
case 72:
if(flag==0)
break;
case 119:
if(snake.direction!=2)
snake.direction=1;
flag=0;
break;
case 80:
if(flag==0)
break;
case 115:
if(snake.direction!=1)
snake.direction=2;
flag=0;
break;
case 75:
if(flag==0)
break;
case 97:
if(snake.direction!=4)
snake.direction=3;
flag=0;
break;
case 77:
if(flag==0)
break;
case 100:
if(snake.direction!=3)
snake.direction=4;
flag=0;
break;
case 13://暂停/开始/现实信息
char msg1[20],msg2[20];
sprintf(msg1,"当前速度为:%d",spp);
sprintf(msg2,"当前得分为:%d",(snake.node-3)*10);
do
{
cleardevice();
outtextxy(100,200,msg2);
outtextxy(100,230,msg1);
fflush(stdin);
key=getch();
}while(key!=13);
cleardevice();
setfillstyle(YELLOW);
bar(food.x,food.y,food.x+20,food.y+20);
continue;
default:
break;
}
}
snake=SnakeMove(snake);
if(snake.direction==0)
{
outtextxy(100,200,"你撞死了!");
fflush(stdin);
getch();
closegraph();
break;
}
}
printf("你的得分是:%d\n",10*(snake.node-3));
printf("是否继续新游戏?(y/n)\n");
char s;
fflush(stdin);
scanf("%c",&s);
if(s=='y')
goto NewGame;
}
猜你喜欢