C语言实现贪吃蛇游戏(命令行)_C 语言

来源:脚本之家  责任编辑:小易  

当然可以边游戏了,汉字可以用点阵显示,如果是windows c 的话显示汉字就更简单了!游戏窗体可以用windows c 编程实现!给你一个贪吃蛇的游戏吧,在tc 下编译成功!可以q聊:969370950includeincludeincludeincludedefine ESC 0x011bdefine LEFT 0x4b00define RIGHT 0x4d00define UP 0x4800define DOWN 0x5000define F5 0x3f00define S 0x1f73int score=0;unsigned level=0;void GAMEOVER(){char ch;while(1){clrscr();gotoxy(35,10);highvideo();cprintf("Game Over!\\n");cprintf("\\n\\n\\r You Score:%d",score);cprintf("\\n\\n\\r Press Q To Exit The Game!cprintf("\\n\\n\\r Press S To Start The Game Again!ch=getch();if(ch='q'|ch='Q')exit(0);if(ch='s'|ch='S'){DRAWGAME();GAMEPLAY();}}}int DRAWGAME(){int i;clrscr();textcolor(CYAN);for(i=1;i;i+){gotoxy(1,i);highvideo();cprintf("%c",5);gotoxy(60,i);highvideo();cprintf("%c",5);gotoxy(79,i);highvideo();cprintf("%c",5);}for(i=1;i;i+){gotoxy(i,1);highvideo();cprintf("%c",5);gotoxy(i,24);highvideo();cprintf("%c",5);}gotoxy(65,3);cprintf("Snake Game!gotoxy(63,7);cprintf("Made By ZhiPing");gotoxy(63,11);cprintf("QQ:1017257402");gotoxy(63,15);cprintf("Level:");gotoxy(63,18);cprintf("Score:");gotoxy(72,18);cprintf("%d",score);gotoxy(68,19);cprintf("%c",24);gotoxy(68,23);cprintf("%c",25);gotoxy(65,21);cprintf("%c",27);gotoxy(71,21);cprintf("%c",26);gotoxy(67,21);cprintf("Esc");}int GAMEPLAY(){int snakex[30],snakey[30],key,direction=4,food=0,foodx,foody,node=2,i;char ch;snakex[0]=2;snakex[1]=3;snakey[0]=12;snakey[1]=12;while(1){while!kbhit()){textcolor(YELLOW);if(food=0){randomize();foodx=rand()%59;foody=rand()%23;while(foodx)foodx+;while(foody)foody+;food=1;}for(i=0;i;i+){if(i>0)textcolor(i);gotoxy(snakex[i],snakey[i]);highvideo();cprintf("%c",2);textcolor(YELLOW);}delay(5000000000-level*1000);gotoxy(snakex[node-1],snakey[node-1]);cprintf("%c",32);gotoxy(foodx,foody);highvideo();cprintf("%c",3);for(i=node-1;i>0;i-){snakex[i]=snakex[i-1];snakey[i]=snakey[i-1];}if(direction=1)snakey[0]-;if(direction=2)snakey[0]+;if(direction=3)snakex[0]-;if(direction=4)snakex[0]+;for(i=4;i;i+)if(snakex[0]=snakex[i]&snakey[0]=snakey[i])GAMEOVER();if(snakex[0]=foodx&snakey[0]=foody){food=0;score+10;node+;if(node>30){level+;for(i=0;i;i+){gotoxy(snakex[i],snakey[i]);cprintf("%c",32);}node=2;}snakex[node-1]=snakex[node-2];snakey[node-1]=snakey[node-2];gotoxy(72,18);cprintf("%d",score);}gotoxy(72,15);highvideo();cprintf("%d",level);if(snakex[0]=1|snakex[0]=60){GAMEOVER();}if(snakey[0]=1|snakey[0]=24){GAMEOVER();}}key=bioskey(0);if(key=ESC){gotoxy(13,12);textcolor(GREEN);highvideo();cprintf("Are you sure to exit the game!(y/n?ch=getch();if(ch='y'|ch='Y')exit(0);else {gotoxy(13,12);cprintf("");}}if(key=LEFT&direction!4)direction=3;if(key=RIGHT&direction!3)direction=4;if(key=UP&direction!2)direction=1;if(key=DOWN&direction!1)direction=2;if(key=S){gotoxy(5,12);highvideo();cprintf("Please select the level you want to play(1~55):");scanf("%d",&level);gotoxy(5,12);cprintf("");}if(key=F5)getch();}}void GAMEHELP(){int i=1;textcolor(CYAN);highvideo();cprintf("Welcome To This Program,This Program is Called Snake Game!cprintf("\\n\\n\\rAnd As We Know This is A Very Old Game,But It Still Worths Playing!cprintf("\\n\\n\\rI Just Write It To Improve My Ability For The C Program!cprintf("\\n\\n\\rThe Operation Is As follows:");cprintf("\\n\\n\\rPress The Direction Key To control The Snake,And Press Esc To Exit The Game!cprintf("\\n\\n\\rAnd Press F5 To Pause The Game,And Anykey To Start!cprintf("\\n\\n\\rWhile You are playing the game,\\n\\n\\rYou can press S to select the game level you want to play!cprintf("\\n\\n\\rHope You Will Enjoy The Game!And If You Have Any New Ideas,\\n\\n\\rWilling To Hearing From You!Made By ZhiPing<QQ:969370950>");printf("\\n\\n\\rPress Anykey To Start The Game Now!Or after 30 seconds auto to run!while!kbhit()&i){gotoxy(40,23);textcolor(i);highvideo();cprintf("%d",i);sleep(1);i+;}}void main(){GAMEHELP();DRAWGAME();GAMEPLAY();}www.zgxue.com防采集请勿采集本网。

这是一个纯C语言写的贪吃蛇游戏,供大家参考,具体内容如下

基本思路: 蛇每吃一个食物蛇身子就增加一格,用UP,DOWN,LEFT,RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。参考资料来源:百度百科-C语言

#include<stdio.h>#include<stdlib.h>#include<windows.h>#include<time.h>#include<conio.h>#define SNAKE_LENGTH 100//定义蛇的最大长度#define SCREEN_WIDETH 80#define SCREEN_HEIGHT 30//定义每一节蛇的坐标struct coor{ int x; int y;};//枚举方向enum CH { right = VK_RIGHT, left = VK_LEFT, up = VK_UP, down = VK_DOWN};//定义蛇的属性struct snake{ int len;//当前蛇的长度 struct coor coord[SNAKE_LENGTH];//每一节蛇的坐标 enum CH CH;//定义蛇的方向 int SPEED; int flag;//定义蛇的状态 1表示存活 0表示死亡}snake;//光标移动函数void gotoxy(int x, int y){ COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);}//初始化游戏界面void init_sence(){ //初始化上下墙 for (int i = 0; i < SCREEN_WIDETH; i += 2) { gotoxy(i,0); printf("■"); gotoxy(i, SCREEN_HEIGHT); printf("■"); } //初始化左右墙 for (int i = 0; i <=SCREEN_HEIGHT; i++) { gotoxy(0, i); printf("■"); gotoxy(SCREEN_WIDETH,i); printf("■"); } //打印提示信息 gotoxy(SCREEN_WIDETH + 5, 2); printf("\t\t贪吃蛇"); gotoxy(SCREEN_WIDETH + 5, 6); printf("2018//12//1"); gotoxy(SCREEN_WIDETH + 5, 8); printf("作者:小豆芽"); gotoxy(SCREEN_WIDETH + 5, 10); printf("F1:加速\tF2:减速"); gotoxy(SCREEN_WIDETH + 5, 12); printf("CTRL:继续\t空格:暂停"); gotoxy(SCREEN_WIDETH + 5, 14); printf("ESC:退出游戏"); gotoxy(SCREEN_WIDETH + 5, 28); printf("建议:QQ:2862841130:::");}struct foodcoord { int x; int y; int flag;//定义食物的状态}food;//**这是c程序**#include"snake.h"//蛇的移动void move_snake();//画出蛇void draw_snake();//产生食物void creatfood();//判断蛇是否吃到食物void eatfood();//判断蛇是否死掉void SnakeState();int main(){ //设置窗口大小 system("mode con cols=110 lines=31"); //设置标题 SetConsoleTitleA("贪吃蛇"); //初始化蛇begin: snake.CH = VK_RIGHT;//初始化方向 snake.len = 5; //初始化长度 snake.SPEED = 300;//初始化蛇的移动速度 snake.coord[1].x = SCREEN_WIDETH / 2;//初始化蛇头的坐标 snake.coord[1].y = SCREEN_HEIGHT / 2; snake.coord[2].x = SCREEN_WIDETH / 2-2;//初始化蛇头的坐标 snake.coord[2].y = SCREEN_HEIGHT / 2; snake.coord[3].x = SCREEN_WIDETH / 2-4;//初始化蛇头的坐标 snake.coord[3].y = SCREEN_HEIGHT / 2; //初始化食物状态 food.flag = 1;//1表示吃到食物 0表示没有吃到食物 //初始化食物状态 snake.flag = 1;//1活 0死 init_sence();//初始化游戏界面 while (1) { draw_snake();//画蛇 Sleep(snake.SPEED);//蛇的移动速度 move_snake();//移动蛇 if(food.flag) creatfood();//产生食物 eatfood();//判断是否吃到食物 SnakeState();//判断蛇是否死亡 if (!snake.flag)break; } system("cls"); gotoxy(SCREEN_WIDETH/2, SCREEN_HEIGHT/2-4); printf(" GAME OVER!!!"); gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+2); printf("你的得分是:\t\t\t%d ",snake.len-1); gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+4); printf("我不服再来:\t\t\tCTRL "); gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+6); printf("算了垃圾游戏毁我青春:\t\tESC"); while (1) { if (GetAsyncKeyState(VK_CONTROL)) { system("cls"); goto begin; } else if (GetAsyncKeyState(VK_ESCAPE)) return 0; }}//蛇的移动void move_snake(){ //判断是否有按键操作 if (GetAsyncKeyState(up)) { if(snake.CH!=down)snake.CH = up; } else if (GetAsyncKeyState(down)) { if (snake.CH != up)snake.CH = down; } else if (GetAsyncKeyState(right)) { if (snake.CH != left)snake.CH = right; } else if (GetAsyncKeyState(left)) { if (snake.CH != right)snake.CH = left; } else if (GetAsyncKeyState(VK_F1)) { if(snake.SPEED>=100)snake.SPEED -= 50; } else if (GetAsyncKeyState(VK_F2)) { if (snake.SPEED <= 3000)snake.SPEED += 100; } //根据检测到的方向改变蛇头的位置 switch (snake.CH) { case right:snake.coord[1].x += 2; break; case left:snake.coord[1].x -= 2; break; case up:snake.coord[1].y -= 1; break; case down:snake.coord[1].y += 1; break; } }//画出蛇void draw_snake(){ //画出蛇头 gotoxy(snake.coord[1].x, snake.coord[1].y); printf("□"); //画出蛇身,直接一个for循环实现 for (int i = 2; i <= snake.len; i++) { gotoxy(snake.coord[i].x, snake.coord[i].y); printf("□"); } //擦掉尾巴 gotoxy(snake.coord[snake.len].x, snake.coord[snake.len].y); printf(" "); //遍历每一节蛇 for (int i = snake.len; i >1; i--) { snake.coord[i].x = snake.coord[i - 1].x; snake.coord[i].y = snake.coord[i - 1].y; } gotoxy(0, 0); printf("■"); gotoxy(85, 25); printf("得分:%d ", snake.len-1); }//产生食物void creatfood(){ //随机种子生成 srand((unsigned)time(NULL)); if(food.flag) while (1) { food.x = rand() % 80; food.y = rand() % 30; if (food.x % 2 == 0 && food.x >= 2 && food.x <= 78 && food.y > 1 && food.y < 30) { int flag = 0; //判断产生的食物可不可能在蛇的身体上 for (int i = 1; i <= snake.len; i++) { if (snake.coord[i].x == food.x&&snake.coord[i].y == food.y) { flag = 1; break; } } if (flag)continue; //绘制食物 else { gotoxy(food.x, food.y); printf("⊙"); food.flag = 0; break; } } } food.flag = 0;}//判断蛇是否吃到食物void eatfood(){ //只需要判断蛇头是否与食物重合 if (food.x == snake.coord[1].x&&food.y == snake.coord[1].y) { snake.len+=1; food.flag = 1; }}//判断蛇是否死掉void SnakeState(){ if (snake.coord[1].x < 2 || snake.coord[1].x>78 || snake.coord[1].y < 1 || snake.coord[1].y>29) snake.flag = 0; for (int i = 2; i <= snake.len; i++) { if (snake.coord[1].x == snake.coord[i].x&&snake.coord[1].y == snake.coord[i].y) snake.flag = 0; } }

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=50000;游戏速度自己调整*/ struct Food {

更多有趣的经典小游戏实现专题,分享给大家:

我这儿有c语言的自写俄罗斯方块,请指教:#include include include include include include include define ESC 0x011b define UP 0x4800 define DOWN 0x5000 define LEFT 0x4b00 define RIGHT 0x4d00

C++经典小游戏汇总

该类用于实现对蛇的操作控制,即蛇头方向的上下左右的移动操作。4 食物类 Food 该类是游戏过程中食物随机产生的控制和显示。5 判断死亡 Dead 该类是对游戏过程中判断玩家操作是否导致蛇的死亡,其中包括

python经典小游戏汇总

在大一下半学期,我们的C语言课设就让每个人编写一个小游戏,每个人被分配的游戏不同,难度比贪吃蛇差不多,稍简单有限(如四子棋、连连看)。用TC编写,DOS下运行,支持鼠标键盘。在学习VC时,让自己编写

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

#include <stdio.h>#include <graphics.h>#include <stdlib.h>#include <dos.h> /*引用的库函数2113*/#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011b/*宏定义键名*/#define N 200int i,key;int level;/*游戏等5261级*/int score=0;/*得分*/int gamespeed;/*游戏速度*/struct Food{int x;/*食物的横坐标4102*/int y;/*食物的纵坐标*/int yes;/*判断是否1653要出现食物的变量*/}food;/*食物的结构体*/struct Snake{int x[N];int y[N];int node;/*蛇的节数*/int direction;/*蛇移动方向*/int life;/* 蛇的生命,0活着,1死亡*/}snake;/*蛇的结构体*/void Choicelevle(void);/*选择游戏等级*/void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void DRAW(void);/*游戏区域*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*//*主函数*/void main(void){Init();/*图形驱动*/Choicelevle();/*选择游戏等级*/DRAW();/*游戏区域*/GamePlay();/*玩游戏具体过程*/Close();/*图形结束*/}/*图形驱动*/void Init(void){int gd=DETECT,gm;initgraph(&gd,&gm,"\\turboc2"); /*初始化图形系统*/cleardevice(); /*清除图形界面*/}/*选择游戏等级*/void Choicelevle(void){char name[20];setcolor(YELLOW);settextstyle(0,0,6);outtextxy(150,150,"Snake");setcolor(GREEN);settextstyle(0,0,1);outtextxy(200,250,"please put in your English name:");outtextxy(200,270,"Choice levle from 1-9.");outtextxy(300,320,"name:yangzilong");/*制作人姓名*/outtextxy(300,350,"number:0902060226");/*制作人学号*/outtextxy(300,380,"class:computer science 0602");/*制作人班级*/getch();printf("please putin your name:");gets(name);printf("please choice levle:");scanf("%d",&level);gamespeed=100000-400*level-300*level*level;if(level>9||level<1){cleardevice(); /*清除图形界面*/setcolor(YELLOW); /*设置字体颜色*/settextstyle(0,0,2); /*设置字体类型*/outtextxy(150,200,"level input error"); /*显示文本*/getch();level=1;}}void DRAW(void){cleardevice(); /*清屏*/setcolor(2);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/rectangle(45,45,465,325);}/*玩游戏具体过程*/void GamePlay(void){setcolor(5);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/randomize();/*随机数发生器*/food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/snake.life=0;/*活着*/snake.direction=1;/*方向往右*/snake.x[0]=320;snake.y[0]=240;/*蛇头*/snake.x[1]=330;snake.y[1]=240; /*蛇的第二节位置*/snake.node=3;/*节数*/PrScore();/*输出得分*/while(1)/*可以重复玩游戏,压ESC键结束*/{while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if(food.yes==1)/*需要出现新食物*/{food.x=rand()%360+70;food.y=rand()%250+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[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;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/{if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){GameOver();/*显示失败*/snake.life=1; /*蛇死*/break;}}/*如果蛇头碰到墙壁,蛇头从对面墙出来*/if(snake.x[0]<50){snake.x[0]=450;/*如果蛇头越过左边界,则从右边界进入*/snake.y[0]=snake.y[0];/*纵坐标不变*/for(i=snake.node-1;i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1]; /*蛇的其他节数向前推进*/}{setfillstyle(SOLID_FILL,0); /*设置填充模式和颜色,0表示黑色*/bar(50,55,455,315);/*bar是表示填充的范围的函数*/}}elseif(snake.x[0]>450){snake.x[0]=50;/*如果蛇头越过右边界,则蛇头从左边界进入*/snake.y[0]=snake.y[0];/*纵坐标不变*/for(i=snake.node-1;i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1]; /*蛇的其他节数向前推进*/}{setfillstyle(SOLID_FILL,0); /*设置填充模式和颜色,0表示黑色*/bar(50,55,455,315);/*bar是表示填充的范围的函数*/}}elseif(snake.y[0]<60){snake.y[0]=320;/*如果蛇头越过上边界,则从下边界进入*/snake.x[0]=snake.x[0];/*横坐标不变*/for(i=snake.node-1;i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1]; /*蛇的其他节数向前推进*/}{setfillstyle(SOLID_FILL,0); /*设置填充模式和颜色,0表示黑色*/bar(50,55,455,315);/*bar是表示填充的范围的函数*/}}elseif(snake.y[0]>320){snake.y[0]=60;/*如果蛇头越过下边界,则从上边界进入*/snake.x[0]=snake.x[0];/*横坐标不变*/for(i=snake.node-1;i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1]; /*蛇的其他节数向前推进*/}{setfillstyle(SOLID_FILL,0); /*设置填充模式和颜色,0表示黑色*/bar(50,55,455,315);/*bar是表示填充的范围的函数*/}}if(snake.life==1)/*如果蛇死就跳出内循环,重新开始*/break;if(snake.x[0]==food.x&&snake.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.node++;/*蛇的身体长一节*/food.yes=1;/*画面上需要出现新的食物*/score+=10; /*每吃掉一食物,得分累加10分*/if(score%100==0){level++;gamespeed=100000-400*level-300*level*level;/*每吃掉10食物提升一级,速度加快*/PrScore();/*输出新得分*/setcolor(YELLOW); /*设置字体颜色*/settextstyle(0,0,4); /*设置字体类型*/outtextxy(150,200,"LEVEL UP"); /*显示文本*/if(level==10){level=1,gamespeed=100000-400*level-300*level*level;}delay(6000000); delay(6000000); delay(6000000); delay(6000000); delay(6000000);delay(6000000); delay(6000000);bar(50,55,455,315);/*bar是表示填充的范围的函数*/}PrScore();/*输出新得分*/ } setcolor(4);/*画出蛇*/ for(i=0;i<snake.node;i++) rectangle(snake.x[i],snake.y[i],snake.x[i]+10, snake.y[i]-10); delay(gamespeed); /*控制游戏速度*/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); } /*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 str1[20];/*设置字符型数组*/ setfillstyle(SOLID_FILL,0); bar(50,15,390,35); /*填充矩形框*/setcolor(6); /*设置文本颜色*/settextstyle(0,0,2); /*设置数组显示位置*/sprintf(str1,"score %d level %d",score,level);/*显示数组内容*/outtextxy(55,20,str1); setcolor(YELLOW); /*设置字体颜色*/settextstyle(0,0,2); /*设置字体类型*/outtextxy(250,400,"EXIT=ESC ");/*显示文本*/} void Close(void) { closegraph(); },#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=50000; struct Food {int x; int y; int yes; }food; struct Snake {int x[N]; int y[N]; int node; int direction; int life; }snake; void Init(); void Close(); void DrawK(); void GamePlay(); void GameOver(); void PrScore(); void main() { Init(); DrawK(); GamePlay(); Close(); } void Init() {int gd=DETECT,gm; initgraph(&gd,&gm,"F:\\tuoboc2");/*此处为5261turboc的路径,读者可以根据自己的电脑4102而改*/ cleardevice(); } void DrawK() {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() {randomize(); food.yes=1; 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) {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[i]=snake.x[i-1]; snake.y[i]=snake.y[i-1]; } 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;i<snake.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|1653|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.x&&snake.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.node++; food.yes=1; score+=10; PrScore(); } setcolor(4); for(i=0;i<snake.node;i++) rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10); delay(gamespeed); 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(snake.life==1) break; key=bioskey(0); if(key==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; } } void GameOver() { cleardevice(); PrScore(); setcolor(RED); settextstyle(3,0,4); outtextxy(100,100,"Mengmeng,i love you!"); getch(); } void PrScore() {char str[10]; 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() { getch(); closegraph(); },给大家2113结束一个经典的小游戏---贪吃蛇,在这个5261游戏中,我们用到了很多基础的4102C语言知识,同1653时也用到了一些常用的数据结构,包括数组、结构体、枚举等。在图形显示的部分,我们会涉及到图片的显示,和简易的几何图形的绘制,另外还有键盘的控制,文字的显示等。还有背景音乐和相应的音效。废话不多说了,让我们先来看一下游戏的界面!游戏欢迎界面:游戏主界面:游戏结束界面:游戏界面:1、游戏欢迎界面。2、游戏主界面(游戏区、得分显示、游戏使用介绍)3、游戏结束界面游戏功能:1、可以使用上下左右键来控制蛇的运动方向2、当蛇吃到食物后,身体长度加一,得分加10分3、可以按空格键,进行游戏暂停和恢复。4、游戏结束后,可以按 ‘r’键,重新开始游戏。5、当游戏开始的时候有背景音乐播放,当吃到食物和游戏结束时播放相应的音乐。贪吃蛇游戏程序的主要模块和运行流程1、整体框架2、mainloop() 函数更多详情,请关注公众号:《肖申克之自由鸟》,点击底部菜单“游戏课”获得内容来自www.zgxue.com请勿采集。


  • 本文相关:
  • 贪吃蛇c语言代码实现(难度可选)
  • c语言贪吃蛇经典小游戏
  • c语言实现贪吃蛇游戏
  • c语言链表实现贪吃蛇游戏
  • c语言手把手教你实现贪吃蛇ai(上)
  • 70行c语言代码实现贪吃蛇
  • c语言结构数组实现贪吃蛇小游戏
  • c语言手把手教你实现贪吃蛇ai(下)
  • c语言手把手教你实现贪吃蛇ai(中)
  • c语言实现贪吃蛇游戏(单人版)
  • c语言实现两个递减数列中寻找某一个数
  • 常用排序算法整理分享(快速排序算法、希尔排序)
  • c语言中正切的相关函数总结
  • c++二叉树的几种遍历算法
  • c语言实现小猫钓鱼游戏
  • c++遍历lua table示例
  • c++开发之crc校验实例详解
  • 浅谈c++对象的内存分布和虚函数表
  • c++虚函数及虚函数表简析
  • c++链表倒序实现方法
  • 求用c语言编写的贪吃蛇程序
  • C语言可以编写贪吃蛇游戏吗?
  • 用C语言编写贪吃蛇游戏的程序
  • 用C语言编写贪吃蛇的游戏需要哪些方面的知识
  • c语言 贪吃蛇 程序
  • 贪吃蛇游戏的C语言代码
  • 关于用C语言编写的小游戏的游戏代码,如黑白棋贪吃蛇等
  • 在dos环境下c语言编程编一个贪吃蛇游戏
  • 我已经能用C语言编像贪吃蛇之类的游戏了,请问我的水平现在怎么样?
  • 贪吃蛇游戏和C语言的具体关系?
  • 网站首页网页制作脚本下载服务器操作系统网站运营平面设计媒体动画电脑基础硬件教程网络安全c#教程vbvb.netc 语言java编程delphijavaandroidiosswiftscala易语言汇编语言其它相关首页c 语言c++经典小游戏汇总python经典小游戏汇总python俄罗斯方块游戏集合javascript经典游戏 玩不停java经典小游戏汇总贪吃蛇c语言代码实现(难度可选)c语言贪吃蛇经典小游戏c语言实现贪吃蛇游戏c语言链表实现贪吃蛇游戏c语言手把手教你实现贪吃蛇ai(上)70行c语言代码实现贪吃蛇c语言结构数组实现贪吃蛇小游戏c语言手把手教你实现贪吃蛇ai(下)c语言手把手教你实现贪吃蛇ai(中)c语言实现贪吃蛇游戏(单人版)c语言实现两个递减数列中寻找某一个数常用排序算法整理分享(快速排序算法、希尔排序)c语言中正切的相关函数总结c++二叉树的几种遍历算法c语言实现小猫钓鱼游戏c++遍历lua table示例c++开发之crc校验实例详解浅谈c++对象的内存分布和虚函数表c++虚函数及虚函数表简析c++链表倒序实现方法c语言程序设计50例(经典收藏)c语言10个经典小程序c语言字符串操作总结大全(超详细c语言文件操作函数大全(超详细)c语言字符数组与字符串的使用详解c语言运算符优先级列表(超详细)c语言/c++中如何产生随机数c++中的string常用函数用法总结深入理解c++中public、protectedc++常用字符串分割方法实例汇总opencv实现绿幕视频背景替换功能错误:sem_union的存储大小未知问题的解决简单掌握c++编程中的while与do-while循环深入const int *p与int * const p的区别详c++ 数据结构完全二叉树的判断c++11新特性中auto 和 decltype 区别和联关于c++动态分配内存的介绍浅谈c语言编程中程序的一些基本的编写优化c语言二维数组几种常用的表示方法tcp socket客户端和服务端示例分享
    免责声明 - 关于我们 - 联系我们 - 广告联系 - 友情链接 - 帮助中心 - 频道导航
    Copyright © 2017 www.zgxue.com All Rights Reserved