贪吃蛇c++代码及实验报告,详细

求c++版贪吃蛇代码 易懂,有相应实验报告

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
intfood_x,food_y;//食物的坐标
structsneak
{
int x,y;
struct sneak *next;
};//蛇的结构体
voidSetpos(int i,int j)
{
COORD pos={i-1,j-1};
HANDLEhOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}//定位在什么地方输出
voidbeginsneak(struct sneak *head)
{
struct sneak *temp1,*temp2;
temp1=(structsneak*)malloc(sizeof(struct sneak));
temp2=(structsneak*)malloc(sizeof(struct sneak));
temp1->x=6;
temp1->y=5;
temp2->x=6;
temp2->y=4;
head->next=temp1;
temp1->next=temp2;
temp2->next=NULL;
}//初始化蛇的身体
voidmap()
{
int i;
for(i=0;i<50;i++)
{
Setpos(i,1);
printf("-");
Setpos(i,25);
printf("-");
}
for(i=1;i<25;i++)
{
Setpos(1,i);
printf("|");
Setpos(50,i);
printf("|");
}
}//画地图
voidfood(struct sneak *head)
{
struct sneak *p;
p=head;
int foodexist=0;//食物标记
srand((int)time(0));
while(foodexist==0)
{
food_x=rand()%48+2;
food_y=rand()%23+2;

while(p!=NULL)//判断食物生成是否在蛇的身体里
{
if(p->x==food_x&&p->y==food_y)
{
foodexist=1;
break;
}
p=p->next;
}

if(foodexist==0)//如果没有,则生成食物
{
Setpos(food_x,food_y);
printf("ï¿¥");
break;
}
}
}
voidpaint(struct sneak *head)//画出蛇的身体
{
struct sneak *p;
int ifhead=0; //判断是否是蛇头
p=head;
while(p!=NULL)
{
if(ifhead==0)//如果是,画出蛇头
{
Setpos(p->x,p->y);
printf("#");
ifhead=1;
}
else//其他部分
{
Setpos(p->x,p->y);
printf("*");
}

p=p->next;
}
}
structsneak *moveresult(struct sneak *head)//蛇的移动
{
struct sneak *new1;
new1=(structsneak*)malloc(sizeof(struct sneak));
static int direction=4;
char x;
if(_kbhit())
{
x=_getch();
if((x=='w'||x=='W')&&direction!=2)
direction=1;
if((x=='s'||x=='S')&&direction!=1)
direction=2;
if((x=='a'||x=='A')&&direction!=4)
direction=3;
if((x=='d'||x=='D')&&direction!=3)
direction=4;
while(_kbhit())
_getch();
}//控制方向

switch(direction)//每移动一步都要增加一个蛇身
{
case 1:
new1->x=head->x;
new1->y=head->y-1;
new1->next=head;
head=new1;
break;
case 2:
new1->x=head->x;
new1->y=head->y+1;
new1->next=head;
head=new1;
break;
case 3:
new1->x=head->x-1;
new1->y=head->y;
new1->next=head;
head=new1;
break;
case 4:
new1->x=head->x+1;
new1->y=head->y;
new1->next=head;
head=new1;
break;
}
return head;
}
intover(struct sneak *head)//判断是否结束
{
struct sneak *p;
p=head->next;
int gameover=0;
if((head->x)>=50||(head->x)<=1||(head->y)>=25||(head->y)<=1)//撞墙
{
gameover=-1;
}
while(p!=NULL)//撞到自己
{
if((head->x==p->x)&&(head->y==p->y))
{
gameover=-1;
break;
}
p=p->next;
}
return gameover;
}
voiddecrease(struct sneak *head)//删减一个蛇身
{
struct sneak *p,*pre;
p=head;
while(p->next!=NULL)
{
pre=p;
p=p->next;
}
Setpos(p->x,p->y);
printf(" ");
free(p);
pre->next=NULL;
}
voidgame()
{
system("cls");
Setpos(54,3);//定位。以后的也是
printf("-贪吃蛇游戏-");
int count=3;//蛇身的长度
int speed=0;//当前速度
struct sneak *head;//初始化蛇头
head=(structsneak*)malloc(sizeof(struct sneak));
head->x=6;
head->y=6;

map();//地图
beginsneak(head);//初始化蛇
food(head);//初始化食物
paint(head);//画蛇身
while(1)
{
if(speed==9)//判断是否通关
{
system("cls");
Setpos(30,7);
printf("666666.恭喜通关");
break;
}
Setpos(54,5);
printf("蛇的长度为:%d",count);
Setpos(54,7);
printf("当前等级:%d",speed);
head=moveresult(head);//移动蛇,同时每一次蛇身加一

if(over(head)==-1)//是否失败
{
system("cls");
Setpos(30,7);
printf("失败了......");
Setpos(30,9);
printf("最终长度:%d",count);
break;
}

if(head->x==food_x&&head->y==food_y)//吃到食物,蛇身不减一
{
count++;
food(head);
}
else//没吃到食物,蛇身减一
{
decrease(head);
}
paint(head);
speed=(int)(count/10);//当前速度
Sleep(150-speed*15);
}
}
intmain()
{
printf("------------------ 欢迎玩家 -----------------\n");
printf("------------------ w,a,s,d控制 --------------\n");
printf("------------------ 输入任意键开始 -------------\n");
_getch();
game();
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答