贪吃蛇源程序

上传人:F****n 文档编号:99765153 上传时间:2019-09-20 格式:DOC 页数:40 大小:71.50KB
返回 下载 相关 举报
贪吃蛇源程序_第1页
第1页 / 共40页
贪吃蛇源程序_第2页
第2页 / 共40页
贪吃蛇源程序_第3页
第3页 / 共40页
贪吃蛇源程序_第4页
第4页 / 共40页
贪吃蛇源程序_第5页
第5页 / 共40页
点击查看更多>>
资源描述

《贪吃蛇源程序》由会员分享,可在线阅读,更多相关《贪吃蛇源程序(40页珍藏版)》请在金锄头文库上搜索。

1、贪吃蛇源程序 本程序为贪吃蛇游戏,想必大家都玩过这个游戏,程序源代码用TC2.0编译通过,需要图形驱动文件的支持,在TC2.0的集成环境中有.本程序利用数据结构中的链表,来将蛇身连接,同时当蛇吃到一定数目的东西时会自动升级,及移动速度会加快,程序会时刻将一些信息显示在屏幕上,包括所得分数,要吃多少东西才能升级,并且游戏者可以自己手动选择游戏级别,级别越高,蛇的移动速度越快.另外,此游戏可能与CPU的速度有关系.源代码如下:*/*COMMENTS*/* snake_game.c */* it is a game for entermainment. */* in the begin,there

2、is only a snake head,and it will have to eat food */* to become stronger,and it eat a piece of food each time,it will */* lengthen its body,with the number of food the snake eats going up,it */* will become long more and more,and the score will goes up also. */* there is always useful information du

3、ring the game process. */* if the path by which the snake goes to eat food is the shortest,the */* score will add up a double. */* */* enjoy yourself,and any problem,contact */*/* all head file that will be used */#include#include#include#include#include#include/* useful MACRO */#define FOOD_SIZE8#d

4、efineSCALE8#defineUP_KEY0x4800#defineDOWN_KEY0x5000#defineLEFT_KEY0x4b00#defineRIGHT_KEY0x4d00#defineMOVE_UP1#defineMOVE_LEFT2#defineMOVE_DOWN3#defineMOVE_RIGHT4#defineINVALID_DIRECTION0#defineQUIT_KEYC0x1051#defineQUIT_KEY0x1071#defineSELECT_KEYC 0x1f53#defineSELECT_KEY 0x1f73#definePAUSE_KEYC 0x19

5、50#definePAUSE_KEY 0x1970#define DEFAULT_LEVEL 1#defineHELP_COLORWHITE#defineWELCOME_COLORWHITE#defineDEFAULT_COLORGREEN/* define the macro as follows to improve the game in future */#define FOOD_COLORYELLOW#defineSNAKE_HEAD_COLORRED#define DEFAULT_SNAKE_COLORYELLOW#defineEXIT_COLORWHITE#define SCOR

6、E_COLORYELLOW/* sturcture for snake body mainly ,and food also */typedef struct food_infor*FOOD_INFOR_PTR;typedef struct food_inforint posx;/* position for each piece of snake body */int posy;int next_move; /* next move direction */int pre_move; /* previous move direction,seems unuseful */int beEate

7、n; /* xidentifier for snake body or food */FOOD_INFOR_PTRnext; /* pointer to next piece of snake body */FOOD_INFOR_PTRpre; /* pointer to previous piece of snake body */FOOD_INFOR;/* structure for snake head */typedef struct _snake_headint posx;int posy;int next_move;int pre_move;int eatenC; /* numbe

8、r of food that have been eaten */int hop; /* number of steps to eat food */FOOD_INFOR_PTR next; /* pointer to the first piece of eaten food */SNAKE_HEAD;/* the left-up corner and right-down corner */typedef struct pointint x;int y;POINT;/* standards for game speed */* before level 5,the time interva

9、l is level_blevel - 1 / 10,and after */* level 5,the time interval is 1.00 / level_blevel - 1 */float level_b9 = 10.0,8.0,6.0,3.0,1.0,20.0,40.0,160.0,640.0;/* available varary */SNAKE_HEAD snake_head;FOOD_INFOR*current; /* always point to food */POINTborder_LT,border_RB;int driver,mode; /* for graph

10、ics driver */int maxx,maxy; /* max length and width of screen,in pixel */inteaten;/* identifier if the food is eaten */intscore = 0; /* total score */int level = DEFAULT_LEVEL; /* level or speed */floatinterval; /* based on speed */int snake_color = DEFAULT_SNAKE_COLOR; /* snake body color */inthopc

11、ount = 0; /* the shortest number of steps for snake */ /* to eat food */* all sub function */void init_graphics();void generate_first_step();int judge_death();int willeatfood();void generate_food();void addonefood();void redrawsnake();void show_all();void sort_all();void change_direction();void help

12、();void show_score(int);void change_level();void show_level();void release(SNAKE_HEAD);int can_promote();void win();void show_infor_to_level();void show_eaten();void calculate_hop();/* main function or entry */void main()char str50 = YOU LOSE!;/* fail information */clock_t start;int querykey;inttemp

13、x,tempy;/* if fail and want to resume game,go here */retry:init_graphics();show_all();/* show wall */generate_first_step(); /* generate food and snake head */show_score(score); /* show score to player */eaten = 0;/* begin to play game */while(1)if(judge_death() = 1)/* die */break;if(willeatfood() = 1)eaten = 1;addonefood();snake_head.hop +;if(snake_head.hop = hopcount)score += level * 2;else

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号