C字符贪吃蛇
算法參照Perl字符貪吃蛇,源碼:
#include <stdio.h> #include <windows.h> #define WIDTH 12 // 寬 #define HEIGHT 8 // 高 #define DEBUG 0 const char FENCE ='*'; // 柵欄 const char HEAD ='@'; // 蛇頭 const char BODY ='#'; // 蛇身 const char FOOD ='O'; // 食物 const char BLANK =' '; // 空白 char arr[HEIGHT][WIDTH]; struct Snake{int y,x; }snake[WIDTH*HEIGHT]; // 結(jié)構(gòu)體,保存坐標(biāo)點(diǎn) int len=0,food=0,key=0,score=0,alive=1,direct=4,speed=1,full=(WIDTH-2)*(HEIGHT-2); // 長度、食物、按鍵、總分、存活、方向、速度 int uldr[5][2]={{},{-1,0},{0,-1},{1,0},{0,1}}; // 上、左、下、右void init(){ // 初始化游戲地圖int y=0,x=0,start_pos=HEIGHT/2;for(y=0;y<HEIGHT;y++)for(x=0;x<WIDTH;x++)if( y == 0 || y == HEIGHT-1 || x == 0 || x == WIDTH-1 ){arr[y][x] = FENCE;}else{arr[y][x] = BLANK;} snake[0].y=start_pos;snake[0].x=3;len++;snake[1].y=start_pos;snake[1].x=2;len++; arr[ snake[0].y ][ snake[0].x ] = HEAD ;arr[ snake[1].y ][ snake[1].x ] = BODY ; }void show(){int y,x;if(!DEBUG)system("cls");printf("your score:%d\n",score);printf("current speed:%d\n",speed);for(y=0 ; y<HEIGHT ; y++ ){for(x=0 ; x<WIDTH ; x++ )printf("%c",arr[y][x]);printf("\n");} }void make_food(){int rx,ry;while(!food){rx = rand()%WIDTH;ry = rand()%HEIGHT;if( arr[ry][rx] == BLANK ){arr[ry][rx]=FOOD;food=1;break;}} }void move(){int cnt=0;if( kbhit()!=0 ){while( kbhit() != 0)key=getch();switch(key){case 'w' : direct=(direct == 3)?3:1;break;case 'a' : direct=(direct == 4)?4:2;break;case 's' : direct=(direct == 1)?1:3;break;case 'd' : direct=(direct == 2)?2:4;break;default : direct;} // 禁止反向移動(dòng) }len++; // 準(zhǔn)備將當(dāng)前位置放在snake數(shù)組首部if(DEBUG)printf("len:%d\n",len);for(cnt=len-1;cnt>0;cnt--){snake[cnt].x=snake[cnt-1].x;snake[cnt].y=snake[cnt-1].y; // 1234 變?yōu)?51234 }snake[0].y+=uldr[direct][0];snake[0].x+=uldr[direct][1]; // 移動(dòng)蛇頭 }void check_head(){int y=snake[0].y;int x=snake[0].x;int i=0;int cnt=0;if(y < 1 || y > HEIGHT-2 || x < 1 || x > WIDTH-2){ // 是否越界alive=0;}if( arr[y][x] == BODY ){ // 是否吃到自己alive=0;}if( arr[y][x] == BLANK){arr[y][x] = HEAD;}if( arr[y][x] == FOOD ){ // 吃到食物arr[y][x] = HEAD;score++;len++;food=0;snake[len-1].y=snake[len-2].y; // 蛇尾增加一節(jié)蛇身snake[len-1].x=snake[len-2].x;make_food();}if(DEBUG)printf("len:%d\n",len);if(DEBUG){for(;i<len;i++){printf("y,x:(%d,%d)\n",snake[i].y,snake[i].x);}}arr[ snake[len-1].y ][ snake[len-1].x ]=BLANK; // 先清除蛇尾顯示len--;for(cnt=1;cnt<=len-1;cnt++){arr[ snake[cnt].y ][ snake[cnt].x ]=BODY; // 蛇身顯示 } }void main(){init();make_food();while(alive && len<full){move();check_head();speed=(speed > 9)?9:(score/5+1);if(alive){show();Sleep(1000-speed*100); // 多久刷新一次 }}if( len == full){printf("congratulations!\n");}else{printf("you lose\n");} }?
轉(zhuǎn)載于:https://www.cnblogs.com/wbjxxzx/p/4594050.html
總結(jié)
- 上一篇: 软件工程作业----个人读后感
- 下一篇: MySQL中用decimal的原因