貪吃蛇C語(yǔ)言代碼實(shí)現(xiàn)(難度可選)
更新時(shí)間:2020年06月19日 08:44:20 作者:小-立子
這篇文章主要為大家詳細(xì)介紹了貪吃蛇C語(yǔ)言代碼實(shí)現(xiàn),游戲難度可供選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)貪吃蛇的具體代碼,供大家參考,具體內(nèi)容如下
/********************************************************* ********************貪吃蛇(難度可選)******************** **************制作者:Xu Lizi 日期:2012/12/31******** ********************部分函數(shù)有借鑒************************ **********************************************************/ #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #include<time.h> int snakey[100]={5,4,3,2,1}; /*定義蛇的橫坐標(biāo)*/ int snakex[100]={1,1,1,1,1}; /*定義蛇的縱坐標(biāo),蛇頭起始位置為(5,1)*/ int life=0; /*定義蛇的生命,0表示存活,1表示死亡*/ int lenght=5; /*定義蛇的長(zhǎng)度,初始為5節(jié)*/ char map[12][24]={"***********************", /*y*/ "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", /*x*/ "***********************"}; void put_money(int i,int j) /*放錢(qián)函數(shù),使用隨機(jī)數(shù),隨機(jī)出現(xiàn)食物*/ { int x=0,y=0; srand(time(NULL)); while ( (map[y][x]==003) || (map[y][x]==002) || (map[y][x]=='*') || ((x==i)&&(y==j)) ) { x=rand()%21+1; y=rand()%10+1; } map[y][x]='$'; return; } void output() /*輸出*/ { system("cls"); int i,j; for(i=0; i<12; i++) { for(j=0; j<23; j++) printf("%c", map[i][j]); printf("\n"); } return; } void gameover() /*游戲結(jié)束*/ { life=1; printf("笨蛋,輸了吧!!!\n"); return; } void turn_up() /*向上移動(dòng)*/ { system("cls"); int i; if ( (snakex[0]==1) || (map[snakex[0]-1][snakey[0]]==003) ) gameover(); else { if (map[snakex[0]-1][snakey[0]]=='$') { put_money( snakey[0], snakex[0]-1 ); lenght++; map[snakex[lenght-1]][snakey[lenght-1]]=003; } for(i=lenght; i>0; i--) { snakex[i]=snakex[i-1]; snakey[i]=snakey[i-1]; } map[snakex[lenght]][snakey[lenght]]=' '; snakex[0]--; for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003; map[snakex[0]][snakey[0]]=002; output(); } return; } void turn_down() /*向下*/ { system("cls"); int i; if ( (snakex[0]==10) || (map[snakex[0]+1][snakey[0]]==003) ) gameover();else { if (map[snakex[0]+1][snakey[0]]=='$') { put_money(snakey[0],snakex[0]+1); lenght++; map[snakex[lenght-1]][snakey[lenght-1]]=003; } for(i=lenght; i>0; i--) { snakex[i]=snakex[i-1]; snakey[i]=snakey[i-1]; } snakex[0]++; map[snakex[lenght]][snakey[lenght]]=' '; for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003; map[snakex[0]][snakey[0]]=002; output(); } return; } void turn_left() /*向左*/ { system("cls"); int i; if ( (snakey[0]==1) || (map[snakex[0]][snakey[0]-1]==003) ) gameover();else { if (map[snakex[0]][snakey[0]-1]=='$') { put_money(snakey[0]-1,snakex[0]); lenght++; map[snakex[lenght-1]][snakey[lenght-1]]=003; } for(i=lenght; i>0; i--) { snakex[i]=snakex[i-1]; snakey[i]=snakey[i-1]; } map[snakex[lenght]][snakey[lenght]]=' '; snakey[0]--; for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003; map[snakex[0]][snakey[0]]=002; output(); } return; } void turn_right() /*向右*/ { system("cls"); int i; if ( (snakey[0]==21) || (map[snakex[0]][snakey[0]+1]==003) ) gameover();else { if (map[snakex[0]][snakey[0]+1]=='$') { put_money(snakey[0]+1,snakex[0]); lenght++; map[snakex[lenght-1]][snakey[lenght-1]]=003; } for(i=lenght; i>0; i--) { snakex[i]=snakex[i-1]; snakey[i]=snakey[i-1]; } map[snakex[lenght]][snakey[lenght]]=' '; snakey[0]++; for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003; map[snakex[0]][snakey[0]]=002; output(); } return; } int main() { int i,timeover,hard; long start; char name , direcation; printf("\n 向上移動(dòng):W ;向下移動(dòng):S ; 向左移動(dòng):A ; 向右移動(dòng):D \n"); printf("\t請(qǐng)選擇難度(數(shù)字)\n\t分1~5級(jí),分別代表\n\t1難,2中上,3中,4中下5,易:\n"); scanf("%d",&hard); system("cls"); for(i=1;i<5;i++) map[1][i]=003; /*輸出蛇身*/ map[1][5]=002; /*輸出蛇頭*/ put_money(0,0); output(); while(life!=1) /*當(dāng)蛇死亡時(shí)結(jié)束循環(huán)*/ { /*讓蛇自動(dòng)運(yùn)行的函數(shù)******有借鑒*/ timeover=1; start=clock(); while((timeover=(clock()-start<=hard*100))&&!kbhit()); //難度設(shè)定 if(timeover) { direcation=getch(); } /*讓蛇自動(dòng)運(yùn)行的函數(shù)******有借鑒*/ switch(direcation) { case 'w':turn_up();break; case 's':turn_down();break; case 'a':turn_left();break; case 'd':turn_right();break; } } return 0; }
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專(zhuān)題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
C語(yǔ)言解決青蛙跳臺(tái)階問(wèn)題(升級(jí)版)
所謂的青蛙跳臺(tái)階問(wèn)題,就是指一只青蛙一次可以跳上1級(jí)臺(tái)階,也可以跳上2級(jí)。求該青蛙跳上一個(gè)n級(jí)的臺(tái)階總共有多少種跳法。本文將用C語(yǔ)言解決這一問(wèn)題,需要的可以參考一下2022-01-01VC下通過(guò)系統(tǒng)快照實(shí)現(xiàn)進(jìn)程管理的方法
這篇文章主要介紹了VC下通過(guò)系統(tǒng)快照實(shí)現(xiàn)進(jìn)程管理的方法,較為詳細(xì)的講述了VC下通過(guò)系統(tǒng)快照實(shí)現(xiàn)進(jìn)程管理的原理與具體實(shí)現(xiàn)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10深入理解:Java是類(lèi)型安全的語(yǔ)言,而C++是非類(lèi)型安全的語(yǔ)言
本篇文章是對(duì)Java是類(lèi)型安全的語(yǔ)言,而C++是非類(lèi)型安全的語(yǔ)言進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C++進(jìn)程間共享數(shù)據(jù)實(shí)例
這篇文章主要介紹了C++進(jìn)程間共享數(shù)據(jù)的方法,是進(jìn)行C++應(yīng)用程序開(kāi)發(fā)中非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10