国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

貪吃蛇C語言代碼實現(難度可選)

2020-01-26 13:46:49
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C語言實現貪吃蛇的具體代碼,供大家參考,具體內容如下

/********************************************************* ********************貪吃蛇(難度可選)******************** **************制作者:Xu Lizi  日期:2012/12/31******** ********************部分函數有借鑒************************ **********************************************************/ #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #include<time.h>   int snakey[100]={5,4,3,2,1};  /*定義蛇的橫坐標*/ int snakex[100]={1,1,1,1,1};  /*定義蛇的縱坐標,蛇頭起始位置為(5,1)*/ int life=0; /*定義蛇的生命,0表示存活,1表示死亡*/ int lenght=5;  /*定義蛇的長度,初始為5節*/   char map[12][24]={"***********************", /*y*/      "*      *",      "*      *",      "*      *",      "*      *",      "*      *",      "*      *",      "*      *",      "*      *",      "*      *",      "*      *",    /*x*/ "***********************"};   void put_money(int i,int j)  /*放錢函數,使用隨機數,隨機出現食物*/ {   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()  /*游戲結束*/ {   life=1;   printf("笨蛋,輸了吧!!!/n");   return; }   void turn_up()  /*向上移動*/ {   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 向上移動:W ;向下移動:S ; 向左移動:A ; 向右移動:D /n");  printf("/t請選擇難度(數字)/n/t分1~5級,分別代表/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) /*當蛇死亡時結束循環*/  {   /*讓蛇自動運行的函數******有借鑒*/   timeover=1;   start=clock();   while((timeover=(clock()-start<=hard*100))&&!kbhit());  //難度設定   if(timeover)   {      direcation=getch();   }   /*讓蛇自動運行的函數******有借鑒*/    switch(direcation)   {     case 'w':turn_up();break;     case 's':turn_down();break;     case 'a':turn_left();break;     case 'd':turn_right();break;   }  }  return 0; } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 新沂市| 九江县| 木兰县| 武川县| 建水县| 枣庄市| 安泽县| 措美县| 伊川县| 简阳市| 泰宁县| 客服| 安远县| 浙江省| 柘荣县| 扬州市| 五指山市| 宁海县| 北海市| 常熟市| 同德县| 巴青县| 松江区| 万山特区| 弋阳县| 仙游县| 通渭县| 宜丰县| 樟树市| 渑池县| 星子县| 宁南县| 大邑县| 南安市| 乐昌市| 吴堡县| 灌云县| 孟州市| 娱乐| 武平县| 肇庆市|