资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C语言怎么制作贪吃蛇游戏?

这篇文章将为大家详细讲解有关C语言怎么制作贪吃蛇游戏?,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联是一家专业提供铜仁企业网站建设,专注与成都网站建设、做网站、H5高端网站建设、小程序制作等业务。10年已为铜仁众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

/********************************************************* 
********************贪吃蛇(难度可选)******************** 
**************制作者:Xu Lizi 日期:2012/12/31******** 
********************部分函数有借鉴************************ 
**********************************************************/ 
#include 
#include 
#include 
#include 
#include 
 
 
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; 
} 

关于C语言怎么制作贪吃蛇游戏?就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站名称:C语言怎么制作贪吃蛇游戏?
网页URL:http://cdkjz.cn/article/gcjepo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220