栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

贪吃蛇C语言代码实现(难度可选)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

贪吃蛇C语言代码实现(难度可选)

本文实例为大家分享了C语言实现贪吃蛇的具体代码,供大家参考,具体内容如下

 
#include 
#include 
#include 
#include 
#include 
 
 
int snakey[100]={5,4,3,2,1};  
int snakex[100]={1,1,1,1,1};  
int life=0;  
int lenght=5;  
 
 
char map[12][24]={"***********************",  
   "*   *", 
   "*   *", 
   "*   *", 
   "*   *", 
   "*   *", 
   "*   *", 
   "*   *", 
   "*   *", 
   "*   *", 
   "*   *", 
   "***********************"}; 
 
 
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请选择难度(数字)nt分1~5级,分别代表nt1难,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; 
} 

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

Javascript经典游戏 玩不停

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/63015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号