C语言贪吃蛇文档

合集下载

C语言贪吃蛇程序设计说明书

C语言贪吃蛇程序设计说明书

C语言贪吃蛇程序设计说明书题目:贪吃蛇游戏学校: 系别: 专业班级: 姓名: 学号: 指导老师: 日期:一、设计题目:贪吃蛇是一款经典的休闲游戏,一条蛇在密闭的围墙内,随机出现一个食物,通过控制方向键操作小蛇不停的朝着食物前进,直到吃掉食物。

每吃一个食物,小蛇都会长长一截,随之难度增大;当小蛇头撞到墙或自己时,小蛇死亡。

二、功能设计:本游戏要求实现以下几个功能:(1) 用上、下、左、右键控制游戏区蛇的运动方向,使之吃食而使身体变长;(2) 用户可以调节蛇的运行速度来选择不同的难度;(3) 游戏分多个难度级别;(4) 用户可自选颜色;(5) 记录成绩前五名的游戏玩家;(6) 增加背景音乐;(7) 提高障碍物和游戏级别。

三、程序模块图:贪吃蛇游戏初画控设帮始图制置助模模模模化块块块块墙蛇食移食死变成等音体身物动物亡长绩级效2四、算法流程图:开始初始化界面和蛇身放置食物获取按键开始运动碰到边界是否否蛇吃到食是蛇长大蛇死亡是继续否结束3五、函数原型与功能 1.主函数:void main()启动程序,触动其他函数。

2.初始化:void init ()设置背景框大小、蛇体初始值,随机产生食物。

3.随机产生食物:void setfoodcrd()设置食物生成坐标,0表示食物被吃。

4.画食物:void showfood()用矩形框来画食物5.画蛇:void showsnake()根据蛇的坐标和节数,循环用矩形框来画蛇。

6.蛇移动:void snakemove() 根据按键,重设坐标7.改变蛇的方向:void changeskdir()响应用户的运动方向8.判断蛇是否死亡:void judgeslod判断蛇是否碰到自己或墙。

9.判断蛇是否吃到食物:void judgefood()判断是否吃到食物,吃食后变0,蛇增长一节。

10.结束游戏:void gameover()结束话语,并执行下一步。

六、基本代码#include<graphics.h> #include<conio.h>#include<stdio.h>#pragma comment(lib,"Winmm.lib")#include "MyTimer.h" #define SIZEMAX 100 /*蛇最大长度*/ #define SPEED 100 /*初始速度*/ #define len 20 /*蛇宽度*/#define lm 10 /*蛇每次移动距离*/ #define initlen 600 /*初始化窗口正方形的长度*/ #define Min_snakelen 2 /*蛇的最小长度*/typedef struct {int x,y;}DIR;int snakelen=Min_snakelen; /*蛇的长度*/4int isfood=1; /*食物状态*/ int isover=0; /*游戏状态*/int ispause=1; /*暂停状态*/int ismusic=1; /*音乐播放状态*/ char dir; /*记录蛇运动的方向*/ char c='d';DIR snake[500],food; /*定义蛇节点和食物的类型*/ int speed=SPEED;void drawmap() /*画地图函数*/ {IMAGE img;char str[10];loadimage(&img,"贪吃蛇.jpg"); /*游戏界面*/putimage(0,0,&img);loadimage(&img,"7.jpg"); /*侧栏提示*/putimage(600,0,&img);sprintf(str,"%d",snakelen);setfont(30,0,"宋体");setbkmode(TRANSPARENT);outtextxy(620,10,"操作说明:");setfont(20,0,"宋体");outtextxy(615,50,"awsd控制方向键");outtextxy(615,80,"p键暂停");outtextxy(615,110,"o键继续");outtextxy(615,200,"esc键退出");outtextxy(615,140,"l键暂停音乐");outtextxy(615,170,"k键继续播放");outtextxy(730,250,str);outtextxy(620,250,"蛇当前长度");}void init() /*初始化蛇函数*/ {int i;IMAGE img;snake[0].x=9*len+lm;snake[0].y=4*len+lm;loadimage(&img,"1.jpg");putimage(snake[0].x-lm,snake[0].y-lm,&img); for(i=1;i<snakelen;i++){snake[i].x=len*(9-i)+lm;snake[i].y=len*4+lm;5loadimage(&img, "2.jpg");putimage(snake[i].x-lm,snake[i].y-lm, &img); }}void showsnake() /*画蛇函数*/ {int i;IMAGE img;loadimage(&img, "1.jpg");putimage(snake[0].x-lm,snake[0].y-lm , &img);loadimage(&img, "2.jpg");for(i=1;i<snakelen;i++)putimage(snake[i].x-lm,snake[i].y-lm, &img); }void snakemove() /*画动蛇函数*/ {int i;int mx=snake[0].x;int my=snake[0].y;switch(dir){case 'a':mx-=len;break;case 'd':mx+=len;break;case 'w':my-=len;break;case 's':my+=len;break;default:break;}for(i=snakelen-1;i>=0;i--){snake[i+1].x=snake[i].x;snake[i+1].y=snake[i].y;}snake[0].x=mx;snake[0].y=my;showsnake();}int ceshiover() /*检测游戏结束函数*/ {int i;if(snake[0].x<0||snake[0].x>30*len-lm||snake[0].y<0||snake[0].y>30*len-lm)return 1;for(i=1;i<snakelen;i++)6{if(snake[0].x==snake[i].x&&snake[0].y==snake[i].y) return 1;}return 0;}int foodinsnake() /*检测食物是否在蛇上函数*/ {for(int i=0;i<snakelen;i++)if(food.x==snake[i].x&&food.y==snake[i].y)return 1;elsereturn 0;}void showfood() /*画食物函数*/{IMAGE img;do{food.x=(rand()%30)*len+lm;food.y=(rand()%30)*len+lm;}while(foodinsnake());loadimage(&img, "3.jpg");putimage(food.x-lm,food.y-lm , &img);isfood=0;}void kmusic(){if(ismusic==0)mciSendString("pause mymusic",NULL,0,NULL);if(ismusic==1)mciSendString("resume mymusic",NULL,0,NULL);}void playbkmusic() /*播放背景音乐函数*/{mciSendString("open 超级玛丽.mp3 alias mymusic", NULL, 0, NULL); mciSendString("play mymusic repeat", NULL, 0, NULL);}void playgame() /*玩游戏函数*/{c='d'; //蛇开始向右移动isover=0;7snakelen=Min_snakelen;dir='d';IMAGE img;MyTimer t; //定义精确延时对象int T=200; // 延长时间drawmap(); //画游戏地图init(); //画小蛇初始位置while(!isover){if(ispause){snakemove();FlushBatchDraw(); //批量绘图EndBatchDraw(); //结束批量绘图if(snake[0].x==food.x&&snake[0].y==food.y){ snakelen++;isfood=1;}if(isfood)showfood();if(snakelen<35)T=200-3*snakelen;t.Sleep(T);BeginBatchDraw(); // 开始批量绘图模式,防止闪烁问题drawmap();loadimage(&img, "3.jpg"); // 加载食物图片putimage(food.x-lm,food.y-lm , &img);};//按键控制if(kbhit())c=getch();switch(c){case 'a':if(dir!='d'){dir=c;}break;case 'd':if(dir!='a'){dir=c;}break;case 'w':if(dir!='s'){8dir=c;}break;case 's':if(dir!='w'){dir=c;}break;case 27: exit(0); break; //游戏退出case 'p': ispause=0;break; //p暂停case 'o': ispause=1;break; //o继续游戏case 'l': ismusic=0;break; //l暂停音乐case 'k': ismusic=1;break; //k继续播放default:break;}kmusic(); //音乐控制播放//判断游戏结束if(ceshiover())isover=1;//判断是否重新再来HWND wnd = GetHWnd(); //获取窗口句柄if(isover)if (MessageBox(wnd, "游戏结束。

C语言实现贪吃蛇游戏

C语言实现贪吃蛇游戏

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============================*/。

C语言项目案例之贪吃蛇

C语言项目案例之贪吃蛇

C语⾔项⽬案例之贪吃蛇项⽬案例:贪吃蛇下载链接:1. 初始化墙代码:// 初始化墙void init_wall(void){for (size_t y = 0; y <= HIGH; ++y){for (size_t x = 0; x <= WIDE; ++x){if (x == WIDE || y == HIGH) // 判断是否到墙{printf("=");}else{printf(" ");}}printf("\n");}}效果:2. 定义蛇和⾷物类型typedef struct{int x;int y;}FOOD; // ⾷物typedef struct{int x;int y;}BODY; // ⾝体typedef struct{int size; // ⾝体长度BODY body[WIDE*HIGH];}SNAKE; // 蛇3. 初始化蛇和⾷物// 定义⼀个蛇和⾷物SNAKE snake;FOOD food;// 初始化⾷物void init_food(void){food.x = rand() % WIDE; // 随机⽣成坐标food.y = rand() % HIGH;}// 初始化蛇void init_snake(void){snake.size = 2;// 将蛇头初始化到墙中间snake.body[0].x = WIDE / 2;snake.body[0].y = HIGH / 2;// 蛇⾝紧跟蛇头snake.body[1].x = WIDE / 2 - 1;snake.body[1].y = HIGH / 2;}4. 显⽰UI// 显⽰UIvoid showUI(void){// 显⽰⾷物// 存放光标位置COORD coord;coord.X = food.x;coord.Y = food.y;// 光标定位SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); putchar('$');// 显⽰蛇for (size_t i = 0; i < snake.size; ++i){// 设置光标coord.X = snake.body[i].x;coord.Y = snake.body[i].y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); if (i == 0)}else{putchar('#');}}}效果:最终代码// main.c#define _CRT_SECURE_NO_WARNINGS#include "./snakeGame.h"int main(void){// 取消光标CONSOLE_CURSOR_INFO cci;cci.bVisible = FALSE; // 取消光标cci.dwSize = sizeof(cci);SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cci); system("color 2");printf("欢迎来到贪吃蛇\n准备好了吗?按s/S开始,q/Q退出\n"); char ch = _getch();switch (ch){case 's':case 'S':system("color 0");system("cls");break;default:return 0;}init_wall();init_food();init_snake();showUI();playGame();return 0;}// snakeGame.c#include "./snakeGame.h"// 定义⼀个蛇和⾷物SNAKE snake;FOOD food;// ⽅向增量int dx = 0;int dy = 0;int lx, ly; // 尾节点// 初始化⾷物void init_food(void){food.x = rand() % WIDE; // 随机⽣成坐标food.y = rand() % HIGH;}// 初始化蛇void init_snake(void){snake.size = 2;snake.fraction = 0;// 将蛇头初始化到墙中间snake.body[0].x = WIDE / 2;snake.body[0].y = HIGH / 2;snake.body[1].x = WIDE / 2 - 1;snake.body[1].y = HIGH / 2;}// 初始化墙void init_wall(void){for (size_t y = 0; y <= HIGH; ++y){for (size_t x = 0; x <= WIDE; ++x){if (x == WIDE || y == HIGH) // 判断是否到墙{printf("=");}else{printf(" ");}}printf("\n");}printf("分数:0\n");}// 显⽰UIvoid showUI(void){// 显⽰⾷物// 存放光标位置COORD coord;coord.X = food.x;coord.Y = food.y;// 光标定位SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);putchar('$');// 显⽰蛇for (size_t i = 0; i < snake.size; ++i){// 设置光标coord.X = snake.body[i].x;coord.Y = snake.body[i].y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);if (i == 0){putchar('@');}else{putchar('#');}}// 处理尾节点coord.X = lx;coord.Y = ly;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);putchar(' ');coord.X = WIDE;coord.Y = HIGH;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);printf("\n分数:%d\n",snake.fraction);}void playGame(void){COORD _coord;system("color 7");char key = 'D';// 蛇不能撞墙while (snake.body[0].x >= 0 && snake.body[0].x <= WIDE && snake.body[0].y >= 0 && snake.body[0].y <= HIGH) {// 蛇不能撞⾃⼰for (size_t i = 1; i < snake.size; ++i){if (snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y){goto OVER;}}// 撞⾷物if (snake.body[0].x == food.x && snake.body[0].y == food.y){++snake.size;++snake.fraction;// 随机出现⾷物init_food();}// 控制蛇移动// 判断是否按下按键if (_kbhit()){key = _getch(); // 不需要敲回车,按下就⽴马确认}// 判断W A S D中哪个按键按下switch (key){case 'w':case 'W':dx = 0;dy = -1;break;case 'a':case 'A':dx = -1;dy = 0;break;case 's':case 'S':dx = 0;dy = 1;break;case 'd':dx = 1;dy = 0;break;case 'q':case 'Q':_coord.X = WIDE;_coord.Y = HIGH;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), _coord); putchar('\n');return;}// 蛇移动// 记录尾节点位置lx = snake.body[snake.size - 1].x;ly = snake.body[snake.size - 1].y;for (size_t i = snake.size - 1; i > 0; --i){snake.body[i].x = snake.body[i - 1].x;snake.body[i].y = snake.body[i - 1].y;}// 更新蛇头snake.body[0].x += dx;snake.body[0].y += dy;showUI();Sleep(500); // 延时}// 游戏结束OVER:system("color 4");_coord.X = 6;_coord.Y = HIGH + 1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), _coord); printf("\n游戏结束");printf("按r/R重新开始,按q/Q退出\n");char _key;_key = _getch();switch (_key){case 'r':case 'R':system("cls");init_wall();init_food();init_snake();showUI();playGame();case 'Q':case 'q':default:system("color 7");return;}}// snakeGame.h#pragma once#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <Windows.h>#define WIDE 60 // 长#define HIGH 20 // ⾼typedef struct{int x;int y;}FOOD; // ⾷物typedef struct{int x;int y;}BODY; // ⾝体typedef struct{int size; // ⾝体长度int fraction; // 分数BODY body[WIDE*HIGH];}SNAKE; // 蛇void init_wall(void);void init_food(void);void init_snake(void);void showUI(void);void playGame(void);。

C语言完整的贪吃蛇游戏1

C语言完整的贪吃蛇游戏1

共有两关,有记时器和记分器;按Enter键开局;在游戏过程中,按ESC键退出,按Enter键重新开局;#include <stdlib.h>#include <graphics.h>#include <bios.h>#include <dos.h>#include <conio.h>#define Enter 7181#define ESC 283#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#ifdef __cplusplus#define __CPPARGS ...#else#define __CPPARGS#endifvoid interrupt (*oldhandler)(__CPPARGS);void interrupt newhandler(__CPPARGS);void SetTimer(void interrupt (*IntProc)(__CPPARGS)); void KillTimer(void);void Initgra(void);void TheFirstBlock(void);void DrawMap(void);void Initsnake(void);void Initfood(void);void Snake_Headmv(void);void Flag(int,int,int,int);void GameOver(void);void Snake_Bodymv(void);void Snake_Bodyadd(void);void PrntScore(void);void Timer(void);void Win(void);void TheSecondBlock(void);void Food(void);void Dsnkorfd(int,int,int);void Delay(int);struct Snake{int x;int y;int color;}Snk[12];struct Food{int x;int y;int color;}Fd;int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse; char Sco[2],Time[6];void main(){ Initgra();SetTimer(newhandler);TheFirstBlock();while(1){DrawMap();Snake_Headmv();GameOver();Snake_Bodymv();Snake_Bodyadd();PrntScore();Timer();Win();if(key==ESC)break;if(key==Enter){cleardevice();TheFirstBlock();}TheSecondBlock();Food();Delay(Snkspeed);}closegraph();KillTimer();}void interrupt newhandler(__CPPARGS){TimerCounter++;oldhandler();}void SetTimer(void interrupt (*IntProc)(__CPPARGS)){oldhandler=getvect(0x1c);disable();setvect(0x1c,IntProc);enable();}void KillTimer(){disable();setvect(0x1c,oldhandler);enable();}void Initgra(){int gd=DETECT,gm;initgraph(&gd,&gm,"d:\\tc");}void TheFirstBlock(){setcolor(11);settextstyle(0,0,4);outtextxy(100,220,"The First Block");loop:key=bioskey(0);if(key==Enter){cleardevice();Initsnake();Initfood();Score=0;Hscore=1;Snkspeed=10;num=2;Times=0;key=0;TureorFalse=1;TimerCounter=0;Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0'; }else if(key==ESC) cleardevice();else goto loop;}void DrawMap(){line(10,10,470,10);line(470,10,470,470);line(470,470,10,470);line(10,470,10,10);line(480,20,620,20);line(620,20,620,460);line(620,460,480,460);line(480,460,480,20);}void Initsnake(){randomize();num=2;Snk[0].x=random(440);Snk[0].x=Snk[0].x-Snk[0].x%20+50;Snk[0].y=random(440);Snk[0].y=Snk[0].y-Snk[0].y%20+50;Snk[0].color=4;Snk[1].x=Snk[0].x;Snk[1].y=Snk[0].y+20;Snk[1].color=4;}void Initfood(){randomize();Fd.x=random(440);Fd.x=Fd.x-Fd.x%20+30;Fd.y=random(440);Fd.y=Fd.y-Fd.y%20+30;Fd.color=random(14)+1;}void Snake_Headmv(){if(bioskey(1)){key=bioskey(0);switch(key){case UP:Flag(1,0,0,0);break;case DOWN:Flag(0,1,0,0);break;case LEFT:Flag(0,0,1,0);break;case RIGHT:Flag(0,0,0,1);break default:break;}}if(flag1){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].y-=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color); }if(flag2){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].y+=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color); }if(flag3){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].x-=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}if(flag4){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].x+=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}}void Flag(int a,int b,int c,int d){flag1=a;flag2=b;flag3=c;flag4=d;}void GameOver(){int i;if(Snk[0].x<20||Snk[0].x>460||Snk[0].y<20||Snk[0].y>460) {cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop1:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}elseif(key==ESC)cleardevice();elsegoto loop1;}for(i=3;i<num;i++){if(Snk[0].x==Snk[i].x&&Snk[0].y==Snk[i].y) {cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop2:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}elseif(key==ESC)cleardevice();else goto loop2;}}}void Snake_Bodymv(){int i,s,t;for(i=1;i<num;i++){Dsnkorfd(checkx,checky,Snk[i].color); Dsnkorfd(Snk[i].x,Snk[i].y,0);s=Snk[i].x;t=Snk[i].y;Snk[i].x=checkx;Snk[i].y=checky;checkx=s;checky=t;}}void Food(){if(flag5){randomize();Fd.x=random(440);Fd.x=Fd.x-Fd.x%20+30;Fd.y=random(440);Fd.y=Fd.y-Fd.y%20+30;Fd.color=random(14)+1;flag5=0;}Dsnkorfd(Fd.x,Fd.y,Fd.color);}void Snake_Bodyadd(){if(Snk[0].x==Fd.x&&Snk[0].y==Fd.y) {if(Snk[num-1].x>Snk[num-2].x) {num++;Snk[num-1].x=Snk[num-2].x+20;Snk[num-1].y=Snk[num-2].y;Snk[num-1].color=Fd.color;}elseif(Snk[num-1].x<Snk[num-2].x) {num++;Snk[num-1].x=Snk[num-2].x-20;Snk[num-1].y=Snk[num-2].y;Snk[num-1].color=Fd.color;}elseif(Snk[num-1].y>Snk[num-2].y) {num++;Snk[num-1].x=Snk[num-2].x; Snk[num-1].y=Snk[num-2].y+20; Snk[num-1].color=Fd.color;}elseif(Snk[num-1].y<Snk[num-2].y) {num++;Snk[num-1].x=Snk[num-2].x; Snk[num-1].y=Snk[num-2].y-20; Snk[num-1].color=Fd.color;}flag5=1;Score++;}}void PrntScore(){if(Hscore!=Score){setcolor(11);settextstyle(0,0,3);outtextxy(490,100,"SCORE"); setcolor(2);setfillstyle(1,0);rectangle(520,140,580,180); floodfill(530,145,2);Sco[0]=(char)(Score+48);Sco[1]='\0';Hscore=Score;setcolor(4);settextstyle(0,0,3);outtextxy(540,150,Sco);}}void Timer(){if(TimerCounter>18){Time[4]=(char)(Time[4]-1);if(Time[4]<'0'){Time[4]='9';Time[3]=(char)(Time[3]-1);}if(Time[3]<'0'){Time[3]='5';Time[1]=(char)(Time[1]-1);}if(TureorFalse){setcolor(11);settextstyle(0,0,3);outtextxy(490,240,"TIMER");setcolor(2);setfillstyle(1,0);rectangle(490,280,610,320);floodfill(530,300,2);setcolor(11);settextstyle(0,0,3);outtextxy(495,290,Time);TureorFalse=0;}if(Time[1]=='0'&&Time[3]=='0'&&Time[4]=='0') {setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}else if(key==ESC) cleardevice();else goto loop;}TimerCounter=0;TureorFalse=1;}}void Win(){if(Score==3)Times++;if(Times==2){cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Y ou Win");loop:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();key=0;}else if(key==ESC) cleardevice();else goto loop;}}void TheSecondBlock(){if(Score==3){cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(100,220,"The Second Block"); loop:key=bioskey(0);if(key==Enter){cleardevice();Initsnake();Initfood();Score=0;Hscore=1;Snkspeed=8;num=2;key=0;}else if(key==ESC) cleardevice();else goto loop;}}void Dsnkorfd(int x,int y,int color) {setcolor(color);setfillstyle(1,color);circle(x,y,10);floodfill(x,y,color);}void Delay(int times){int i;for(i=1;i<=times;i++)delay(15000);}。

贪吃蛇游戏代码(C语言编写)

贪吃蛇游戏代码(C语言编写)

cleardevice(); if (!pause)
nextstatus(); else {
settextstyle(1,0,4); setcolor(12); outtextxy(250,100,"PAUSE"); } draw();
if(game==GAMEOVER) { settextstyle(0,0,6); setcolor(8); outtextxy(101,101,"GAME OVER"); setcolor(15); outtextxy(99,99,"GAME OVER"); setcolor(12); outtextxy(100,100,"GAME OVER"); sprintf(temp,"Last Count: %d",count); settextstyle(0,0,2); outtextxy(200,200,temp); }
place[tear].st = FALSE; tear ++; if (tear >= MAX) tear = 0; } else { eat = FALSE; exit = TRUE; while(exit) { babyx = rand()%MAXX; babyy = rand()%MAXY; exit = FALSE; for( i = 0; i< MAX; i++ ) if( (place[i].st)&&( place[i].x == babyx) && (place[i].y == babyy))
struct SPlace { int x; int y; int st; } place[MAX]; int speed; int count; int score; int control; int head; int tear; int x,y; int babyx,babyy; int class; int eat; int game; int gamedelay[]={5000,4000,3000,2000,1000,500,250,100}; int gamedelay2[]={1000,1};

c语言课程设计贪吃蛇设计

c语言课程设计贪吃蛇设计
物或蛇身
Part Three
C语言基础知识
数据类型和变量
基本数据类型:int、float、char、double等 复合数据类型:数组、结构体、指针等 变量声明:使用关键字"int"、"float"等声明变量 变量赋值:使用"="为变量赋值 变量作用域:局部变量、全局变量等 变量生命周期:从声明到释放的过程
结构体和联合体:包括结构 体定义、结构体初始化、结 构体访问、联合体定义、联 合体初始化、联合体访问等
函数和数组
函数:C语言中的基本单元,用于实现特定功能
数组:C语言中的基本数据类型,用于存储一组相同类型 的数据
数组函数:如strlen()、strcpy()等,用于操作字符串
指针:C语言中的重要概念,用于指向内存地址
Part Four
贪吃蛇游戏设计
游戏逻辑设计
游戏结束:当蛇碰到边界或 自己时结束
游戏循环:不断更新蛇的位 置和方向
游戏开始:初始化蛇的位置 和方向
得分计算:根据吃到的食物 数量计算得分
游戏难度:根据得分调整游 戏难度,如增加蛇的速度或
改变食物的位置
游戏界面:设计游戏界面, 包括蛇、食物、边界等元素
Part Seven
总结和展望
课程设计收获和体会
掌握了C语言的基本语法和编 程技巧
学会了如何设计并实现一个完 整的游戏项目
提高了解决问题的能力和团队 协作能力
对游戏开发有了更深入的了解 和兴趣
C语言在游戏开发中的应用前景
游戏开发中,C语言具有高效、稳定的特点,适合开发大型游戏。 C语言具有广泛的应用领域,可以开发各种类型的游戏,如动作、冒险、策略等。 C语言具有强大的社区支持,可以找到大量的游戏开发资源和教程。 C语言在游戏开发中具有广泛的应用前景,可以开发出更多优秀的游戏作品。

贪吃蛇开发文档

贪吃蛇开发文档

【名称】贪吃蛇开发文档【日期】2011-1-24 星期一1【需求文档】1.1 主界面1.2 菜单游戏菜单【开始】:重新开始。

【暂停、开始】:停止一下游戏,游戏开始。

【退出】:退出程序。

帮助菜单【关于】弹出关于对话框1.3 游戏界面【蛇】:蛇头黑色,蛇身白色,蛇每吃一次食物,蛇身就变长一格。

【食物】:黑色一点,每次被蛇吃掉,就在游戏界面上随机产生。

1.4 右边界面得分:每吃一个食物加一分。

游戏说明:上:W键下:S键左:A键右:D键暂停:空格键1.5 关于对话框2【全中文建模】2.1 类图1.蛇2.食物3.主窗口4.关于窗口5.得分2.2.1蛇2.2.2食物2.2.4主窗口2.2.5关于窗口2.3 序列图2.4 伪代码伪代码编写原理:你按照序列图去做伪代码,序列图怎么相互调用就怎么写函数。

2.4.1显示游戏流程主界面::显示(){蛇.得到坐标();食物.得到坐标();得分.得到坐标();}2.4.2蛇移动流程主界面::开始菜单(){键盘消息();}主界面::键盘消息(){蛇.移动();}蛇::移动(){食物.吃掉();得分.设置得分();}食物::吃掉(){产生();}2.4.3关于对话框流程主界面::关于菜单(){关于对话框.显示();}3【全英文建模】3.1 类图3.2 用例图3.3 序列图3.4 伪代码3.4.1显示游戏流程CMainDialog::Show(){CSnake.GetPoint();CFood.GetPoint();CScore.GetScore();}3.4.2蛇移动流程CMainDialog::StartMenu(){KeyboardMessage();}CMainDialog::KeyboardMessage() {CSnake.Move();}CSnake::Move(){CFood.Eat();CScore.SetScore();}CFood::Eat(){Make();}3.4.3关于对话框流程CMainDialog::AboutMenu(){CAboutDialog.Show();}4【真正代码】。

C语言贪吃蛇文档

C语言贪吃蛇文档

C语言贪吃蛇文档#includestdio.h#includebios.h#includeconio.h#includedos.h#includegraphics.h#includealloc.h#includestdlib.h#includetime.h#define LEFT 0x4B00#define RIGHT 0x4D00#define UP 0x4800#define DOWN 0x5000#define ESC 0x011B#define ENTER 0x1C0Dchar s1_title[]=Snake game;char s1_choose[3][11]={start game,author,exit};char s2_title[]=Snake game,made by HungryAnt!; char s2_fail[]=Game over!;char s2_win[]=You win;int s1_x=320-45;int s1_y=240-33;int s2_x=320-150;int s2_y=240-150;int s3_x=320-120;int s3_y=240-50;char map[30][30];空地0,蛇身1,食物2int length;int direction=2;上,下,左,右0,1,2,3int delay_time=20;延时,单位10毫秒int difficult=0;游戏难度int game_out=0;struct snake{蛇结构体char x;struct snake previous;前struct snake next;}head,tail;void initsnake(){蛇初始化struct snake sn;head=sn=(struct snake )malloc(sizeof(struct snake));蛇头sn-x=14;sn-y=15;map[15][14]=1;sn-previous=NULL;sn-next=(struct snake )malloc(sizeof(struct snake));sn-next-previous=sn;sn=sn-next;sn-x=15;sn-y=15;map[15][15]=1;sn-next=(struct snake )malloc(sizeof(struct snake));sn-next-previous=sn;sn=sn-next;sn-x=16;sn-y=15;map[15][16]=1;sn-next=NULL;tail=sn;蛇尾}void barbox(int x,int y,int color,int width,int height){填充一定范围的函数setfillstyle(SOLID_FILL,color);bar(x,y,x+width-1,y+height-1);}void box(int x,int y,int color){填充地图小方格函数barbox(x10+1,y10+1,color,9,9);}int choose(){在s1窗口里的选择框里进行选择int key,i=0,j;do{j=i;barbox(1,i(240-s1_y)23+1,LIGHTGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置浅灰色的选择条setcolor(BLUE);outtextxy(4,i(240-s1_y)23+9,s1_choose);while(bioskey(1)==0);key=bioskey(0);switch(key){case UPif(i0)j=i--;break;case DOWNif(i2)j=i++;break;case ESCexit(0);break;case ENTERreturn i;}if(j!=i){barbox(1,j(240-s1_y)23+1,DARKGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置浅灰色的选择条setcolor(WHITE);outtextxy(3,j(240-s1_y)23+8,s1_choose[j]);}}while(1);}void s1_window(){进入程序的第一个界面--中间的窗口int i=0;setviewport(s1_x,s1_y,640-s1_x-1,480-s1_y-1,0);setcolor(LIGHTBLUE);画两个连在一起的框架rectangle(-1,-26,(320-s1_x)2,-1); 标题部分rectangle(-1,-1,(320-s1_x)2,(240-s1_y)2+1);选择部分barbox(0,-25,BLUE,(320-s1_x)2,24);设置标题框架填充settextstyle(0,0,1);setcolor(WHITE);outtextxy(5,-17,s1_title);输出标题while(i3){barbox(1,i(240-s1_y)23+1,DARKGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置深灰色的条i++;}}void s2_window(){第二个界面--游戏界面int x,y;setviewport(s2_x,s2_y,640-s2_x-1,480-s2_y-1,0);clearviewport();setcolor(LIGHTBLUE);画两个连在一起的框架rectangle(-1,-30,(320-s2_x)2+1,-1); 分值框架barbox(0,-29,BLUE,(320-s2_x)2+1,28);设置分值框架填充settextstyle(0,0,1);setcolor(WHITE);outtextxy(5,-17,s2_title);输出标题setcolor(LIGHTGRAY);rectangle(-1,0,(320-s2_x)2+1,(240-s2_y)2+1);地图框架rectangle(0,0,(320-s2_x)2,(240-s2_y)2);for(y=0;y300;y+=10)地图绘制for(x=0;x300;x+=10)barbox(x+1,y+1,DARKGRAY,9,9);}void initmap(){初始化地图int x,y;for(y=0;y30;y++)for(x=0;x30;x++)map[y][x]=0;}void setfood(){随机产生一个食物int x,y;do{x=rand()%30;y=rand()%30;}while(map[y][x]==1);map[y][x]=2;box(x,y,YELLOW);}struct snake sn=head;box(sn-x,sn-y,LIGHTBLUE);sn=head-next;while(sn!=NULL){box(sn-x,sn-y,LIGHTGREEN);sn=sn-next;}}void cleartime(){时间设置为0struct time time_0;time_0.ti_min=0;time_0.ti_hour=0;time_0.ti_hund=0;time_0.ti_sec=0;settime(&time_0);}void freesnake(){释放蛇所占的内存struct snake sn,sn1;sn=head;head=NULL;while(sn!=NULL){free蛇所占的内存sn1=sn-next;free(sn);sn=sn1;}}void snakemove(){蛇移动struct snake sn;sn=head;head=(struct snake )malloc(sizeof(struct snake));蛇移动一格head-x=sn-x;head-y=sn-y;switch(direction){case 0head-y--;break;case 1head-y++;break;head-x--;break;case 3head-x++;break;}if(head-x0 head-x29 head-y0 head-y29 map[head-y][head-x]==1){如果蛇超出地图或者撞到自己,游戏失败freesnake();setviewport(0,0,639,479,0);settextstyle(0,0,3);setcolor(CYAN);outtextxy(205,210,s2_fail);显示game overgetch();clearviewport();清除屏幕内容game_out=1;return;}else{head-next=sn;sn-previous=head;head-previous=NULL;if(map[head-y][head-x]==2){如果遇到食物length++;长度增加if(length==30){freesnake();setviewport(0,0,639,479,0); settextstyle(0,0,3);setcolor(WHITE);outtextxy(206,211,s2_win);显示you win setcolor(LIGHTRED);outtextxy(205,210,s2_win);显示you win getch();clearviewport();清除屏幕内容game_out=1;return;}difficult=(length-3)3;delay_time=20-difficult;setfood();再添加一个食物}else{如果没有遇到食物则清除蛇尾map[tail-y][tail-x]=0;sn=tail;tail=tail-previous;tail-next=NULL;map[sn-y][sn-x]=0;box(sn-x,sn-y,DARKGRAY);free(sn);map[head-y][head-x]=1;}}}void gamestart(){游戏进行中int key;char l[4];struct time t_last;setfood();while(1){barbox((320-s2_x)2-28,-29,BLUE,27,28);覆盖上次蛇长sprintf(l,%d,length);setcolor(YELLOW);outtextxy((320-s2_x)2-28,-17,l);输出蛇长printsnake();cleartime();key=0;while(1){gettime(&t_last);if(t_last.ti_hunddelay_time)break;如果超过延时时间就退出if(bioskey(1)){key=bioskey(0);switch(key){case UPif(direction!=1)direction=0;break;case DOWNif(direction!=0)direction=1;break;case LEFTif(direction!=3)direction=2;break;if(direction!=2)direction=3;break;}break;}}snakemove();if(key==ESC){freesnake();game_out=1;setviewport(0,0,639,479,0);clearviewport();return;}if(game_out==1)return;}}void s2(){游戏length=3;direction=2;game_out=0;s2_window();界面initmap();地图初始化initsnake();蛇初始化gamestart();游戏开始if(game_out==1)return;}void s3_window(){char author[]=Author HungryAnt;char email[]=E-mail ljsunlin@/doc/1b13302096.html,;charblog[]=/doc/1b13302096.html,zhongji;char QQ[]=QQ517377100;setviewport(0,0,639,479,0);首先清屏clearviewport();setviewport(s3_x,s3_y,640-s3_x-1,480-s3_y-1,0); setcolor(LIGHTBLUE);rectangle(-1,-1,(320-s3_x)2,(240-s3_y)2);个人信息barbox(0,0,BLUE,(320-s3_x)2,(240-s3_y)2);设置标题框架填充settextstyle(0,0,1);输出作者信息setcolor(BLACK);outtextxy(6,35,email);outtextxy(6,60,blog);outtextxy(6,85,QQ);setcolor(WHITE);outtextxy(5,9,author);outtextxy(5,34,email);outtextxy(5,59,blog);outtextxy(5,84,QQ);getch();setviewport(0,0,639,479,0);清屏clearviewport();}void s3(){显示作者信息s3_window();}void s1(){int i;while(1){s1_window();i=choose();switch(i){case 0s2();游戏break;case 1s3();作者break;case 2exit(0);}}}void s_detectgraph(){自定义图形检测int gdriver,gmode,errorcode;gdriver=VGA;gmode=VGAHI;initgraph(&gdriver,&gmode,);if (errorcode !=0){printf(ntttGame Snake Gamen);printf(tttAuthor HungryAntn);printf(nterrort%sn, grapherrormsg(errorcode));getch();exit(1);}}int main(){srand((unsigned) time(NULL));设置随机数不同s_detectgraph(); 图形检测s1();getch();closegraph();}本文来自CSDN博客,转载请标明出处:/doc/1b13302096.html,/lilixiong08/archiv e/2008/01/16/2047086.aspx。

贪吃蛇c语言代码

贪吃蛇c语言代码

贪吃蛇c语言代码#include <graphics.h>#include <conio.h>#include <stdlib.h>#include <dos.h>#define NULL 0#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#define ESC 283#define ENTER 7181struct snake{int centerx;int centery;int newx;int newy;struct snake *next;};struct snake *head;int grade=60; /*控制速度的*******/int a,b; /* 背静遮的位置*/void *far1,*far2,*far3,*far4; /* 蛇身指针背静遮的指针虫子*/int size1,size2,size3,size4; /* **全局变量**/int ch=RIGHT; /**************存按键开始蛇的方向为RIGHT***********/int chy=RIGHT;int flag=0; /*********判断是否退出游戏**************/int control=4; /***********判断上次方向和下次方向不冲突***/int nextshow=1; /*******控制下次蛇身是否显示***************/int scenterx; /***************随即矩形中心坐标***************/int scentery;int sx; /*******在a b 未改变前得到他们的值保证随机矩形也不在此出现*******/int sy;/************************蛇身初始化**************************/void snakede(){struct snake *p1,*p2;head=p1=p2=(struct snake *)malloc(sizeof(struct snake));p1->centerx=80;p1->newx=80;p1->centery=58;p1->newy=58;p1=(struct snake *)malloc(sizeof(struct snake));p2->next=p1;p1->centerx=58;p1->newx=58;p1->centery=58;p1->newy=58;p1->next=NULL;}/*******************end*******************/void welcome() /*************游戏开始界面,可以选择速度**********/ {int key;int size;int x=240;int y=300;int f;void *buf;setfillstyle(SOLID_FILL,BLUE);bar(98,100,112,125);setfillstyle(SOLID_FILL,RED);bar(98,112,112,114);setfillstyle(SOLID_FILL,GREEN);bar(100,100,110,125);size=imagesize(98,100,112,125);buf=malloc(size);getimage(98,100,112,125,buf);cleardevice();setfillstyle(SOLID_FILL,BLUE);bar(240,300,390,325);outtextxy(193,310,"speed:");setfillstyle(SOLID_FILL,RED);bar(240,312,390,314);setcolor(YELLOW);outtextxy(240,330,"DOWN");outtextxy(390,330,"UP");outtextxy(240,360,"ENTER to start..." );outtextxy(270,200,"SNAKE");fei(220,220);feiyang(280,220);yang(340,220);putimage(x,y,buf,COPY_PUT);setcolor(RED);rectangle(170,190,410,410);while(1){ if(bioskey(1)) /********8选择速度部分************/ key=bioskey(0);switch(key){case ENTER:f=1;break;case DOWN:if(x>=240){ putimage(x-=2,y,buf,COPY_PUT);grade++;key=0;}case UP:if(x<=375){ putimage(x+=2,y,buf,COPY_PUT);grade--;key=0;break;}}if (f==1)break;} /********** end ****************/ free(buf);}/*************************随即矩形*****************//***********当nextshow 为1的时候才调用此函数**********/void ran(){ int nx;int ny;int show; /**********控制是否显示***********/int jump=0;struct snake *p;p=head;if(nextshow==1) /***********是否开始随机产生***************/{show=1;randomize();nx=random(14);ny=random(14);scenterx=nx*22+58;scentery=ny*22+58;while(p!=NULL){if(scenterx==p->centerx&&scentery==p->centery||scenterx==sx&&scentery==sy) {show=0;jump=1;break;}elsep=p->next;if(jump==1)break;}if(show==1){putimage(scenterx-11,scentery-11,far3,COPY_PUT);nextshow=0;break;}}}/***********过关动画**************/ void donghua(){ int i;cleardevice();setbkcolor(BLACK);randomize();while(1){for(i=0;i<=5;i++){putpixel(random(640),random(80),13); putpixel(random(640),random(80)+80,2); putpixel(random(640),random(80)+160,3); putpixel(random(640),random(80)+240,4); putpixel(random(640),random(80)+320,1); putpixel(random(640),random(80)+400,14); }setcolor(YELLOW);settextstyle(0,0,4);outtextxy(130,200,"Wonderful!!"); setfillstyle(SOLID_FILL,10);bar(240,398,375,420);feiyang(300,400);fei(250,400);yang(350,400);if(bioskey(1))if(bioskey(0)==ESC){flag=1;break;}}}/*************************end************************//***********************初始化图形系统*********************/ void init(){int a=DETECT,b;int i,j;initgraph(&a,&b,"");}/***************************end****************************/ /***画立体边框效果函数******/void tline(int x1,int y1,int x2,int y2,int white,int black){ setcolor(white);line(x1,y1,x2,y1);line(x1,y1,x1,y2);setcolor(black);line(x2,y1,x2,y2);line(x1,y2,x2,y2);}/****end*********//*************飞洋标志**********/int feiyang(int x,int y){int feiyang[18][18]={ {0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0,0}, {0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,0},{0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0},{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0},{0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0},{0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0},{0,0,1,1,1,1,1,0,0,1,0,0,1,1,0,0,0,0},{0,0,1,1,1,0,0,0,0,1,0,1,1,1,0,0,0,0},{0,0,1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0},{0,0,1,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},{0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,1,0,0},{0,0,1,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0},{0,0,1,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0},{0,0,0,1,1,1,0,1,1,1,1,1,0,0,1,0,0,0},{0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0},{0,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (feiyang[i][j]==1)putpixel(j+x,i+y,RED);}}/********"飞"字*************/int fei(int x,int y){int fei[18][18]={{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0},{0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (fei[i][j]==1)putpixel(j+x,i+y,BLUE);}}/*********"洋"字**************/int yang(int x,int y){int yang[18][18]={{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, {1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0},{0,1,1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0},{0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0},{0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0},{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0},{0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0},{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0},{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (yang[i][j]==1)putpixel(j+x,i+y,BLUE);}}/******************主场景**********************/ int bort(){ int a;setfillstyle(SOLID_FILL,15);bar(49,49,71,71);setfillstyle(SOLID_FILL,BLUE);bar(50,50,70,70);size1=imagesize(49,49,71,71);far1=(void *)malloc(size1);getimage(49,49,71,71,far1);cleardevice();setfillstyle(SOLID_FILL,12);bar(49,49,71,71);size2=imagesize(49,49,71,71);far2=(void *)malloc(size2);getimage(49,49,71,71,far2);setfillstyle(SOLID_FILL,12);bar(49,49,71,71);setfillstyle(SOLID_FILL,GREEN);bar(50,50,70,70);size3=imagesize(49,49,71,71);far3=(void *)malloc(size3);getimage(49,49,71,71,far3);cleardevice(); /*取蛇身节点背景节点虫子节点end*/ setbkcolor(8);setfillstyle(SOLID_FILL,GREEN);bar(21,23,600,450);tline(21,23,600,450,15,8); /***开始游戏场景边框立体效果*******/ tline(23,25,598,448,15,8);tline(45,45,379,379,8,15);tline(43,43,381,381,8,15);tline(390,43,580,430,8,15);tline(392,45,578,428,8,15);tline(412,65,462,85,15,8);tline(410,63,464,87,15,8);tline(410,92,555,390,15,8);tline(412,94,553,388,15,8);tline(431,397,540,420,15,8);tline(429,395,542,422,15,8);tline(46,386,377,428,8,15);tline(44,384,379,430,8,15);setcolor(8);outtextxy(429,109,"press ENTER ");outtextxy(429,129,"---to start"); /*键盘控制说明*/outtextxy(429,169,"press ESC ");outtextxy(429,189,"---to quiet");outtextxy(469,249,"UP");outtextxy(429,289,"LEFT");outtextxy(465,329,"DOWN");outtextxy(509,289,"RIGHT");setcolor(15);outtextxy(425,105,"press ENTER ");outtextxy(425,125,"---to start");outtextxy(425,165,"press ESC ");outtextxy(425,185,"---to quiet");outtextxy(465,245,"UP");outtextxy(425,285,"LEFT");outtextxy(461,325,"DOWN");outtextxy(505,285,"RIGHT"); /*******end*************/ setcolor(8);outtextxy(411,52,"score");outtextxy(514,52,"left");setcolor(15);outtextxy(407,48,"score");outtextxy(510,48,"left");size4=imagesize(409,62,465,88); /****分数框放到内存********/ far4=(void *)malloc(size4);getimage(409,62,465,88,far4);putimage(500,62,far4,COPY_PUT); /*******输出生命框***********/setfillstyle(SOLID_FILL,12);setcolor(RED);outtextxy(415,70,"0"); /***************输入分数为零**********/outtextxy(512,70,"20"); /*************显示还要吃的虫子的数目*********/ bar(46,46,378,378);feiyang(475,400);fei(450,400);yang(500,400);outtextxy(58,390,"mailto:");outtextxy(58,410,"snake game");outtextxy(200,410,"made by yefeng");while(1){ if(bioskey(1))a=bioskey(0);if(a==ENTER)break;}}/******************gameover()******************/void gameover(){ char *p="GAME OVER";int cha;setcolor(YELLOW);settextstyle(0,0,6);outtextxy(100,200,p);while(1){if(bioskey(1))cha=bioskey(0);if(cha==ESC){flag=1;break;}}}/***********显示蛇身**********************/void snakepaint(){struct snake *p1;p1=head;putimage(a-11,b-11,far2,COPY_PUT);while(p1!=NULL){putimage(p1->newx-11,p1->newy-11,far1,COPY_PUT);p1=p1->next;}}/****************end**********************//*********************蛇身刷新变化游戏关键部分*******************/ void snakechange(){struct snake *p1,*p2,*p3,*p4,*p5;int i,j;static int n=0;static int score;static int left=20;char sscore[5];char sleft[1];p2=p1=head;while(p1!=NULL){ p1=p1->next;if(p1->next==NULL){a=p1->newx;b=p1->newy; /************记录最后节点的坐标************/ sx=a;sy=b;}p1->newx=p2->centerx;p1->newy=p2->centery;p2=p1;}p1=head;while(p1!=NULL){p1->centerx=p1->newx;p1->centery=p1->newy;p1=p1->next;}/********判断按键方向*******/if(bioskey(1)){ ch=bioskey(0);if(ch!=RIGHT&&ch!=LEFT&&ch!=UP&&ch!=DOWN&&ch!=ESC) /********chy为上一次的方向*********/ch=chy;}switch(ch){case LEFT: if(control!=4){head->newx=head->newx-22;head->centerx=head->newx;control=2;if(head->newx<47)gameover();}else{ head->newx=head->newx+22;head->centerx=head->newx;control=4;if(head->newx>377)gameover();}chy=ch;break;case DOWN:if(control!=1){ head->newy=head->newy+22;head->centery=head->newy; control=3;if(head->newy>377)gameover();}else{ head->newy=head->newy-22; head->centery=head->newy;control=1;if(head->newy<47)gameover();}chy=ch;break;case RIGHT: if(control!=2){ head->newx=head->newx+22;head->centerx=head->newx;control=4;if(head->newx>377)gameover();}else{ head->newx=head->newx-22;head->centerx=head->newx;control=2;if(head->newx<47)gameover();}chy=ch;break;case UP: if(control!=3){ head->newy=head->newy-22;head->centery=head->newy;control=1;if(head->newy<47)gameover();}else{ head->newy=head->newy+22;head->centery=head->newy;control=3;if(head->newy>377)gameover();}chy=ch;break;case ESC:flag=1;break;}/* if 判断是否吃蛇*/if(flag!=1){ if(head->newx==scenterx&&head->newy==scentery){ p3=head;while(p3!=NULL){ p4=p3;p3=p3->next;}p3=(struct snake *)malloc(sizeof(struct snake));p4->next=p3;p3->centerx=a;p3->newx=a;p3->centery=b;p3->newy=b;p3->next=NULL;a=500;b=500;putimage(409,62,far4,COPY_PUT); /********** 分数框挡住**************/ putimage(500,62,far4,COPY_PUT); /*********把以前的剩下虫子的框挡住********/ score=(++n)*100;left--;itoa(score,sscore,10);itoa(left,sleft,10);setcolor(RED);outtextxy(415,70,sscore);outtextxy(512,70,sleft);nextshow=1;if(left==0) /************判断是否过关**********/donghua(); /*******如果过关,播放过关动画*********************/}p5=head; /*********************判断是否自杀***************************/ p5=p5->next;p5=p5->next;p5=p5->next;p5=p5->next; /****从第五个节点判断是否自杀************/while(p5!=NULL){if(head->newx==p5->centerx&&head->newy==p5->centery){ gameover();break;}elsep5=p5->next;}}}/************snakechange()函数结束*******************//*****************************主函数******************************************/ int main(){ int i;init(); /**********初始化图形系统**********/ welcome(); /*********8欢迎界面**************/ bort(); /*********主场景***************/ snakede(); /**********连表初始化**********/ while(1){ snakechange();if(flag==1)break;snakepaint();ran();for(i=0;i<=grade;i++)delay(3000);}free(far1);free(far2);free(far3);free(far4);closegraph();return 0;}。

贪吃蛇c语言精简版

贪吃蛇c语言精简版

贪吃蛇c语言精简版//贪吃蛇#include<stdio.h>#include <windows.h>#include <stdlib.h>#include <time.h>#include <conio.h>struct all_xy{POINT point;struct all_xy *next;};int x=2,y=0,key,i,found_time;POINT save_point,save_point2,food_xy={20,10};BOOL end_self=FALSE,flag;struct all_xy *head=NULL,*node1,*node2;void gotoxy(int x, int y){COORD coord;coord.X = x,coord.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);}void snake_add(){for(node2=head;head!=NULL&&node2->next!=NULL;node2=node2->next);node1=(struct all_xy *)malloc(sizeof(struct all_xy));node1->point=(head==NULL?food_xy:node2->point);node1->next=NULL;if(head==NULL){head=node1;return;}else{for(node2=head;node2->next!=NULL;node2=node2->next);node2->next=node1;} }void make_food(long *x,long *y){for(found_time=0;found_time<500;found_time++){*x=(rand()%38+1)*2,*y=rand()%23+1;for(node1=head,flag=FALSE;node1!=NULL;node1=node1->next)if(*x==node1->point.x&&*y==node1->point.y){flag=TRUE;break;}if(!flag){gotoxy(*x,*y);printf("○");return;}}MessageBox(NULL,"已找不到空点放食物了,程序结束!","你太牛了!",MB_ICONASTERISK);exit(0);}int main(){for(i=0;i<3;i++)snake_add();for(srand(time(NULL)),make_food(&food_xy.x,&food_xy.y);;Sleep(400)){if(kbhit()){if((key=getch())==224)key=getch();switch(key){case 80:y!=-1?(x=0,y=1):printf("\a");break;case 72:y!=1?(x=0,y=-1):printf("\a");break;case 75:x!=2?(x=-2,y=0):printf("\a");break;case 77:x!=-2?(x=2,y=0):printf("\a");break;}}node2=head,node1=node2->next,save_point=node1->point;node1->point=node2->point,node1=node1->next;head->point.x+=x,head->point.y+=y;for(node2=head->next;node2!=NULL;node2=node2->next)//依次检查是否自己撞到自己if(node2->point.x==head->point.x&&node2->point.y==head->point.y)//如果发现头结点和任意一个结点的X Y相同则设置end_self的值并跳出{end_self=TRUE;break;}if(head->point.x<0||head->point.x>=78||head->point.y<0||head->point.y>=25| |end_self==TRUE){gotoxy(32,5);printf("游戏结束!");getch();return 0;}for(;node1!=NULL;node1=node1->next){save_point2=node1->point,node1->point=save_point;node2=node1,save_point=save_point2;}gotoxy(save_point.x,save_point.y);printf(" ");if(head->point.x==food_xy.x&&head->point.y==food_xy.y)snake_add(),make_food(&food_xy.x,&food_xy.y);for(node1=head;node1!=NULL;node1=node1->next)gotoxy(node1->point.x,node1->point.y),node1==head?printf("⊙"):printf("□");}}。

贪吃蛇文本文档

贪吃蛇文本文档
POINT2D myFood = {0};
POINT2D myReward={0};
POINT2D myBar[BARS] = {{0}};
int snakeLength[2] = {0,0};
int snakeDir[2] = {DIR_RIGHT,DIR_RIGHT};
int isFood = 0;
printf("Player 1");
{
drawBlock(myReward.x, myReward.y, BS_SPACE); //超出规定时间则将奖励食物用背景方框覆盖
myReward.x=-1;
myReward.y=-1;
isReward=0;
#pragma comment(lib,"libpcc32.a")
#pragma comment(lib,"libjkey32.a")
// 定义地图的尺寸及坐标(均使用双字符长度)
#define MAP_WIDTH 24
#define MAP_HEIGHT 16
#define MAP_BASE_X 8
}
else if(ch=='2')
{
delayMS(gamespeed);
}
}
//主函数
int main()
{ clock_t start,finish;
int isPause = 1;
int chance=0;
float RewardTime=0;
int z;
int z;
char ch2;
char ch;

C语言贪吃蛇游戏需求分析说明书

C语言贪吃蛇游戏需求分析说明书

项目需求规格说明书当前版本号:1.0最后更新日期:作者:目录1.引言 (3)1.1. 目的 (3)1.2. 背景 (3)1.3. 参考资料 (3)1.4. 开发人员 (3)2. 任务概述 (3)2.1目标 (3)2.2 假定与约束 (4)3.游戏总体需求说明 (5)3.1. 功能需求 (5)3.2. 性能需求 (5)3.3 界面需求 (6)3.4 运行环境需求 (6)1.引言1.1.目的本文档分析和说明贪吃蛇游戏的总体需求①:展示游戏的结果,以及游戏的价值;②:总结反思,为后续游戏做的更好进行复盘。

③:明确游戏的功能、性能、界面等方面的需求,为游戏的开发和设计提供指导;1.2.背景项目委托单位:开发单位:主管部门:1.3.参考资料1、贪吃蛇游戏现有版本2、相关书籍与文档3、网络上的相关资源和资料4、严蔚敏. 数据结构(c语言版). 清华大学出版. 2007年.5、谭浩强. C程序设计(第4版). 清华大学出版社. 2010年6月.1.4.开发人员2. 任务概述2.1目标①开发的意图:为了提供一款经典有趣的游戏,让玩家可以通过控制蛇的移动来获取食物并不断增加长度,增强反应能力和策略思维。

②应用目标:主要是娱乐和休闲。

通过游戏,玩家可以在放松身心的同时享受游戏带来的挑战和成就感。

③作用范围:贪吃蛇游戏适用于各种游戏平台,包括电脑、手机、平板等。

它可以在个人设备上单人游戏,也可以通过网络连接与其他玩家进行多人对战。

④背景资料:贪吃蛇游戏最早出现于20世纪70年代末期,是一款经典的电子游戏。

最初的贪吃蛇游戏是在黑白屏幕上展示,玩家通过操纵一个小蛇吃食物,并随着吃食物的数量增加而变长。

随着游戏的发展,贪吃蛇游戏在不同的平台上得到了广泛的推广和发展,衍生出了各种版本和变种。

如今,贪吃蛇游戏仍然受到许多玩家的喜爱,并成为了经典的游戏之一。

⑤软件系统与其它系统的关系:贪吃蛇软件系统通常是一个独立的游戏系统,与其他系统没有直接的依赖关系。

贪吃蛇游戏开发文档最终

贪吃蛇游戏开发文档最终

贪吃蛇游戏开发文档本文档为稻草人于暑假开发C语言版本之链表学习的贪吃蛇游戏文档!一:游戏分为两个大模块:一:界面开发(UI):1:菜单界面;2:游戏界面;3:积分板块;二:游戏开发:1:界面的控制;2 : 成长的规则;(1300升中级,2500升高级)3:碰撞检测//本游戏采用全局使用键盘控制,以及使用了多线程,所以使用了WINDOWS API函数,//运行系统:Windos/7/8/10/XP二:本项目所用到的头文件:1:库头文件:#include<stdio.h>#include<windows.h>#include<conio.h>#include<time.h>2:自写头文件:#include"tools.h"#include"Snake.h"三:头文件介绍:Snake.h头文件为:游戏的核心文件,里面包含了游戏界面的绘制函数,游戏的成长机制,游戏的碰撞检测等。

Snake.h;typedef struct Snaked{int x;int y;//蛇身体的坐标char Content[3];Snaked *pNext;//指向下一个结构体Snaked *pHead;//指向上一个结构体}Snakedd;//定义蛇身体的结构体void GameWord(void);//绘制游戏界面void ConsoleControl(void);//控制台显示格式控制void MenuControl(void);//菜单控制中心Snakedd *ProduceSnake(int iCnt); //产生蛇的身体,iCnt代表的是蛇的身体有几节。

void PrintList(Snakedd *pHead);//打印出链表(蛇身)void DeleteList(Snakedd *pHead);//销毁链表void RandomCoordinate();//产生一个随机坐标,并且打印出食物//多线程函数:DWORD WINAPI MobileSnake(LPVOID lpChandle); //移动蛇(未实现)DWORD WINAPI KeyboardControl(LPVOID lpChandle);//键盘控制。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
case UP if(direction!=1)direction=0; break;
case DOWN if(direction!=0)direction=1; break;
case LEFT if(direction!=3)direction=2; break;
outtextxy(3,i(240-s1_y)23+8,s1_choose);
while(bioskey(1)==0); key=bioskey(0); switch(key){
case UP if(i0)j=i--; break;
case DOWN if(i2)j=i++; b
int delay_time=20;延时,单位 10 毫秒
int difficult=0;游戏难度
int game_out=0;
struct snake{蛇结构体 char x; char y;
struct snake previous;前 struct snake next; }head,tail;
barbox(0,-25,BLUE,(320-s1_x)2,24);设置标题框架填充
settextstyle(0,0,1); setcolor(WHITE); outtextxy(5,-17,s1_title);输出标题
while(i3){ barbox(1,i(240-s1_y)23+1,DARKGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置深灰色的条
#includestdio.h #includebios.h #includeconio.h #includedos.h #includegraphics.h #includealloc.h #includestdlib.h #includetime.h
#define LEFT 0x4B00 #define RIGHT 0x4D00 #define UP 0x4800 #define DOWN 0x5000 #define ESC 0x011B #define ENTER 0x1C0D
void s1_window(){进入程序的第一个界面--中间的窗口 int i=0; setviewport(s1_x,s1_y,640-s1_x-1,480-s1_y-1,0);
setcolor(LIGHTBLUE);画两个连在一起的框架 rectangle(-1,-26,(320-s1_x)2,-1); 标题部分 rectangle(-1,-1,(320-s1_x)2,(240-s1_y)2+1);选择部分
printsnake(); cleartime(); key=0; while(1){
gettime(&t_last); if(t_last.ti_hunddelay_time)break;如果超过延时时间就退出 if(bioskey(1)){
key=bioskey(0); switch(key){
setcolor(LIGHTBLUE);画两个连在一起的框架 rectangle(-1,-30,(320-s2_x)2+1,-1); 分值框架 barbox(0,-29,BLUE,(320-s2_x)2+1,28);设置分值框架填充
settextstyle(0,0,1); setcolor(WHITE); outtextxy(5,-17,s2_title);输出标题
if(map[head-y][head-x]==2){如果遇到食物 length++;长度增加 if(length==30){ freesnake(); setviewport(0,0,639,479,0); settextstyle(0,0,3); setcolor(WHITE); outtextxy(206,211,s2_win);显示 you win setcolor(LIGHTRED); outtextxy(205,210,s2_win);显示 you win getch(); clearviewport();清除屏幕内容 game_out=1; return;
head=(struct snake )malloc(sizeof(struct snake));蛇移动一格
head-x=sn-x; head-y=sn-y; switch(direction){
case 0 head-y--; break;
case 1 head-y++; break;
case 2
case ENTER return i;
}
if(j!=i){ barbox(1,j(240-s1_y)23+1,DARKGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置浅灰色的选择条
setcolor(WHITE); outtextxy(3,j(240-s1_y)23+8,s1_choose[j]); } }while(1); }
}
void freesnake(){释放蛇所占的内存 struct snake sn,sn1; sn=head; head=NULL; while(sn!=NULL){free 蛇所占的内存 sn1=sn-next; free(sn); sn=sn1; }
}
void snakemove(){蛇移动 struct snake sn; sn=head;
void gamestart(){游戏进行中 int key; char l[4]; struct time t_last;
setfood(); while(1){
barbox((320-s2_x)2-28,-29,BLUE,27,28);覆盖上次蛇长 sprintf(l,%d,length); setcolor(YELLOW); outtextxy((320-s2_x)2-28,-17,l);输出蛇长
settextstyle(0,0,3); setcolor(CYAN); outtextxy(205,210,s2_fail);显示 game over getch(); clearviewport();清除屏幕内容 game_out=1; return; } else{ head-next=sn; sn-previous=head; head-previous=NULL;
head-x--; break; case 3 head-x++; break; }
if(head-x0 head-x29 游戏失败
freesnake();
head-y0
head-y29
map[head-y][head-x]==1){如果蛇超出地图或者撞到自己,
setviewport(0,0,639,479,0);
} difficult=(length-3)3; delay_time=20-difficult;
map[head-y][head-x]=1;
setfood();再添加一个食物 } else{如果没有遇到食物则清除蛇尾
map[tail-y][tail-x]=0; sn=tail; tail=tail-previous; tail-next=NULL; map[sn-y][sn-x]=0; box(sn-x,sn-y,DARKGRAY); free(sn); map[head-y][head-x]=1; } } }
}
void barbox(int x,int y,int color,int width,int height){填充一定范围的函数 setfillstyle(SOLID_FILL,color); bar(x,y,x+width-1,y+height-1);
}
void box(int x,int y,int color){填充地图小方格函数 barbox(x10+1,y10+1,color,9,9);
setcolor(WHITE);
outtextxy(3,i(240-s1_y)23+8,s1_choose); i++; }
}
void s2_window(){第二个界面--游戏界面 int x,y; setviewport(s2_x,s2_y,640-s2_x-1,480-s2_y-1,0); clearviewport();
}
void initmap(){初始化地图 int x,y; for(y=0;y30;y++) for(x=0;x30;x++) map[y][x]=0;
}
void setfood(){随机产生一个食物 int x,y; do{ x=rand()%30; y=rand()%30; }while(map[y][x]==1); map[y][x]=2; box(x,y,YELLOW);
}
int choose(){在 s1 窗口里的选择框里进行选择 int key,i=0,j;
do{ j=i; barbox(1,i(240-s1_y)23+1,LIGHTGRAY,(320-s1_x)2-2,(240-s1_y)23-1);设置浅灰色的选择条 setcolor(BLUE); outtextxy(4,i(240-s1_y)23+9,s1_choose); setcolor(WHITE);
setcolor(LIGHTGRAY); rectangle(-1,0,(320-s2_x)2+1,(240-s2_y)2+1);地图框架 rectangle(0,0,(320-s2_x)2,(240-s2_y)2);
for(y=0;y300;y+=10)地图绘制 for(x=0;x300;x+=10) barbox(x+1,y+1,DARKGRAY,9,9);
}
void printsnake(){显示出蛇
相关文档
最新文档