C语言实现贪吃蛇游戏

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C语言实现贪吃蛇游戏.txt每天早上起床都要看一遍“福布斯”富翁排行榜,如果上面没有我的名字,我就去上班。

谈钱不伤感情,谈感情最他妈伤钱。

我诅咒你一辈子买方便面没有调料包。

/*===========================================================
*程序名:贪吃蛇
/*===========================================================
*调用库:
*----------------------
*图形库graphics.h,随机库stdlib.h
*===========================================================*/
#include <graphics.h>
#include <stdlib.h>
/*===========================================================
*宏定义:
*----------------------
*上、下、左、右、退出(ESC),暂停(PAUSE),确定(ENTER)
*alive---蛇活着,dead---蛇死亡
*no---食物不存在,yes---食物存在
*N---蛇最大长度,达到200时游戏完成
*===========================================================*/
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920
#define ENTER 0x1c0d
#define alive 1
#define dead 0
#define yes 1
#define no 0
#define N 200
/*===========================================================
*自定义函数以及全局变量声明
*---------------------------
*Score---统计吃的食物个数;Speed_degree:控制蛇速度变量
*===========================================================*/
void make_full_screen();
void start_screen();
void menu();
void wall_note();
void play_game();
void score();
void game_over();
void close_full_screen();
void victory();
void forum();
void leave_or_again();
void victory();
int Score,Speed_degree;
/*=========================================================== *自定义函数:make_full_screen()
*---------------------------------
*初始化图形界面
*===========================================================*/ void make_full_screen()
{
int driver=DETECT,mode;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
cleardevice();
}
/*=========================================================== *自定义函数:start_screen()
*------------------------------
*贪吃蛇游戏的欢迎界面
*===========================================================*/ void start_screen()
{
int i,p,color=9;
settextstyle(0,0,2);
setcolor(GREEN);
outtextxy(150,400,"Press any key to loading...");
setcolor(YELLOW);
outtextxy(200,450,"Deviser:caolvchong");
forum();
while(bioskey(1)==0) /*当没有按键时显示snake*/
{
settextstyle(0,0,6);
for(i=9;i<15;i++)
{
color++;
if(color>=14) color=9;
setcolor(color);
outtextxy(200,200,"SNAKE");
for(p=1;p<3;p++)
delay(50000);
}
}
bioskey(0);/*返回键盘值,不然按下的扫描码将被后面检测按键函数接收*/ cleardevice();/*清屏*/
}
/*===========================================================
*自定义函数:menu()
*------------------------------
*贪吃蛇游戏的菜单界面
*===========================================================*/
void menu()
{
int j=100,k=j,n,key,tag=1;
char *char_up=NULL,*char_down=NULL;
/*----------------------------------------------
*参数说明:
*j:作为显示选择条(bar)的位置参数
*k: 作为清除选择条的位置参数
*key:接收按键参数
*tag:跳出menu()函数的标签,触发条件按下enter
**char_up,*char_down接收ASCII码的24(↑)和25(↓)
*----------------------------------------------*/
setcolor(RED);
settextstyle(0,0,2);
outtextxy(15,15,"choose a level");
setcolor(LIGHTBLUE);
settextstyle(0,0,3);
outtextxy(15,100,"Easy");
outtextxy(15,150,"Normal");
outtextxy(15,200,"Hard");
setcolor(GREEN);
settextstyle(0,0,2);
sprintf(char_up,"%c",24);
outtextxy(150,350,char_up);
sprintf(char_down,"%c",25);
outtextxy(210,350,char_down);
outtextxy(170,350,"or ");
outtextxy(230,350,"to select");
outtextxy(150,380,"ENTER to play");
outtextxy(150,410,"ESC to exit");
setfillstyle(2,YELLOW);
bar(190,j,215,j+25);
forum();
while(tag!=0)
{
setfillstyle(1,BLACK);
bar(190,k,215,k+25);
setfillstyle(2,YELLOW);
bar(190,j,215,j+25);
key=bioskey(0);
switch(key)
{
case DOWN: k=j;if(j<200) j+=50;break;
case UP: k=j;if(j>100) j-=50;break;
case ENTER: tag=0;break;
case ESC: close_full_screen(); /*退出*/
}
switch(j)
{
case 100:Speed_degree=12;break;
case 150:Speed_degree=8;break;
case 200:Speed_degree=4;break; /*对应各等级的速度延迟循环次数*/ }
}
}
/*===========================================================
*自定义函数:wall_note()
*------------------------------
*贪吃蛇游戏的围墙,就是蛇的活动范围
*以及游戏中提示按键:ESC--退出;SPACE--暂停
*===========================================================*/
void wall_note()
{
cleardevice();
setlinestyle(0,0,3);
setcolor(LIGHTRED);
rectangle(47,57,603,453);
setfillstyle(1,LIGHTGREEN);
bar(55,10,600,40);
settextstyle(0,0,3);
setcolor(RED);
outtextxy(58,15,"ESC:exit");
outtextxy(305,15,"SPACE:pause");
forum();
}
/*=========================================================== *自定义函数:play_game()
*------------------------------
*具体的游戏过程
*===========================================================*/ void play_game()
{
struct
{
int x[N];
int y[N];
int block;
int life;
int direction;
}snake;
struct
{
int x;
int y;
int exist;
}food;
int i,key;
/*---------------------------------
*参数说明:
*结构体snake.[x],snake.[y]为蛇身体坐标
*snake.block蛇的节数;snake.life蛇生命参数
*snake.direction蛇的运动方向
*--------------
*结构体food.x,food.y为食物坐标
*food.exist食物存在参数
*-------------
*i:一些循环控制参量
*key:接收键盘按键参量
*----------------------------------*/
randomize();/*初始化随机库*/
snake.x[0]=100;snake.y[0]=100;
snake.direction=RIGHT;snake.life=alive;snake.block=3;
food.exist=no;
score();/*初始分数*/
/*-----------------------------------
*初始化:
*蛇的头部位置,运动方向向右,生命活着
*节数为3,食物开始不存在,初始化分数显示为0
*-----------------------------------*/
for(;;)
/* 循环,作用于下面while(!kbhit),按键后重新开始
* while(!hkbit)循环,并对按键分析,实现上下左右
*以及退出暂停的检测*/
{
while(!kbhit())
/*没有按键时,实现对食物是否存在的判断,对于不存
*在时产生食物,并画出;
*对蛇移动的处理:自动向前移动,对接收来的上下左
*右的处理,对蛇运动过程是否导致死亡判断,对蛇身
*体变长以及画出蛇的处理
*对分数的处理:显示分数,对是否完全200个进行判断*/
{
if(food.exist==no)/*没有食物时,随机出现食物*/
{
food.x=random(531)+60;
food.y=random(381)+60;
/*随机出现食物,确保食物在蛇的活动范围内*/
while(food.x%10!=0) food.x++;
while(food.y%10!=0) food.y++;
/*确保食物在屏幕坐标10的正数倍,这样才能被蛇吃到*/ food.exist=yes;/*食物存在了*/
for(i=0;i<snake.block;i++)
if(food.x==snake.x&&food.y==snake.y)
{
food.exist=no;
break;
}
/*如果食物在蛇的身体内,重新产生食物*/
}
setlinestyle(0,0,1);
setcolor(RED);
rectangle(food.x,food.y,food.x+10,food.y+10);/*画出食物*/
for(i=snake.block-1;i>0;i--)
{
snake.x=snake.x[i-1];
snake.y=snake.y[i-1];
}/*蛇身体后面一格变前面一格,实现蛇移动的原理*/
switch(snake.direction)
{
case RIGHT: snake.x[0]+=10;break;
case LEFT: snake.x[0]-=10;break;
case UP: snake.y[0]-=10;break;
case DOWN: snake.y[0]+=10;break;
}/*蛇上下左右移动的处理*/
for(i=4;i<snake.block;i++)
if(snake.x==snake.x[0]&&snake.y==snake.y[0])
{
snake.life=dead;
break;
}/*对蛇是否碰到自己的判断,碰到自己,蛇死*/
if(snake.x[0]<48||snake.x[0]>597||snake.y[0]<53||snake.y[0]>447) snake.life=dead; /*碰到墙,蛇死*/
if(snake.life==dead)
{
game_over();
break;
}/*如果蛇死的话,显示游戏结束,退出while(!hkbit())循环*/
if(food.x==snake.x[0]&&food.y==snake.y[0])/*食物被吃*/
{
setcolor(BLACK);
rectangle(food.x,food.y,food.x+10,food.y+10);/*把食物去掉*/ snake.block++;/*蛇身增加*/
Score++; /*分数增加*/
score();/*统计显示分数*/
victory();/*吃到食物200个显示完成游戏*/
food.exist=no;/*食物被吃,食物就不存在了*/
}
setcolor(LIGHTBLUE);
for(i=1;i<snake.block;i++)
{
setlinestyle(0,0,1);
rectangle(snake.x,snake.y,snake.x+10,snake.y+10);
}/*画蛇*/
setcolor(YELLOW);
rectangle(snake.x[0],snake.y[0],snake.x[0]+10,snake.y[0]+10);
/*设置蛇的头部*/
snake.x[snake.block]=-100;snake.y[snake.block]=-100;
for(i=0;i<Speed_degree;i++)
delay(10000);
setcolor(BLACK);
rectangle(snake.x[snake.block-1],snake.y[snake.block-1],
snake.x[snake.block-1]+10,snake.y[snake.block-1]+10);
/*去掉蛇的最后一节*/
}
key=bioskey(0);/*等待按键*/
if(key==SPACE) bioskey(0);/*暂停*/
else if(key==ESC) closegraph();/*结束游戏*/
else if(key==RIGHT&&snake.direction!=LEFT) snake.direction=RIGHT; else if(key==UP&&snake.direction!=DOWN) snake.direction=UP;
else if(key==LEFT&&snake.direction!=RIGHT) snake.direction=LEFT; else if(key==DOWN&&snake.direction!=UP) snake.direction=DOWN; if(snake.life==dead) break;
}
}
/*===========================================================
*自定义函数:score()
*------------------------------
*统计显示分数
*===========================================================*/
void score()
{
char *str=NULL;
setfillstyle(0,BLACK);
bar(250,460,405,490);
setcolor(LIGHTGREEN);
settextstyle(0,0,2);
sprintf(str,"Score:%d",Score);
outtextxy(255,460,str);
}
/*=========================================================== *自定义函数:victory()
*------------------------------
*吃到食物200个,完成游戏
*===========================================================*/ void victory()
{
if(Score==200)
{
cleardevice();
setcolor(YELLOW);
settextstyle(0,0,6);
outtextxy(100,200,"Victory!");
forum();
leave_or_again();
}
}
/*=========================================================== *自定义函数:game_over()
*------------------------------
*显示游戏结束,选择继续游戏还是离开
*===========================================================*/ void game_over()
{
cleardevice();
score();
setcolor(RED);
settextstyle(0,0,6);
outtextxy(100,200,"Game Over");
forum();
leave_or_again();
}
/*=========================================================== *自定义函数:leave_or_again()
*------------------------------
*离开还是重新游戏选择
*至于菜单条的设计与前面menu()类似
*===========================================================*/
void leave_or_again()
{
int j=300,k,key,tag=1;
while(tag!=0)
{
setcolor(BLUE);
settextstyle(0,0,3);
outtextxy(150,300,"Leave");
outtextxy(150,350,"Again");
setfillstyle(1,BLACK);
bar(330,k,355,k+25);
setfillstyle(2,YELLOW);
bar(330,j,355,j+25);
key=bioskey(0);
switch(key)
{
case DOWN: k=j;if(j<350) j+=50;break;
case UP: k=j;if(j>300) j-=50;break;
case ENTER: tag=0;break;
}
}
switch(j)
{
case 300:close_full_screen();
case 350:cleardevice();
Score=0;
menu();
wall_note();
play_game();
break;
}
}
*===========================================================
*自定义函数:close_full_screen()
*------------------------------
*关闭图形界面
*===========================================================*/ void close_full_screen()
{
cleardevice();
closegraph();
}
/*=========================================================== *主函数:
*------------------------------
*调用图形界面--->开始欢迎界面--->菜单界面
*--->画围墙--->游戏过程--->结束图形界面
*===========================================================*/ main()
{
make_full_screen();
start_screen();
menu();
wall_note();
play_game();
close_full_screen();
}
/*============================END============================*/。

相关文档
最新文档