欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

貪吃蛇C語言代碼實(shí)現(xiàn)(難度可選)

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎ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語言

下一篇:C++實(shí)現(xiàn)五子棋游戲

本文標(biāo)題:貪吃蛇C語言代碼實(shí)現(xiàn)(難度可選)

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/909.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有