c语言贪吃蛇最简单代码50行(c++玫瑰花代码)
C语言 的 贪吃蛇 代码 谢谢 大家啦。。。。。。。
你好,很荣幸回答你的问题,我这里是一个c的贪吃蛇源代码,希望对你有帮助,不过运行这个时需要你的软件包含惊蛰EasyX图形函数,比如vc++6.0,如遇到问题问题可以联系我,希望对你有帮助。
#include graphics.h
#include stdlib.h
#include conio.h
#include time.h
#include stdio.h
#define LEFT 'a'
#define RIGHT 'd'
#define DOWN 's'
#define UP 'w'
#define ESC 27
#define N 200 /*蛇的最大长度*/
int i;
char key;
int score=0; /*得分*/
int gamespeed=100; /*游戏速度自己调整*/
struct Food
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*判断是否要出现食物的变量*/
}food; /*食物的结构体*/
struct Snake
{
int x[N];
int y[N];
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=9,gm=2;
initgraph(gd,gm," ");
cleardevice();
}
/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(LIGHTCYAN);
setlinestyle(PS_SOLID,0,1); /*设置线型*/
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)
{
srand(time(NULL)); /*随机数发生器*/
food.yes=1; /*1表示需要出现新食物,0表*/
snake.life=0; /*活着*/
snake.direction=1; /*方向往右*/
snake.x[0]=100;snake.y[0]=100; /*蛇头*/
snake.x[1]=110;snake.y[1]=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;i0;i--) /*蛇的每个环节往前移动,也法/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可*/
for(i=3;isnake.node;i++)
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
GameOver(); /*显示失败*/
snake.life=1;
break;
}
}
if(snake.x[0]55||snake.x[0]595||snake.y[0]55||snake.y[0]455) /*蛇是否撞到墙壁*/
{ GameOver(); /*本次游戏结束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1) /*以上两种判断以后,如果蛇*/
break;
if(snake.x[0]==food.xsnake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(BLACK); /*把画面上的食物东西去*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/
snake.node++; /*蛇的身体长一节*/
food.yes=1; /*画面上需要出现新的食物*/
score+=10;
PrScore(); /*输出新得分*/
}
setcolor(RED); /*画出蛇*/
for(i=0;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
Sleep(gamespeed);
setcolor(BLACK); /*用黑色去除蛇的的最后*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/
if(snake.life==1) /*如果蛇死就跳出循环*/
break;
key=getch(); /*接收按键*/
if (key == ESC) break; /*按ESC键退出*/
switch(key)
{
case UP:
if(snake.direction!=4) /*判断是否往相反的方向移动*/
snake.direction=3;
break;
case RIGHT:
if(snake.direction!=2)
snake.direction=1;
break;
case LEFT:
if(snake.direction!=1)
snake.direction=2;
break;
case DOWN:
if(snake.direction!=3)
snake.direction=4;
break;
}
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
setfont(56,0,"黑体");
outtextxy(200,200,"GAME OVER");
getch();
}
/*输出成绩*/
void PrScore(void)
{
char str[10];
setfillstyle(YELLOW);
bar(50,15,220,35);
setcolor(BROWN);
setfont(16,0,"宋体");
sprintf(str,"score:%d",score);
outtextxy(55,16,str);
}
/*图形结束*/
void Close(void)
{
closegraph();
}
c语言贪吃蛇代码
基本思路:
蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。
#include stdio.h
#include conio.h
#include windows.h
#define BEG_X 2
#define BEG_Y 1
#define WID 20
#define HEI 20
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语言
贪吃蛇c语言代码
#includestdio.h
#includestdlib.h
#includeWindows.h
#includeconio.h
#includetime.h
char gamemap[20][40];//游戏地图大小 20*40
int score=0;//当前分数
//记录蛇的结点
int x[800];//每个结点的行编号
int y[800];//每个结点的列编号
int len = 0;//蛇的长度
//记录水果信息
int fx=0;//食物的横坐标
int fy=0;//食物的纵坐标
int fcount=0;//食物的数目
//主要函数操作
void createfood();//生成食物
void PrintgameMap(int x[],int y[]);//画游戏地图
void move(int x[],int y[]);//移动蛇
int main()
{
srand(time(NULL));
//初始化蛇头和身体的位置,默认刚开始蛇长为2
x[len] = 9;
y[len] = 9;
len++;
x[len] = 9;
y[len] = 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;ilen;i++)
{
for(j=0;jlen;j++)
{
if(x[i]==fxy[j]==fy)
{
have=1;
break;
}
else
{
have=0;
}
}
if(1==have)//若为蛇的一部分,执行下一次循环
{
continue;
}
else//否则生成新的水果
{
fcount++;
fx=tfx;
fy=tfy;
break;
}
}
}
}
//游戏地图
void PrintgameMap(int x[],int y[])
{
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[i][j] = '=';
}
else if (i == 19 j = 1 j = 38)
{
gamemap[i][j] = '=';
}
else if (j == 0 || j == 39)
{
gamemap[i][j] = '#';
}
else
{
gamemap[i][j] = ' ';
}
//判断蛇是否在当前位置
int k;
for ( k = 0; k len; k++)
{
if (i == x[k]j == y[k])
{
snake = 1;
break;
}
else
{
snake = 0;
}
}
{
if(fcountfx==ify==j)
{
food=1;
}
else
{
food=0;
}
}
//若蛇在当前位置
if (1==snake )
{
printf("*");
}
else if(1==food)
{
printf("f");
}
//若蛇不在当前位置并且当前位置没有水果
else
{
printf("%c", gamemap[i][j]);
}
}
printf("\n");
}
printf("score:%d",score);
}
//移动
void move(int x[],int y[])
{
char s;
s=getch();
int move=0,beat=0;
while (1)
{
int cx[800];
int cy[800];
memcpy(cx, x, sizeof(int)*len);
memcpy(cy, y, sizeof(int)*len);
//头
if (s=='w')
{
x[0]--;
move=1;
if(x[0]=0)
{
printf("Game over\n");
break;
}
}
else if (s=='s')
{
x[0]++;
move=1;
if(x[0]=19)
{
printf("Game over\n");
break;
}
}
else if (s=='a')
{
y[0] --;
move=1;
if(y[0]=0)
{
printf("Game over\n");
break;
}
}
else if (s=='d')
{
y[0]++;
move=1;
if(y[0]=39)
{
printf("Game over\n");
break;
}
}
//身体
int i;
for ( i = 1; i len; i++)
{
x[i] = cx[i - 1];
y[i] = cy[i - 1];
}
for(i=1;ilen;i++)//要是咬到了自己
{
if(x[0]==x[i]y[0]==y[i])
{
beat=1;
}
else
{
beat=0;
}
}
if(1==beat)
{
printf("Game over\n");
break;
}
if(1==move)
{
if(fcountx[0]==fxy[0]==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语言 简易程序设计
#includegraphics.h
#includestdlib.h
#define N 200
#define up 0x4800
#define down 0x5000
#define left 0x4b00
#define right 0x4d00
#define esc 0x011b
#define Y 0x1579
#define n 0x316e
int gamespeed; /* 游戏速度 */
int i,key,color;
int score=0; /* 游戏分数 */
char cai48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x04,0x00,0x18,0x00,0x00,0x00,0x0E,0x00,
0x1C,0x00,0x00,0x00,0x1C,0x00,0x1C,0x00,
0x00,0x00,0x20,0x00,0x38,0x00,0x00,0x00,
0x40,0x00,0x78,0x00,0x00,0x01,0x80,0x40,
0x70,0x00,0x00,0x03,0x80,0xC0,0xE0,0x00,
0x00,0x07,0x80,0x80,0xC0,0x00,0x00,0x0E,
0x11,0x81,0xC0,0x00,0x00,0x08,0x61,0x01,
0x80,0x00,0x00,0x00,0x23,0x03,0x04,0x00,
0x00,0x02,0x02,0x00,0x06,0x00,0x00,0x1E,
0x04,0x00,0x0F,0x00,0x00,0x1C,0x1F,0x80,
0x1E,0x00,0x00,0x08,0x3F,0x80,0x3C,0x00,
0x00,0x00,0xFF,0x80,0x38,0x00,0x00,0x03,
0xFF,0x80,0x78,0x00,0x00,0x0F,0xF8,0x00,
0xF0,0x00,0x00,0x7F,0xF0,0x00,0xE0,0x00,
0x03,0xFF,0xFC,0x01,0x80,0x00,0x03,0xC0,
0xFF,0x01,0x03,0x80,0x01,0x01,0xFF,0x00,
0x03,0x80,0x00,0x01,0x3F,0x00,0x07,0x80,
0x00,0x02,0x11,0x00,0x07,0x00,0x00,0x00,
0x10,0x00,0x07,0x00,0x00,0x00,0x10,0x00,
0x0E,0x00,0x00,0x08,0x10,0x00,0x1C,0x00,
0x00,0x30,0x10,0x00,0x18,0x00,0x00,0x70,
0x10,0x00,0x30,0x00,0x01,0xE0,0x10,0x00,
0x70,0x00,0x03,0x80,0x10,0x00,0x60,0x00,
0x00,0x00,0x30,0x00,0xE0,0x00,0x00,0x00,
0xF0,0x01,0xC0,0x00,0x00,0x00,0x70,0x03,
0xC0,0x00,0x00,0x00,0x10,0x07,0x80,0x00,
0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,
0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x3C,
0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,
0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char she48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,
0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,
0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,
0x00,0x0E,0x00,0x00,0x00,0x03,0x00,0x07,
0x00,0x00,0x00,0x02,0x00,0x03,0x00,0x00,
0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x02,
0x00,0x00,0xF8,0x00,0x00,0x02,0x00,0x07,
0x86,0x00,0x00,0x02,0x00,0x18,0x03,0x00,
0x00,0x02,0x00,0x00,0x07,0x80,0x00,0x03,
0xF0,0x00,0x07,0x80,0x00,0x0F,0xFC,0x00,
0x0C,0x00,0x00,0x7E,0x3F,0x80,0x00,0x00,
0x01,0xFE,0x1F,0x80,0x00,0x00,0x01,0xE2,
0x39,0x8C,0x00,0x00,0x00,0xC2,0x30,0x08,
0x00,0x00,0x00,0xC2,0x60,0x08,0x00,0x00,
0x00,0xC3,0xE0,0x08,0x60,0x00,0x00,0x7F,
0xE0,0x01,0xE0,0x00,0x00,0x3F,0x80,0x1F,
0xE0,0x00,0x00,0x1E,0x00,0x1F,0x80,0x00,
0x00,0x1E,0x00,0x1F,0x00,0x00,0x00,0x02,
0x38,0x1E,0x00,0x00,0x00,0x07,0xFC,0x1C,
0x00,0x20,0x00,0x07,0xFC,0x18,0x00,0x20,
0x00,0x1F,0x0C,0x10,0x00,0x20,0x00,0x7C,
0x04,0x10,0x00,0x60,0x01,0xF0,0x00,0x10,
0x00,0x60,0x01,0xE0,0x00,0x08,0x00,0xF0,
0x00,0x80,0x00,0x08,0x03,0xF0,0x00,0x00,
0x00,0x07,0xFF,0xF0,0x00,0x00,0x00,0x07,
0xFF,0xF0,0x00,0x00,0x00,0x03,0xFF,0xE0,
0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char tun48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x3E,
0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,
0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,
0x03,0xC0,0x00,0x00,0x00,0x00,0x1F,0x00,
0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,
0x00,0x01,0xF8,0x00,0x00,0x00,0x00,0x03,
0xF8,0x00,0x40,0x00,0x00,0x00,0x06,0x07,
0xC0,0x00,0x00,0x00,0x07,0xFF,0xE0,0x00,
0x00,0x00,0x07,0xFF,0xE0,0x00,0x00,0x00,
0x0F,0xFF,0x80,0x00,0x00,0x00,0x7F,0xF8,
0x00,0x00,0x00,0x1F,0xFF,0xF8,0x00,0x00,
0x00,0x1F,0xFF,0xF8,0x00,0x00,0x00,0x1F,
0xFC,0x3C,0x00,0x00,0x00,0x0F,0xF8,0x0E,
0x00,0x00,0x00,0x04,0x70,0x07,0x00,0x00,
0x00,0x00,0x60,0x03,0x80,0x00,0x00,0x00,
0xC0,0x00,0xC0,0x00,0x00,0x01,0x80,0x00,
0x30,0x00,0x00,0x01,0x00,0x3C,0x18,0x00,
0x00,0x02,0x03,0xFF,0x0C,0x00,0x00,0x0C,
0x7F,0xFF,0x8E,0x00,0x00,0x18,0xFF,0xFF,
0xC7,0x80,0x00,0x78,0xFE,0x07,0x87,0xE0,
0x01,0xF0,0x70,0x07,0x03,0xF8,0x07,0xE0,
0x70,0x0E,0x03,0xFE,0x00,0x00,0x38,0x1E,
0x01,0xFE,0x00,0x00,0x3F,0xFE,0x00,0x0C,
0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,0x00,
0x1F,0xFE,0x00,0x00,0x00,0x00,0x0F,0xFE,
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char dan48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFC,0x00,0x00,0x00,0x00,0x07,0xFF,
0x00,0x00,0x00,0x00,0x7F,0xC0,0x80,0x00,
0x00,0x03,0xFF,0x80,0x40,0x00,0x00,0x01,
0xF1,0x80,0x40,0x00,0x00,0x01,0x81,0x80,
0xE0,0x00,0x00,0x00,0x01,0x93,0xF0,0x00,
0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,
0x21,0xFF,0xF0,0x00,0x00,0x00,0x21,0xF8,
0x00,0x00,0x00,0x00,0x61,0xC0,0x00,0x00,
0x00,0x00,0x61,0x80,0x00,0x00,0x00,0x00,
0xF3,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,
0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0x00,
0x00,0x03,0xFF,0xF8,0x00,0x00,0x00,0x02,
0x00,0xFC,0x00,0x00,0x00,0x04,0x02,0x1F,
0x00,0x00,0x00,0x08,0x03,0x01,0xC0,0x00,
0x00,0x38,0x03,0x00,0x7C,0x00,0x00,0xF8,
0x07,0xF8,0x3F,0xC0,0x01,0xF0,0x3F,0xFE,
0x3F,0xF8,0x03,0xC1,0xFF,0x0F,0x1F,0xF8,
0x00,0x01,0xE3,0x0F,0x0F,0xF0,0x00,0x01,
0xC3,0x0E,0x00,0x00,0x00,0x01,0x83,0xFC,
0x00,0x00,0x00,0x00,0xC7,0xF8,0x00,0x00,
0x00,0x00,0xFF,0xF8,0x00,0x00,0x00,0x00,
0x7F,0xF0,0x00,0x00,0x00,0x00,0x3F,0x03,
0x80,0x00,0x00,0x00,0x03,0x04,0x00,0x00,
0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x00,
0x1F,0xF8,0x20,0x00,0x00,0x00,0xFF,0xFF,
0xE0,0x00,0x00,0x07,0xFF,0x81,0xE0,0x00,
0x00,0x07,0xE0,0x00,0xE0,0x00,0x00,0x03,
0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char zuo16H[]={
0x18,0xC0,0x18,0xC0,0x19,0x80,0x31,0xFE,
0x33,0xFE,0x76,0xC0,0xF0,0xFC,0xB0,0xFC,
0x30,0xC0,0x30,0xC0,0x30,0xFE,0x30,0xFE,
0x30,0xC0,0x30,0xC0,0x30,0xC0,0x00,0x00,
};
char zhe16H[]={
0x03,0x00,0x03,0x0C,0x1F,0xCC,0x1F,0xD8,
0x03,0x30,0xFF,0xFE,0xFF,0xFE,0x03,0x00,
0x0F,0xF8,0x3F,0xF8,0xEC,0x18,0xCF,0xF8,
0x0C,0x18,0x0F,0xF8,0x0F,0xF8,0x00,0x00,
};
char tian16H[]={
0x00,0x00,0x3F,0xFC,0x3F,0xFC,0x31,0x8C,
0x31,0x8C,0x31,0x8C,0x3F,0xFC,0x3F,0xFC,
0x31,0x8C,0x31,0x8C,0x31,0x8C,0x3F,0xFC,
0x3F,0xFC,0x30,0x0C,0x00,0x00,0x00,0x00,
};
char xue16H[]={
0x33,0x18,0x19,0x98,0x08,0xB0,0x7F,0xFC,
0x7F,0xFC,0x60,0x0C,0x1F,0xF0,0x1F,0xF0,
0x00,0xC0,0x7F,0xFC,0x7F,0xFC,0x01,0x80,
0x01,0x80,0x07,0x80,0x03,0x00,0x00,0x00,
};
char ke16H[]={
0x00,0x00,0x0C,0x18,0xFD,0x98,0xF8,0xD8,
0x18,0x58,0xFE,0x18,0xFE,0x98,0x18,0xD8,
0x3C,0x58,0x7E,0x1E,0xDB,0xFE,0x9B,0xF8,
0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,
};
struct Food/*定义结构体存储食物的属性*/
{
int x; /* 食物的坐标 */
int y;
int yes; /* 值为0表示屏幕上没有食物,值为1表示屏幕上有食物 */
int color; /* 食物颜色 */
}food;
struct Snake/*定义结构体存储蛇的属性*/
{
int x[N]; /* 每一节蛇的坐标 */
int y[N];
int color[N];/*存储每一节蛇的颜色*/
int node; /* 蛇的节数 */
int direction; /* 蛇移动的方向 */
int life; /* 蛇的生命,如果为1,蛇死,游戏结束 */
}snake;
void init(void)/*图形驱动*/
{
int driver=DETECT,mode=0;
registerbgidriver(EGAVGA_driver);
initgraph(driver,mode,"");
}
void drawmat(char *mat,int matsize,int x,int y,int color)/*汉字点阵*/
{
int i,j,k,m;
m=(matsize-1)/8+1;
for(j=0;jmatsize;j++)
for(i=0;im;i++)
for(k=0;k8;k++)
if(mat[j*m+i](0x80k))
putpixel(x+i*8+k,y+j,color);
}
void showword(void)
{/* 调用汉字点阵输出程序,显示标题和作者信息 */
drawmat(cai48H,48,249,-4,7);
drawmat(she48H,48,329,-4,7);
drawmat(tun48H,48,409,-4,7);
drawmat(dan48H,48,489,-4,7);
drawmat(cai48H,48,250,-5,4);
drawmat(she48H,48,330,-5,4);
drawmat(tun48H,48,410,-5,4);
drawmat(dan48H,48,490,-5,4);
/*作者 田学科*/
drawmat(zuo16H,16,515,465,7);
drawmat(zhe16H,16,530,465,7);
drawmat(tian16H,16,550,465,7);
drawmat(xue16H,16,565,465,7);
drawmat(ke16H,16,580,465,7);
}
void draw(void)/*画出四周的墙*/
{
if(color==15)
color=0;
setcolor(++color);
setlinestyle(SOLID_LINE,0,1);
for(i=30;i=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i450;i+=10)
{
rectangle(30,i,39,i+10);
rectangle(601,i,610,i+10);
}
}
void prscore(void)
{/* 打印游戏分数 */
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,10,200,30);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,15,str);
}
void gameover(void)
{
cleardevice(); /* 清屏函数 */
for(i=0;isnake.node;i++)/* 画出蛇死时的位置 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}
prscore(); /* 显示分数 */
draw();
showword();
settextstyle(0,0,6);
setcolor(7);
outtextxy(103,203,"GAME OVER");
setcolor(RED);
outtextxy(100,200,"GAME OVER");
}
void gameplay(void)/* 玩游戏的具体过程 */
{
int flag,flag1;
randomize();
prscore();
gamespeed=50000;
food.yes=0;/* food.yes=0表示屏幕上没有食物 */
snake.life=1;/* snake.life=1表示蛇是活着的 */
snake.direction=4;/* 表示蛇的初始方向为向右 */
snake.node=2; /* 蛇的初始化为两节 */
snake.color[0]=2; /*两节蛇头初始化为绿色*/
snake.color[1]=2;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
food.color=random(15)+1;
while(1)
{
while(1)
{
if(food.yes==0) /* 如果蛇活着 */
{
while(1)
{
flag=1;
food.yes=1;
food.x=random(56)*10+40;
food.y=random(40)*10+50;
for(i=0;isnake.node;i++)
{
if(food.x==snake.x[i]food.y==snake.y[i])
flag=0;
}
if(flag) break;
}
}
if(food.yes)
{
setcolor(food.color);
rectangle(food.x,food.y,food.x+10,food.y+10);
}
for(i=snake.node-1;i0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction)
{
case 1: snake.y[0]-=10;break;
case 2: snake.y[0]+=10;break;
case 3: snake.x[0]-=10;break;
case 4: snake.x[0]+=10;break;
}
for(i=3;isnake.node;i++)
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
gameover();
snake.life=0;
break;
}
}
if(snake.x[0]40||snake.x[0]590||snake.y[0]50||snake.y[0]440)
{
gameover();
snake.life=0;
}
if(snake.life==0)
break;
if(snake.x[0]==food.xsnake.y[0]==food.y)/*蛇吃掉食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y+10);
snake.x[snake.node]=-20;
snake.y[snake.node]=-20;
snake.color[snake.node]=food.color;
snake.node++;
food.yes=0;
food.color=random(15)+1;
score+=10;
prscore();
if(score%100==0score!=0)
{
for(i=0;isnake.node;i++)/* 画出蛇 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}
sound(200);
delay(50000);delay(50000);delay(50000);
delay(50000);delay(50000);delay(50000);
nosound();
gamespeed-=5000;
draw();
}
else
{
sound(500);
delay(500);
nosound();
}
}
for(i=0;isnake.node;i++)/* 画出蛇 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}
delay(gamespeed);
delay(gamespeed);
flag1=1;
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]+10);
if(kbhit()flag1==1)/*如果没按有效键就重新开始循环*/
{
flag1=0;
key=bioskey(0);
if(key==esc)
exit(0);
else if(key==upsnake.direction!=2)
snake.direction=1;
else if(key==downsnake.direction!=1)
snake.direction=2;
else if(key==leftsnake.direction!=4)
snake.direction=3;
else if(key==rightsnake.direction!=3)
snake.direction=4;
}
}
if(snake.life==0)/*如果蛇死了就退出循环*/
break;
}
}
void main(void)
{
while(1)
{
color=0;
init();
cleardevice();
showword();
draw();
gameplay();
setcolor(15);
settextstyle(0,0,2);
outtextxy(200,400,"CONTINUE(Y/N)?");
while(1)
{
key=bioskey(0);
if(key==Y||key==n||key==esc)
break;
}
if(key==n||key==esc)
break;
}
closegraph();
}
C语言的贪吃蛇源代码
?
//******友情提示:如想速度快点,请改小_sleep(500)函数中参数*****??
#include?stdio.h??
#include?stdlib.h??
#include?conio.h??
#include?string.h??
#include?time.h??
const?int?H?=?8;???//地图的高??
const?int?L?=?16;??//地图的长??
char?GameMap[H][L];???//游戏地图??
int??key;??//按键保存??
int??sum?=?1,?over?=?0;??//蛇的长度,?游戏结束(自吃或碰墙)??
int??dx[4]?=?{0,?0,?-1,?1};??//左、右、上、下的方向??
int??dy[4]?=?{-1,?1,?0,?0};??
struct?Snake???//蛇的每个节点的数据类型??
{??
?int?x,?y;??//左边位置??
?int?now;???//保存当前节点的方向,?0,1,2,3分别为左右上下??
}Snake[H*L];??
const?char?Shead?=?'@';??//蛇头??
const?char?Sbody?=?'#';??//蛇身??
const?char?Sfood?=?'*';??//食物??
const?char?Snode?=?'.';??//'.'在地图上标示为空??
void?Initial();??//地图的初始化??
void?Create_Food();?//在地图上随机产生食物??
void?Show();???//刷新显示地图??
void?Button();??//取出按键,并判断方向??
void?Move();???//蛇的移动??
void?Check_Border();??//检查蛇头是否越界??
void?Check_Head(int?x,?int?y);???//检查蛇头移动后的位置情况??
int?main()???
{??
?Initial();??
?Show();??
?return?0;??
}??
void?Initial()??//地图的初始化??
{??
?int?i,?j;??
?int?hx,?hy;??
?system("title?贪吃蛇");??//控制台的标题??
?memset(GameMap,?'.',?sizeof(GameMap));??//初始化地图全部为空'.'??
?system("cls");??
?srand(time(0));???//随机种子??
?hx?=?rand()%H;????//产生蛇头??
?hy?=?rand()%L;??
?GameMap[hx][hy]?=?Shead;??
?Snake[0].x?=?hx;??Snake[0].y?=?hy;??
?Snake[0].now?=?-1;??
?Create_Food();???//随机产生食物??
?for(i?=?0;?i??H;?i++)???//地图显示??
?{???
??for(j?=?0;?j??L;?j++)??
???printf("%c",?GameMap[i][j]);??
??printf("\n");??
?}??
?????
?printf("\n小小C语言贪吃蛇\n");??
?printf("按任意方向键开始游戏\n");??
????
?getch();???//先接受一个按键,使蛇开始往该方向走??
?Button();??//取出按键,并判断方向??
}??
void?Create_Food()??//在地图上随机产生食物??
{??
?int?fx,?fy;??
?while(1)??
?{??
??fx?=?rand()%H;??
?????fy?=?rand()%L;??
?????
??if(GameMap[fx][fy]?==?'.')??//不能出现在蛇所占有的位置??
??{???
???GameMap[fx][fy]?=?Sfood;??
??????break;??
??}??
?}??
}??
void?Show()??//刷新显示地图??
{??
?int?i,?j;??
?while(1)??
?{????
??_sleep(500);?//延迟半秒(1000为1s),即每半秒刷新一次地图??
??Button();???//先判断按键在移动??
??Move();??
??if(over)??//自吃或碰墙即游戏结束??
??{???
???printf("\n**游戏结束**\n");??
???printf("?????_\n");??
???getchar();??
??????break;??
??}??
??system("cls");???//清空地图再显示刷新吼的地图??
??for(i?=?0;?i??H;?i++)???
??{???
???for(j?=?0;?j??L;?j++)??
????printf("%c",?GameMap[i][j]);??
???printf("\n");??
??}??
?????
??printf("\n小小C语言贪吃蛇\n");??
??printf("按任意方向键开始游戏\n");??
?}??
}??
void?Button()??//取出按键,并判断方向??
{??
?if(kbhit()?!=?0)?//检查当前是否有键盘输入,若有则返回一个非0值,否则返回0??
?{???
??while(kbhit()?!=?0)??//可能存在多个按键,要全部取完,以最后一个为主??
??????key?=?getch();?//将按键从控制台中取出并保存到key中??
??switch(key)??
??{???//左??
???case?75:??Snake[0].now?=?0;??
??????????break;??
????????????//右??
????????????case?77:??Snake[0].now?=?1;???????
??????????break;??
????????????//上??
???case?72:??Snake[0].now?=?2;??
??????????break;??
????????????//下??
???case?80:??Snake[0].now?=?3;??
??????????break;??
??}??
?}??
}??
void?Move()???//蛇的移动??
{??
?int?i,?x,?y;??
????int?t?=?sum;??//保存当前蛇的长度??
?//记录当前蛇头的位置,并设置为空,蛇头先移动??
?x?=?Snake[0].x;??y?=?Snake[0].y;??GameMap[x][y]?=?'.';??
?Snake[0].x?=?Snake[0].x?+?dx[?Snake[0].now?];??
?Snake[0].y?=?Snake[0].y?+?dy[?Snake[0].now?];??
?Check_Border();???//蛇头是否越界??
?Check_Head(x,?y);??//蛇头移动后的位置情况,参数为:?蛇头的开始位置??
?if(sum?==?t)??//未吃到食物即蛇身移动哦??
????for(i?=?1;?i??sum;?i++)??//要从蛇尾节点向前移动哦,前一个节点作为参照??
?{??
??if(i?==?1)???//尾节点设置为空再移动??
???GameMap[?Snake[i].x?][?Snake[i].y?]?=?'.';??
?????
??if(i?==?sum-1)??//为蛇头后面的蛇身节点,特殊处理??
??{??
???Snake[i].x?=?x;??
?????????Snake[i].y?=?y;??
??????Snake[i].now?=?Snake[0].now;??
??}??
??else???//其他蛇身即走到前一个蛇身位置??
??{??
???Snake[i].x?=?Snake[i+1].x;??
?????????Snake[i].y?=?Snake[i+1].y;??
??????Snake[i].now?=?Snake[i+1].now;??
??}??
??????
??GameMap[?Snake[i].x?][?Snake[i].y?]?=?'#';?//移动后要置为'#'蛇身???
?}??
}??
void?Check_Border()??//检查蛇头是否越界??
{??
?if(Snake[0].x??0?||?Snake[0].x?=?H??
?||?Snake[0].y??0?||?Snake[0].y?=?L)??
?????over?=?1;??
}??
void?Check_Head(int?x,?int?y)??//检查蛇头移动后的位置情况??
{??
????
?if(GameMap[?Snake[0].x?][?Snake[0].y?]?==?'.')??//为空??
??GameMap[?Snake[0].x?][?Snake[0].y?]?=?'@';??
?else?
??if(GameMap[?Snake[0].x?][?Snake[0].y?]?==?'*')??//为食物??
??{??
???GameMap[?Snake[0].x?][?Snake[0].y?]?=?'@';????
???Snake[sum].x?=?x;???//新增加的蛇身为蛇头后面的那个??
??????Snake[sum].y?=?y;??
??????Snake[sum].now?=?Snake[0].now;??
?????????GameMap[?Snake[sum].x?][?Snake[sum].y?]?=?'#';???
???sum++;??
???Create_Food();??//食物吃完了马上再产生一个食物??
??}??
??else?
???over?=?1;??
}