cmd贪吃蛇简码(c语言贪吃蛇代码)

本文目录
c语言贪吃蛇代码
基本思路:
蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。
#include 《stdio.h》
#include 《conio.h》
#include 《windows.h》
#define BEG_X2
#define BEG_Y1
#define WID20
#define HEI20
HANDLE hout;
typedef enum {UP, DOWN, LEFT, RIGHT} DIR;
typedef struct Snake_body
{
COORD pos;//蛇身的位置
struct Snake_body *next;//下一个蛇身
struct Snake_body *prev;//前一个蛇身
}SNAKE, *PSNAKE;
PSNAKE head = NULL;//蛇头
PSNAKE tail = NULL;//蛇尾
//画游戏边框的函数
void DrawBorder()
{
int i, j;
COORD pos = {BEG_X, BEG_Y};
for(i = 0; i 《 HEI; ++i)
{
SetConsoleCursorPosition(hout, pos);
for(j = 0; j 《 WID; ++j)
{
if(i == 0)//第一行
{
if(j == 0)
printf("┏");
else if(j == WID - 1)
printf("┓");
else
printf("━");
}
else if(i == HEI - 1)//最后一行
{
if(j == 0)
printf("┗");
else if(j == WID - 1)
printf("┛");
else
printf("━");
}
else if(j == 0 || j == WID - 1)//第一列或最后一列
printf("┃");
else
printf(" ");
}
++pos.Y;
}
}
//添加蛇身的函数
void AddBody(COORD pos)
{
PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));
pnew-》pos = pos;
if(!head)
{
head = tail = pnew;
}
else
{
pnew-》next = head;//新创建蛇身的next指向原先的蛇头
head-》prev = pnew;//原先的蛇头的prev指向新创建的蛇身
head = pnew;//把新创建的蛇身作为新的蛇头
}
SetConsoleCursorPosition(hout, head-》pos);
printf("◎");
}
//蛇身移动的函数
void MoveBody(DIR dir)
{
PSNAKE ptmp;
COORD pos = head-》pos;
switch(dir)
{
case UP:
if(head-》pos.Y 》 BEG_Y + 1)
--pos.Y;
else
return;
break;
case DOWN:
if(head-》pos.Y 《 BEG_Y + HEI - 2)
++pos.Y;
else
return;
break;
case LEFT:
if(head-》pos.X 》 BEG_X + 2)
pos.X -= 2;
else
return;
break;
case RIGHT:
if(head-》pos.X 《 BEG_X + (WID - 2) * 2)
pos.X += 2;
else
return;
break;
}
AddBody(pos);//添加了一个新的蛇头
ptmp = tail;//保存当前的蛇尾
tail = tail-》prev;
if(tail)
tail-》next = NULL;
SetConsoleCursorPosition(hout, ptmp-》pos);
printf(" ");
free(ptmp);
}
int main()
{
int ctrl;
DIR dir = RIGHT;//初始蛇的方向是向右的
COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};
system("color 0E");
system("mode con cols=90 lines=30");
hout = GetStdHandle(STD_OUTPUT_HANDLE);
printf(" ------------贪吃蛇的移动------------");
DrawBorder();
//自定义几个蛇的身体
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
//控制蛇的移动
while(ctrl = getch())
{
switch(ctrl)
{
case ’w’:
if(dir == DOWN)
continue;
dir = UP;
break;
case ’s’:
if(dir == UP)
continue;
dir = DOWN;
break;
case ’a’:
if(dir == RIGHT)
continue;
dir = LEFT;
break;
case ’d’:
if(dir == LEFT)
continue;
dir = RIGHT;
break;
case ’q’:
return 0;
}
MoveBody(dir);
}
return 0;
}
扩展资料:
实现逻辑
1,可以设置光标,就能实现制定位置打印制定符号。
2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。
3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。
4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。
5,食物产生的位置判定,不能越界,也不能与蛇身体重合。
6,蛇的转向判定,一条规则,不允许倒退。
7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)
8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。
9,加速减速,设置刷新休眠时间实现。
简单贪吃蛇C语言程序
#include 《graphics.h》
#include 《stdlib.h》
#include 《dos.h》
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define N 200/*蛇的最大长度*/
#define M 3 /*食物数量*/
#define F 2 /*毒花数量*/
int i,j,key;
int k=0;
int a=0;
int map;/*障碍地图*/
int score=200;/*得分*/
int gamespeed;/*游戏速度自己调整*/
int level=1; /*游戏级别*/
struct Food
{
int x;/*食物的横坐标*/
int y;/*食物的纵坐标*/
int yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
struct Flower
{
int x;
int y;
int yes;
}flower; /*毒花的结构体*/
struct Lucky
{
int x;
int y;
int yes;
}lucky;/*救命稻草结构体*/
struct Snake
{
int x;
int y;
int node;/*蛇的节数*/
int direction;/*蛇移动方向*/
int life;/* 蛇的生命,0死亡*/
}snake;
void Init(void);/*图形驱动*/
void Close(void);/*图形结束*/
void DrawK(void);/*开始画面*/
void GameOver(void);/*结束游戏*/
void GamePlay(void);/*玩游戏具体过程*/
void PrScore(void);/*输出成绩*/
/*主函数*/
void main(void)
{
Init();/*图形驱动*/
DrawK();/*开始画面*/
GamePlay();/*玩游戏具体过程*/
Close();/*图形结束*/
}
/*图形驱动*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
/*开始画面,左上角坐标为(50,40),右下角坐标为(595,460)的围墙*/
void DrawK(void)
{
setcolor(RED);
rectangle(50,40,595,460);
}
/*玩游戏具体过程*/
void GamePlay(void)
{
int ii,jj;
randomize();
for(i=0;i《M;i++)/*随机数发生器*/
food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/
for(i=0;i《F;i++)
flower.yes=1;
snake.life=25;
snake.direction=1;/*方向往右*/
snake.x=60;
snake.x=60;/*蛇头*/
snake.node=2;/*节数*/
PrScore();/*输出得分*/
while(1)/*可以重复玩游戏,压ESC键结束*/
{
while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/
{
/*画地图*/
setcolor(RED);
for(i=0;i《20;i++)
for(j=0;j《20;j++)
{
if(map)
rectangle(200+j*10-5,100+i*10-5,210+j*10-5,110+i*10-5);
}
for(i=0;i《M;i++)
{
if(food.yes==1)/*需要出现新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
for(ii=0;ii《20;ii++)
for(jj=0;jj《20;jj++)
if(map){
while(food.y==100+ii*10)
food.x+=10; } /*不让食物在内墙上出现*/
food.yes=0;/*画面上有食物了*/
}
if(food.yes==0)/*画面上有食物了就要显示*/
{
setcolor(GREEN);
circle(food.y,4);
}
}
for(i=0;i《F;i++)
{
if(flower.yes==1)
{
flower.x=rand()%400+60;
flower.y=rand()%350+60;
while(flower.x%10!=0)
flower.x++;
while(flower.y%10!=0)
flower.y++;
for(ii=0;ii《20;ii++)
for(jj=0;jj《20;jj++)
if(map)
{
if(flower.y==100+ii*10)
flower.x+=10; /*不让毒花在内墙上出现*/
}
for(ii=0;ii《M;ii++)
{
while(flower.y)
flower.x+=10;/*不让毒花与食物重合*/
}
flower.yes=0; /*画面上有毒花了*/
}
if(flower.yes==0) /*画面上有毒花了就要显示*/
{
setcolor(WHITE); /*毒花为白色*/
circle(flower.y,4);
}
}
if(level》=3&&a==0)
{
for(ii=0;ii《20;ii++)
for(jj=0;jj《20;jj++)
{
if(ii==0||ii==9||ii==19)
map=1;
}
a=1;
}
if(level》=3&&snake.life《=5&&k==0)
lucky.yes=1;
if(lucky.yes==1)
{
lucky.x=rand()%400+60;
lucky.y=rand()%350+60;
while(lucky.x%10!=0)
lucky.x++;
while(lucky.y%10!=0)
lucky.y++;
for(ii=0;ii《20;ii++)
for(jj=0;jj《20;jj++)
if(map)
{
while(lucky.x==200+jj*10&&lucky.y==100+ii*10)
lucky.x+=10; /*不让救命稻草在内墙上出现*/
}
for(ii=0;ii《M;ii++)
{
while(lucky.x==food.y)
lucky.x+=10;/*不让救命稻草与食物重合*/
}
for(ii=0;ii《F;ii++)
{
while(lucky.x==flower.y)
lucky.x+=10;/*不让救命稻草与毒花重合*/
}
lucky.yes=0;
k=1;
}
if(lucky.yes==0&&k==1)
{
setcolor(RED); /*救命稻草为红色*/
circle(lucky.x,lucky.y,4);
}
for(i=snake.node-1;i》0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
{
snake.x;
snake.y;
}
/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/
switch(snake.direction)
{
case 1:snake.x+=10;break;
case 2:snake.x-=10;break;
case 3:snake.y-=10;break;
case 4:snake.y+=10;break;
}
for(i=3;i《snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/
{
if(snake.x)
{
GameOver();/*显示失败*/
snake.life=0;
break;
}
}
if(snake.x《45||
snake.y》455)/*蛇是否撞到外墙壁*/
{
GameOver();/*本次游戏结束*/
snake.life=0; /*蛇死*/
}
for(i=0;i《20;i++)
{
for(j=0;j《20;j++)
{
if(map)
{
if(snake.x==(100+i*10)) /*蛇是否撞到内墙壁*/
{
snake.life--;/*生命值减1*/
score-=10;
setcolor(0);
rectangle(200+j*10-5,100+i*10-5,210+j*10-5,110+i*10-5);/*去掉被撞掉的砖块*/
map=0;/*清地图*/
PrScore();
}
}
}
}
if(snake.life==0)/*以上三种判断,如果蛇死就跳出内循环,重新开始*/
break;
for(i=0;i《M;i++)
{
if(snake.x.y)/*吃到食物以后*
{
setcolor(0);/*把画面上的食物东西去掉*/
circle(food.y,4);
snake.x=-20;
/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/
snake.node++;/*蛇的身体长一节*/
food.yes=1;/*画面上需要出现新的食物*/
score+=10*level;
PrScore();/*输出新得分*/
}
}
for(i=0;i《F;i++)
{
if(snake.x.y)/*吃到毒花后*/
{
setcolor(0);
circle(flower.y,4);
snake.life--;
flower.yes=1;
score-=20;
PrScore();
}
}
if(snake.x==lucky.y)/*吃到救命稻草后*/
{
setcolor(0);
circle(lucky.x,lucky.y,4);
snake.life++;
score+=100;
PrScore();
k=0;
}
level=snake.node/10+1;
gamespeed=60000-level*5000;
for(i=0;i《snake.node;i++)
{
setcolor(i%15+1);
/*画出彩蛇*/
circle(snake.x,5);
}
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最后一节*/
circle(snake.x,5);
} /*endwhile(!kbhit)*/
if(snake.life==0)/*如果蛇死就跳出循环*/
break;
key=bioskey(0);/*接收按键*/
if(key==ESC)/*按ESC键退出*/
break;
else if(key==UP&&snake.direction!=4)
/*判断是否往相反的方向移动*/
snake.direction=3;
else if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/*输出成绩*/
void PrScore(void)
{
char str;
char str2;
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
setfillstyle(SOLID_FILL,YELLOW);
bar(225,15,350,35);
setcolor(1);
sprintf(str1,"level:%d",level);
outtextxy(230,20,str1);
setfillstyle(SOLID_FILL,YELLOW);
bar(355,15,470,35);
setcolor(7);
sprintf(str2,"life:%d",snake.life);
outtextxy(360,20,str2);
}
/*图形结束*/
void Close(void)
{
closegraph();
}
求贪吃蛇C语言代码,有一定功能要求
以下是代码
/* 贪吃蛇程序 by champking */
#define N 200
#include 《graphics.h》
#include 《stdlib.h》
#include 《dos.h》
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score = 0;/*得分*/
int gamespeed = 100000;/*游戏速度自己调整*/
struct Food
{
int x;/*食物的横坐标*/
int y;/*食物的纵坐标*/
int yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
struct Snake
{
int x;
int y;
int node;/*蛇的节数*/
int direction;/*蛇移动方向*/
int life;/* 蛇的生命,0活着,1死亡*/
}snake;
void Init(void);/*图形驱动*/
void Close(void);/*图形结束*/
void DrawK(void);/*开始画面*/
void GameOver(void);/*结束游戏*/
void GamePlay(void);/*玩游戏具体过程*/
void PrScore(void);/*输出成绩*/
/*主函数*/
void main(void)
{
Init();/*图形驱动*/
DrawK();/*开始画面*/
GamePlay();/*玩游戏具体过程*/
Close();/*图形结束*/
}
/*图形驱动*/
void Init(void)
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc");
cleardevice();
}
/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE, 0, THICK_WIDTH);/*设置线型*/
for(i = 50; i 《= 600; i += 10)/*画围墙*/
{
rectangle(i, 40, i + 10, 49); /*上边*/
rectangle(i, 451, i + 10, 460);/*下边*/
}
for(i = 40; i 《= 450; i += 10)
{
rectangle(50, i, 59, i + 10); /*左边*/
rectangle(601, i, 610, i + 10);/*右边*/
}
}
/*玩游戏具体过程*/
void GamePlay(void)
{
randomize();/*随机数发生器*/
food.yes = 1;/*1表示需要出现新食物,0表示已经存在食物*/
snake.life = 0;/*活着*/
snake.direction = 1;/*方向往右*/
snake.x = 100;/*蛇头*/
snake.x = 100;
snake.node = 2;/*节数*/
PrScore();/*输出得分*/
while(1)/*可以重复玩游戏,压ESC键结束*/
{
while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/
{
if(food.yes == 1)/*需要出现新食物*/
{
food.x = rand() % 400 + 60;
food.y = rand() % 350 + 60;
while(food.x % 10 != 0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/
food.x++;
while(food.y % 10 != 0)
food.y++;
food.yes = 0;/*画面上有食物了*/
}
if(food.yes == 0)/*画面上有食物了就要显示*/
{
setcolor(GREEN);
rectangle(food.x, food.y, food.x + 10, food.y - 10);
}
for(i = snake.node - 1; i 》 0; i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
{
snake.x;
snake.y;
}
/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/
switch(snake.direction)
{
case 1: snake.x += 10; break;
case 2: snake.x -= 10; break;
case 3: snake.y -= 10; break;
case 4: snake.y += 10; break;
}
for(i = 3; i 《 snake.node; i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/
{
if(snake.x)
{
GameOver();/*显示失败*/
snake.life = 1;
break;
}
}
if(snake.x《55||
snake.y》455)/*蛇是否撞到墙壁*/
{
GameOver();/*本次游戏结束*/
snake.life=1; /*蛇死*/
}
if(snake.life == 1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/
break;
if(snake.x == food.y)/*吃到食物以后*/
{
setcolor(0);/*把画面上的食物东西去掉*/
rectangle(food.x, food.y, food.x + 10, food.y - 10);
snake.x =- 20;
/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/
snake.node++;/*蛇的身体长一节*/
food.yes = 1;/*画面上需要出现新的食物*/
score += 10;
PrScore();/*输出新得分*/
}
setcolor(4);/*画出蛇*/
for(i = 0; i 《 snake.node; i++)
rectangle(snake.x + 10,
snake.y - 10);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最后一节*/
rectangle(snake.x,
snake.x - 10);
} /*endwhile(!kbhit)*/
if(snake.life == 1)/*如果蛇死就跳出循环*/
break;
key = bioskey(0);/*接收按键*/
if(key == ESC)/*按ESC键退出*/
break;
else
if(key == UP&&snake.direction!=4)
/*判断是否往相反的方向移动*/
snake.direction=3;
else
if(key == RIGHT &&snake.direction != 2)
snake.direction=1;
else
if(key == LEFT && snake.direction != 1)
snake.direction = 2;
else
if(key == DOWN && snake.direction != 3)
snake.direction = 4;
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0, 0, 4);
outtextxy(200, 200, "GAME OVER");
getch();
}
/*输出成绩*/
void PrScore(void)
{
char str;
setfillstyle(SOLID_FILL, YELLOW);
bar(50, 15, 220, 35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str, "score:%d", score);
outtextxy(55, 20, str);
}
/*图形结束*/
void Close(void)
{
getch();
closegraph();
}
贪吃蛇c语言代码
#include《stdio.h》
#include《stdlib.h》
#include《Windows.h》
#include《conio.h》
#include《time.h》
char gamemap;//游戏地图大小 20*40
int score=0;//当前分数
//记录蛇的结点
int x;//每个结点的行编号
int y;//每个结点的列编号
int len = 0;//蛇的长度
//记录水果信息
int fx=0;//食物的横坐标
int fy=0;//食物的纵坐标
int fcount=0;//食物的数目
//主要函数操作
void createfood();//生成食物
void PrintgameMap(int x);//画游戏地图
void move(int x);//移动蛇
int main()
{
srand(time(NULL));
//初始化蛇头和身体的位置,默认刚开始蛇长为2
x = 9;
y = 9;
len++;
x = 9;
y = 8;
len++;
createfood();
PrintgameMap(x,y);
move(x,y);
return 0;
}
void createfood()
{
if(0==fcount)
{
int tfx=rand()%18+1;
int tfy=rand()%38+1;
int i,j;
int have=0;//为0表示食物不是食物的一部分
for(i=0;i《len;i++)
{
for(j=0;j《len;j++)
{
if(x==fy)
{
have=1;
break;
}
else
{
have=0;
}
}
if(1==have)//若为蛇的一部分,执行下一次循环
{
continue;
}
else//否则生成新的水果
{
fcount++;
fx=tfx;
fy=tfy;
break;
}
}
}
}
//游戏地图
void PrintgameMap(int x)
{
int snake = 0,food=0;
int i, j;
//画游戏地图,并画出蛇的初始位置
for (i = 0; i 《 20; i++)
{
for (j = 0; j 《 40; j++)
{
if (i == 0 && j 》= 1 && j 《= 38)
{
gamemap = ’=’;
}
else if (i == 19 && j 》= 1 && j 《= 38)
{
gamemap = ’=’;
}
else if (j == 0 || j == 39)
{
gamemap = ’#’;
}
else
{
gamemap = ’ ’;
}
//判断蛇是否在当前位置
int k;
for ( k = 0; k 《 len; k++)
{
if (i == x)
{
snake = 1;
break;
}
else
{
snake = 0;
}
}
{
if(fcount&&fx==i&&fy==j)
{
food=1;
}
else
{
food=0;
}
}
//若蛇在当前位置
if (1==snake )
{
printf("*");
}
else if(1==food)
{
printf("f");
}
//若蛇不在当前位置并且当前位置没有水果
else
{
printf("%c", gamemap);
}
}
printf("\n");
}
printf("score:%d",score);
}
//移动
void move(int x)
{
char s;
s=getch();
int move=0,beat=0;
while (1)
{
int cx;
int cy;
memcpy(cx, x, sizeof(int)*len);
memcpy(cy, y, sizeof(int)*len);
//头
if (s==’w’)
{
x--;
move=1;
if(x《=0)
{
printf("Game over\n");
break;
}
}
else if (s==’s’)
{
x++;
move=1;
if(x》=19)
{
printf("Game over\n");
break;
}
}
else if (s==’a’)
{
y --;
move=1;
if(y《=0)
{
printf("Game over\n");
break;
}
}
else if (s==’d’)
{
y++;
move=1;
if(y》=39)
{
printf("Game over\n");
break;
}
}
//身体
int i;
for ( i = 1; i 《 len; i++)
{
x;
y;
}
for(i=1;i《len;i++)//要是咬到了自己
{
if(x)
{
beat=1;
}
else
{
beat=0;
}
}
if(1==beat)
{
printf("Game over\n");
break;
}
if(1==move)
{
if(fcount&&x==fy)//如果吃到了果子
{
//拷贝当前蛇头地址到第二个结点
memcpy(x+1,cx,sizeof(int)*len);
memcpy(y+1,cy,sizeof(int)*len);
len++;
fcount--;
fx=0;
fy=0;
score++;
createfood();
}
Sleep(70);
system("cls");
PrintgameMap( x, y);
}
else
continue;
if(kbhit())//判断是否按下按键
{
s=getch();
}
}
}
C语言贪吃蛇代码求注释
void main()
{
int a={0};/*先声明一个30*30的数组,用于存储屏幕上的蛇、食物的分布情况*/
int i,j,t,flag=0;
char c=’d’,c1=’d’;
struct Food food={10,16,’A’};/*初始化一个食物对象,在10,16位置,它的另一个参数是’A’*/
int gameover=0;/*让游戏状态切换到正常*/
struct Node *head,*p,*rear,*pt;/*head存储蛇的头部,pt用来临时遍历蛇的每一节、rear用来存储蛇的尾部(不过你截的这段代码没有使用到尾部信息的值、p是增删结点时采用的一个临时变量)*/
head=(struct Node *)malloc(sizeof(struct Node));/*给蛇分配内存空间*/
head-》x=10;/*蛇头的x值为10*/
head-》y=16;/*同理*/
head-》pre=NULL;/*蛇头前面没有结点*/
head-》next=NULL;/*蛇头后面也没有结点(空空荡荡的一个大头)*/
rear=head;/*所以~蛇的屁股就是它的脑袋……*/
srand((unsigned)time(NULL));/*初始化随机数发生器,让程序每一次执行时产生的随机数序列都不一样*/
while(1)
{
if(food.x==head-》x && food.y==head-》y)/*如果蛇头碰到了食物*/
{
p=(struct Node *)malloc(sizeof(struct Node));/*创建一个新的结点,并分配空间*/
pt=head;/*pt指向蛇头,准备遍历*/
while(pt-》next!=NULL)/*只要pt指向的结点后面还有结点,即没有遍历到蛇尾*/
pt=pt-》next ;/*pt指向下一个结点*/
p-》pre= pt;/*新增结点的上一个结点是尾部(此时pt已经指向了蛇尾)*/
pt-》next = p;/*蛇尾的下一个结点变为新增结点,链表被链接在一起了*/
p-》next=NULL;/*新增结点的下一个结点设为没有,即下一次遍历时,它默认为尾部*/
rear=p;/*蛇尾变成新增结点*/
food.x=rand()%15;/*初始化下一个食物位置(0-14,0-14)范围内*/
food.y=rand()%15;
food.c=65+rand()%26;/*食物的另一个参数随机分配为某一个英文字母*/
flag=1;/*让旗标变为1,旗标的作用在这段被截断的代码中未知,下同*/
t=0;
贪吃蛇代码
TC下的代码
#include 《dos.h》
#include《alloc.h》
#include 《stdlib.h》
#include 《stdio.h》
#include "graphics.h"
#include《math.h》
typedef struct
{
int x;
int y;
}node;
struct
{
node info;
int length;
}snake;
char light;
void main()
{
int search(int m,int n);
void Light(int m,int n,int color);
int settle(int key);
char met=1;
int i,j,direction,key1,key2=0,rm,rn;
int driver=DETECT,mode=0;
initgraph(&driver,&mode," ");
setcolor(10);
rectangle(0,0,580,450);
snake.length=3;
for(i=0;i《60;i++)
for(j=0;j《48;j++)
light=0;
snake.info.x=32;
snake.info.x=32;
snake.info.x=32;
snake.info.y=24;
snake.info.y=25;
snake.info.y=26;
for(i=0;i《3;i++)
Light(snake.info.y,8);
do
{
if(met==1)
{
do
{
rm=random(57);
rn=random(44);
met=0;
}while(search(rm,rn));
}
Light(rm,rn,10);
if(!bioskey(1))
{
key1=key2;
}
else
{
key1=bioskey(0);
if((key2==0x4b00&&key1==0x4d00)||(key1==0x4b00&&key2==0x4d00)||(key1==0x4800&&key2==0x5000)||(key1==0x5000&&key2==0x4800)||(key1==0x5000&&key2==0))
key1=key2;
else key2=key1;
}
if(key1) met=settle(key1);
if(met==2) return;
for(i=0;i《snake.length;i++)
Light(snake.info.y,8);
for(i=0;i《13-snake.length*0.3;i++) delay(12000);
}while(snake.length《40);
getch();
}
void Light(int m,int n,int color)
{
setfillstyle(1,color);
bar(10*m,10*n,10*m+8,10*n+8);
if(color==8) light=2;
if(color==10) light=1;
if(color==0) light=0;
}
int search(int m,int n)
{
if(light==2)
return(1);
return 0;
}
int settle(int key)
{
int i,rx,ry;
rx=snake.info.y;
for(i=snake.length-1;i》0;i--)
{
snake.info.x;
snake.info.y;
}
switch(key)
{
case 0x4800:
snake.info.y--;
break;
case 0x5000:
snake.info.y++;
break;
case 0x4b00:
snake.info.x--;
break;
case 0x4d00:
snake.info.x++;
break;
}
if(light==1)
{
snake.length++;
snake.info.x=rx;
snake.info.y=ry;
Light(snake.info.y,0);
return(1);
}
if(snake.info==2)
{
return(2);
}
Light(rx,ry,0);
return(0);
}
求贪吃蛇C语言代码
// 2.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include《stdio.h》
#include《conio.h》
#include《time.h》
#include《windows.h》
#include《stdlib.h》
int length=1;//蛇的当前长度,初始值为1
int line;//蛇的走的路线
int head={40,12};//蛇头
int food;//食物的位置
char direction;//蛇运动方向
int x_min=1,x_max=77, y_min=2, y_max=23;//设置蛇的运动区域
int tail_before={40,12};//上一个状态的蛇尾
char direction_before=’s’;//上一个状态蛇的运动方向
int live_death=1;//死活状态,0死,1活
int eat_flag=0;//吃食物与否的状态。0没吃 1吃了
int max=0;
int delay;//移动延迟时间
void gotoxy(int x, int y)//x为列坐标,y为行坐标
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}
void update_score()
{
gotoxy(2,1);
printf("我的分数:%d",length);
gotoxy(42,1);
printf("最高记录:%d",max);
}
void create_window()
{
gotoxy(0,0);
printf("╔══════════════════╦═══════════════════╗");
printf("║ ║ ║");
printf("╠══════════════════╩═══════════════════╣");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("╚══════════════════════════════════════╝");
}
void update_line()
{
int i;
if(eat_flag==0)//吃了食物就不用记住上一个状态的蛇尾,否则会被消掉
{
tail_before;//记住上一个状态的蛇尾
tail_before;
for(i=0;i《length-1;i++)//更新蛇头以后部分
{
line;
line;
}
line;//更新蛇头
line;
}
}
void initial()
{
FILE *fp;
gotoxy(head);
printf("蛇");
line;//把蛇头装入路线
line;
if((fp=fopen("highest","r"))==NULL)
{
fp=fopen("highest","w");
fprintf(fp,"%d",0);
max=0;
fclose(fp);
}//第一次使用时,初始化奖最高分为0
else
{
fp=fopen("highest","r");
fscanf(fp,"%d",&max);
}
update_score();
}
void createfood()
{
int flag,i;
srand((unsigned)time(NULL));
for(;;)
{
for(;;)
{
food=rand()%(x_max+1);
if(food》x_min)
break;
}//产生一个偶数横坐标
for(;;)
{
food=rand()%(y_max);
if(food》y_min)
break;
}
for(i=0,flag=0;i《length;i++)//判断产生的食物是否在蛇身上,在flag=1,否则为0
if(food)
{ flag=1; break; }
if(flag==0)// 食物不在蛇身上 结束循环
break;
}
gotoxy(food);
printf("蛇");
}
void show_snake()
{
gotoxy(head);
printf("蛇");
if(eat_flag==0)//没吃食物时消去蛇尾
{
gotoxy(tail_before);
printf(" ");//消除蛇尾
}
else
eat_flag=0;//吃了食物就回到没吃状态
}
char different_direction(char dir)
{
switch(dir)
{
case ’a’: return ’d’;
case ’d’: return ’a’;
case ’w’: return ’s’;
case ’s’: return ’w’;
}
return 0;
}
void get_direction()
{
direction_before=direction;//记住蛇上一个状态的运动方向
while(kbhit()!=0) //调试
direction=getch();
if( direction_before == different_direction(direction) || (direction!=’a’ && direction!=’s’ && direction!=’d’ && direction!=’w’) ) //新方向和原方向相反,或获得的方向不是wasd时,保持原方向
direction=direction_before;
switch(direction)
{
case ’a’: head-=2; break;
case ’d’: head+=2; break;
case ’w’: head--; break;
case ’s’: head++; break;
}
}
void live_state()//判断蛇的生存状态
{
FILE *fp;
int i,flag;
for(i=0,flag=0;i《length-1;i++)//判断是否自己咬到自己
if( head)
{
flag=1;
break;
}
if(head》=y_max || flag==1)
{
system("cls");
create_window();
update_score();
gotoxy(35,12);
printf("游戏结束!\n");
Sleep(500);
live_death=0;
fp=fopen("highest","w");
fprintf(fp,"%d",max);//保存最高分
}
}
void eat()
{
if(head)
{
length++;
line;//更新蛇头
line;
eat_flag=1;
createfood();
if(length》max)
max=length;
update_score();
if(delay》100)
delay-=30;//加速
}
}
void main()
{
int x=0,y=0;
// int i;
hidden();//隐藏光标
create_window();
initial();
createfood();
for(direction=’s’,delay=600;;)
{
get_direction();
eat();
update_line();
live_state();//判断生死状态
if(live_death==1)
{
show_snake();
}
else
break;
Sleep(delay);
}
}
在dos环境下c语言编程编一个贪吃蛇游戏
程序设计及说明
1
、边墙(
Wall
)
该类规定游戏的范围大小。
2
、蛇类(
Snake
)
用该类生成一个实例蛇
snake
。
3
、移动(
Move
)
该类用于实现对蛇的操作控制,即蛇头方向的上下左右的移动操作。
4
、食物类(
Food
)
该类是游戏过程中食物随机产生的控制和显示。
5
、判断死亡(
Dead
)
该类是对游戏过程中判断玩家操作是否导致蛇的死亡,其中包括蛇头咬食自己身体和蛇头是否触
及游戏“边墙”。
6
、蛇结点(
SnakeNode
)
该类是蛇吃下随机产生的食物从而增加长度的控制类,其中包括蛇长度增加和尾部的变化。
7
、计分统计(
Score
)
该类由于玩家的游戏成绩记录,及游戏结束时的得分输出。
...
部分函数及说明
1.Char menu();
/*
用于玩家选择的游戏速度,返回一个
char
值
*/
2.DELAY(char ch1);
/*
用于控制游戏速度
*/
3.void drawmap();
/*
绘制游戏地图函数
*
4
、
void menu()
/*
游戏帮助信息的输出
*
...
部分类细节解说
1
、蛇的构建
—
Snake
class Snake{
public:
int x
;
int y;
int node;
//
蛇身长度
int direction;//
蛇运动方向
int
life;//
蛇生命,判断死亡
}
2
、随机食物
Food
利用
rand(
)函数进行随机数产生,然后就行坐标定位
void Food(void){
...
int pos_x = 0;
int pos_y = 0;
pos_x = rand() % length;//x
坐标的确定
pos_y = rand() % (width-1);//y
坐标的确定
...
}
3
、蛇头方向确定
利用
switch
语句进行方向确定
...
switch(){
case VK_UP:{
OutChar2.Y--;
y--;
break;
}
case VK_LEFT:{
OutChar2.Y++;
y++;
break;
}
case VK_DOWN:{
OutChar2.X---;
x--;
break;
}
case ’VK_RIGHT:{
OutChar2.X++;
x++;
break;
}
}
代码
#include 《iostream》
#include 《ctime》
#include 《conio.h》
#include 《windows.h》
#include 《time.h》
using namespace std;
int score=0,t=300,f=1;//
得分与时间间隔
/ms
(控制贪吃蛇的速度)
double ss=0,tt=0;//
统计时间所用参数
class Node
{
Node(): x(0), y(0), prior(0), next(0) { }
int x;
int y;
Node *prior;
Node *next;
friend class Snake;
};
class Snake
{
public:
Snake();
~Snake();
void output();
void move();
void change_point(char);
private:
Node *head;
Node *tail;
enum p{ UP
, RIGHT, DOWN, LEFT }point; //
方向
int food_x, food_y; //
食物的坐标
static const int N = 23;
int game;
void add_head(int, int); //
添加坐标为
a,b
的结点
void delete_tail(); //
删除最后一个结点
void greate_food(); //
产生食物
void gotoxy(int, int);
};
void menu();
//
游戏操作菜单
int main()
{
system("color a");
//
初始
cmd
窗口颜色为黑(背景)淡绿(文字)
cout《《"\n\n\n\n\n\n
";
for(int i=0;i《23;i++)
{char star={"Welcome To Snake Game!"};
cout《《star;
Sleep(170);}
cout《《"\n\n
祝你好运!
"《《endl;
Sleep(3000);
if(kbhit()){char kk=getch();if(kk==9)f=5;} //
如果执行,吃一颗星加
5
分
system("cls");
Snake s;
menu();
system("color 1a");
s.output();
while (true)
{
char keydown = getch();
if(keydown==32)getch();
if(keydown==27)return 0;
s.change_point(keydown);
while (!kbhit())
{clock_t start,end;start=clock();
s.move();
s.output();
Sleep(t);
end=clock();tt=(double)(end-start)/CLOCKS_PER_SEC;ss+=tt;
cout《《"
时间:
"《《(int)ss;

更多文章:
androidapp源码免费下载(如何实现APK的反编译得到APK的源码)
2026年9月22日 18:00
service pack 3(操作系统版本升级(SP) Service Pack 3当中的“Service Pack 3”是什么意思)
2026年9月22日 10:20
html代码怎么写大佬教程(html网页的题来个大佬,写代码,题目在图上)
2026年9月22日 10:10
结构体内又一个struct(c++ 在结构体中再嵌入一个结构体如何调用)
2026年9月22日 09:40
cocos creator中文(cocoscreator和cocoscreator3d的区别)
2026年9月22日 02:30





