新手共享贪吃蛇代码(200行)
暑假期间用C语言写的一个贪吃蛇游戏,无需图形驱动,控制台程序。现在贴出来与和我一样的新手共享,并请高手多指教。欢迎大家回帖指正批评。pcc32.h是在网上找的一个头文件,因为我用dev,不支持tc的bioskey等函数。程序及源码下载地址在代码下面。
代码如下,在dev-c++编译通过:
程序代码:
#include <stdio.h> #include "pcc32.h" #define FALSE 0 #define TURE 1 #define WIDTH 22 #define HEIGH 18 #define BASETIME 300 #define TIME(x) BASETIME-23*(x) enum DIR{ LEFT, RIGHT, UP, DOWN }; struct COOR{ uint8 x; uint8 y; }; struct SNAKE{ uint16 length; struct COOR body[328]; enum DIR dir; }; struct SNAKE snake={0,{0,0},0}; struct COOR food; uint16 score=0; void Ready(); void Play(); void Over(); void Wall(); void InitSnake(); void InitFood(); uint8 IsOver(uint8 x, uint8 y); uint8 InSnake(uint8 x, uint8 y); uint8 MoveSnake(); void Ready() { printf("说明:\n"); printf("开始游戏前请将输入法切换成英文输入法。\n"); printf("↑↓←→分别控制蛇上下左右移动。\n"); printf("回车键暂停或继续游戏。\n"); printf("\n\n请按任意键开始游戏"); jkGetKey(); } void Play() { uint8 level=1; system("cls"); Wall(); setTextColor(YELLOW); InitSnake(); InitFood(); do { if(level<10) level=(snake.length+5)/10; gotoTextPos(0,HEIGH+1); printf("SCORE: %5u LEVEL: %u",score,level); sleep(TIME(level)); if(jkHasKey()) switch(jkGetKey()) { case JK_LEFT: if(snake.dir!=RIGHT) snake.dir=LEFT; break; case JK_RIGHT: if(snake.dir!=LEFT) snake.dir=RIGHT; break; case JK_UP: if(snake.dir!=DOWN) snake.dir=UP; break; case JK_DOWN: if(snake.dir!=UP) snake.dir=DOWN; break; case JK_ENTER: for(;jkGetKey()!=JK_ENTER;); break; default:; } }while(MoveSnake()); } void Over() { gotoTextPos(WIDTH-10,HEIGH/2-1); setTextColor(WHITE); printf("Your score is:%u",score); sleep(1500); jkGetKey(); } void Wall() { uint8 x=0,y=0; setTextColor(RED_ORANGE); gotoTextPos(0,0); while(x++<WIDTH) printf("■"); while(++y<HEIGH) { gotoTextPos(0,y); printf("■"); gotoTextPos((WIDTH-1)*2,y); printf("■"); } gotoTextPos(0,HEIGH-1); while(--x>0) printf("■"); } void InitFood() { srand((unsigned)time(NULL)); do { food.x=rand()%(WIDTH-2)+1; food.y=rand()%(HEIGH-2)+1; }while(InSnake(food.x, food.y)); gotoTextPos(food.x*2, food.y ); printf("●"); } void InitSnake() { uint8 i=0; snake.length=5; snake.dir=RIGHT; setTextColor(YELLOW); do { snake.body[i].x=5-i; snake.body[i].y=4; gotoTextPos(snake.body[i].x*2,snake.body[i].y); printf("■"); }while(++i<snake.length); } uint8 IsOver(uint8 x,uint8 y) { if(InSnake(x,y)||0==x||WIDTH-1==x||0==y||HEIGH-1==y) return TURE; return FALSE; } uint8 InSnake(uint8 x, uint8 y) { uint8 i= snake.length; while(i--) if(snake.body[i].x==x&&snake.body[i].y==y) return TRUE; return FALSE; } uint8 MoveSnake() { uint8 i,x=snake.body[0].x, y=snake.body[0].y; switch(snake.dir) { case LEFT: --x; break; case RIGHT: ++x; break; case UP: --y; break; default: ++y; } if(x==food.x&&y==food.y) { ++snake.length; InitFood(); score+=50; } else { gotoTextPos(snake.body[snake.length-1].x*2,snake.body[snake.length-1].y); printf(" "); } gotoTextPos(x*2,y); printf("■"); for(i=snake.length-1;i>0;--i) snake.body[i]=snake.body[i-1]; if(IsOver(x,y)) return FALSE; snake.body[i].x=x; snake.body[i].y=y; return TRUE; } int main(void) { setConsoleTitle("Snake 贪吃蛇 貌似掉线 QQ群:31066229"); setCursorVisible(0); fixConsoleSize(WIDTH*2,HEIGH+2); Ready(); Play(); Over(); return 0; }
程序源码下载地址:http://www.
如果不能下载就试下这个:http://www.