貪吃蛇C語言代碼實(shí)現(xiàn)(難度可選)
本文實(shí)例為大家分享了C語言實(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; /*定義蛇的長度,初始為5節(jié)*/ char map[12][24]={"***********************", /*y*/ "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", /*x*/ "***********************"}; void put_money(int i,int j) /*放錢函數(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請選擇難度(數(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; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C/C++語言中全局變量重復(fù)定義問題的解決方法
欄 目:C語言
本文標(biāo)題:貪吃蛇C語言代碼實(shí)現(xiàn)(難度可選)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/909.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)