贪吃蛇c语言代码--CharlesZhu(无注释).docx_第1页
贪吃蛇c语言代码--CharlesZhu(无注释).docx_第2页
贪吃蛇c语言代码--CharlesZhu(无注释).docx_第3页
贪吃蛇c语言代码--CharlesZhu(无注释).docx_第4页
贪吃蛇c语言代码--CharlesZhu(无注释).docx_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

版本1.0#define E 200#include /*图形头文件*/#include /*输入输出头文件*/#include /*函数头文件*/#include /*包含很多BIOS DOS 调用函数*/#include #include #include /*计算机的每个按键都有一个编码,左按键的编码是0x4b00。0x4b00是一个16进制的数方便计算机读取。当你按下左按键时,会将按键编号0x4b00发送给程序,程序就可以根据这个编号判断你按下了哪个按键。#define LEFT 0x4b00的意思是,用LEFT代替0x4b00,计算机知道LEFT就是0x4b00。这样在编程时候,你只需要记住并使用LEFT即可,而不需要记住0x4b00。这种方式既节约了你的工作量,还使得程序代码更容易理解。*/#define LEFT 0x4b00 /*0x4b00是bios中对键盘按键的编码,代表了左箭头的按键编码*/#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011b#define ENTER 0x1c0d#define P 0x1970#define Y 0x1579#define N 0x316eint i,key,speed;int score=0;int sleepspeed=1,speedmode=0,speedlevel=0;double delayspeed=30000;char playname20;struct Food int x; int y; int yes;food;struct Snake int xE; int yE; int node; int direction; int life;snake;typedef struct player char name20; int grade; int speed;PL;typedef struct node struct player data; struct node *next;Node,*Link;void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void Greeting(void); /*欢迎界面*/void Player(void);/*玩家信息管理模块*/void SelectSpeed(void); /*等级模块*/void DrawK(void);/*开始画面*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*/void ContinueRequest(void);/*退出询问*/void Exit(void);/*退出询问*/*void DisplayerRequest(Link link); */void display(Link link);/*显示文件*/void load(Link link); /*把文件中数据加载到链表*/void save(Link link);void sort(Link link);/*主函数*/int main(void) Init();/*图形驱动*/ Greeting(); /*欢迎界面*/ Player(); /*玩家信息管理模块*/ SelectSpeed();/*等级模块*/ DrawK();/*开始画面*/ GamePlay();/*玩游戏具体过程*/ Close();/*图形结束*/void Init(void) int gd=DETECT,gm; registerbgidriver(EGAVGA_driver); /*该函数告诉连接程序在连接时把EGAVGA的驱动程序装入到用户的执行程序中*/ initgraph(&gd,&gm,c:tc); /*initgraph(int far *gdriver, int far *gmode, char *path); gdriver和gmode分别表示图形驱动器和模式, path是指图形驱动程序所在的目录路径*/void Greeting() int i=120; cleardevice(); setbkcolor(WHITE); setcolor(0); setcolor(3); settextstyle(0,0,3); outtextxy(70,200,The Rapacious Snake!); setcolor(7); settextstyle(0,0,1); outtextxy(220,420,CopyRight SISO 2010-2011); settextstyle(0,0,4); do setfillstyle(SOLID_FILL,7); bar(5,50,630,100); setcolor(3); settextstyle(0,0,4); outtextxy(10,i,Welcome to the Game); delay(delayspeed*2); setcolor(WHITE); settextstyle(0,0,4); outtextxy(10,i,Welcome to the Game); i-=5; while(i=60); key=bioskey(0); if(key=ESC) Exit();void Player(void) cleardevice(); setbkcolor(WHITE); setfillstyle(SOLID_FILL,7); bar(5,100,630,130); setcolor(0); settextstyle(0,0,2); outtextxy(42,110,Please Input the name of Player!); setcolor(RED); settextstyle(0,0,2); outtextxy(140,220,No More Than Twenty Byte); setcolor(7); settextstyle(0,0,2); outtextxy(250,303,Name:); setcolor(7); settextstyle(0,0,1); outtextxy(220,420,CopyRight SISO 2010-2011); gotoxy(42,20); scanf(%s,&playname); getch();void SelectSpeed(void) int bar_y=0; snake.x0=100;snake.y0=100; snake.x1=110;snake.y1=100; snake.direction=1; food.yes=0; snake.life=1; snake.node=2; cleardevice(); setbkcolor(WHITE); setcolor(6); settextstyle(0,0,3); outtextxy(40,100,Please choose the Speed); setcolor(3); settextstyle(0,0,2); outtextxy(200,210,1.Low Speed); outtextxy(200,260,2.Normal Speed); outtextxy(200,310,3.High Speed); setcolor(7); settextstyle(0,0,1); outtextxy(200,420,CopyRight SISO-CharlesZhu 2010-2011); do key=bioskey(0); if(key=ESC) Exit(); if(key=UP) if(bar_y=0) setfillstyle(SOLID_FILL,WHITE); bar(180,bar_y,440,bar_y+40); speedmode=1; speed=1; bar_y=200; if(bar_y=200) setfillstyle(SOLID_FILL,WHITE); bar(180,bar_y,440,bar_y+40); speedmode=1; speed=1; bar_y=200; if(bar_y=250) setfillstyle(SOLID_FILL,WHITE); bar(180,bar_y,440,bar_y+40); speedmode=1; speed=1; bar_y=200; if(bar_y=300) setfillstyle(SOLID_FILL,WHITE); bar(180,bar_y,440,bar_y+40); speedmode=2; speedlevel=1; speed=2; bar_y=250; if(key=DOWN) if(bar_y=300) setfillstyle(SOLID_FILL,WHITE); bar(180,bar_y,440,bar_y+40); speedmode=1; speed=1; bar_y=200; if(bar_y=250) setfillstyle(SOLID_FILL,WHITE); bar(180,bar_y,440,bar_y+40); speedmode=2; speedlevel=2; speed=3; bar_y=300; if(bar_y=200) setfillstyle(SOLID_FILL,WHITE); bar(180,bar_y,440,bar_y+40); speedmode=2; speedlevel=1; speed=2; bar_y=250; if(bar_y=0) setfillstyle(SOLID_FILL,WHITE); bar(180,bar_y,440,bar_y+40); speedmode=2; speedlevel=2; speed=3; bar_y=300; setfillstyle(SOLID_FILL,YELLOW); bar(180,bar_y,440,bar_y+40); setcolor(3); settextstyle(0,0,2); outtextxy(200,210,1.Low Speed); outtextxy(200,260,2.Normal Speed); outtextxy(200,310,3.High Speed); while(key!=ENTER); cleardevice(); setcolor(3); settextstyle(0,0,5); outtextxy(100,180,Please Wait); outtextxy(250,300,Loding.); sleep(sleepspeed*4); cleardevice(); setcolor(3); settextstyle(0,0,6); outtextxy(100,200,Drawing.); sleep(sleepspeed); void DrawK(void) cleardevice(); setcolor(8); setlinestyle(SOLID_LINE,0,THICK_WIDTH); for(i=58;i=598;i+=5) rectangle(i,36,i+5,47); rectangle(i,453,i+5,464); for(i=36;i0;i-) if(food.x=snake.xi&food.y=snake.yi) food.yes=0; if(speed=1) if(food.x90&food.x231&food.y310&food.x70&food.y380&food.x231&food.y310&food.x270&food.y433&food.x131&food.y213&food.x123&food.y153&food.x131&food.y213&food.x293&food.y110&food.x85&food.y380&food.x85&food.y300&food.x120&food.y110&food.x380&food.y380&food.x380&food.y3) for(i=4;i0;i-) snake.xi=snake.xi-1; snake.yi=snake.yi-1; switch(snake.direction) case 1:snake.x0+=10;break; case 2:snake.x0-=10;break; case 3:snake.y0-=10;break; case 4:snake.y0+=10;break; case 2: for(i=snake.node-1;i=0;i-) snake.xi=snake.xi; snake.yi=snake.yi; if(snake.x0590|snake.y0450) snake.life=0; GameOver(); if(speed=1) if(snake.x0=98&snake.x0=246&snake.y0=319&snake.x0=292&snake.y0=390&snake.x0=239&snake.y0=320&snake.x0=82&snake.y0=450&snake.x0=152&snake.y0=230&snake.x0=150&snake.y0=170&snake.x0=153&snake.y0=230&snake.x0=311&snake.y0=120&snake.x0=110&snake.y0=400&snake.x0=110&snake.y0=316&snake.x0=140&snake.y0=130&snake.x0=405&snake.y0=400&snake.x0=400&snake.y0=0;i-) /*画蛇*/ rectangle(snake.xi,snake.yi,snake.xi+10,snake.yi-10); if(speedmode=1)sleep(sleepspeed); else if(speedmode=2) switch(speedlevel) case 1:delay(delayspeed); case 2:delay(delayspeed/2); setcolor(WHITE); rectangle(snake.xsnake.node-1,snake.ysnake.node-1,snake.xsnake.node-1+10,snake.ysnake.node-1-10); key=bioskey(0); if(key=P)t=2; if(key=ENTER)t=1; if(key=ESC) ContinueRequest(); if(key=UP&snake.direction!=4) snake.direction=3; if(key=RIGHT&snake.direction!=2) snake.direction=1; if(key=LEFT&snake.direction!=1) snake.direction=2; if(key=DOWN&snake.direction!=3) snake.direction=4; void GameOver(void) Link link,a; FILE *fp; a=(Node*)malloc(sizeof(Node); link=(Node*)malloc(sizeof(Node); strcpy(,00-00); strcpy(link-data.grade,11-11); link-next=NULL; strcpy(,playname); a-data.grade=score; a-data.speed=speed; fp=fopen(grade.txt,ab); if(fp=NULL) exit(0); else fwrite(a,sizeof(Node),1,fp); fclose(fp); cleardevice(); setcolor(RED); settextstyle(0,0,4); outtextxy(180,200,GAME OVER!); PrScore(); getch(); load(link); sort(link); save(link); display(link); ContinueRequest();void PrScore(void) int prscorex=50,prscorey=2; char str10,stt20; setfillstyle(SOLID_FILL,BLUE); if(snake.life=1) bar(prscorex,prscorey*4,prscorex*6,prscorey*15); setcolor(WHITE); if(snake.life=0) setcolor(3); settextstyle(0,0,2); sprintf(str,Score:%d,score); if(snake.life=0) outtextxy(prscorex*4+62,prscorey*120+62,str); sprintf(stt,Name:%s,playname); outtextxy(prscorex*4+30,prscorey*120+32,stt); if(snake.life=1) outtextxy(prscorex*1.1,prscorey*7,str);void ContinueRequest(void) cleardevice(); setcolor(RED); settextstyle(0,0,3); outtextxy(20,150,Do You Want to Continue?); settextstyle(0,0,2); outtextxy(170,290,Yes or No to Choose); settextstyle(0,0,1); outtextxy(290,330,(Y/N); key=bioskey(0); if(key=N)Exit(); if(key=Y|key=ESC) if(snake.life=1) DrawK(); GamePlay(); else if(snake.life=0)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论