码迷,mamicode.com
首页 > 微信 > 详细

C语言贪吃蛇小程序

时间:2018-09-16 15:58:08      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:dao   ||   void   idt   tps   new   \n   ons   line   

技术分享图片
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<conio.h>
  4 #include<windows.h>
  5 
  6 #define High 20
  7 #define Width 50
  8 
  9 int direction,food_x,food_y;
 10 int canvas[High][Width]={0};//二维数组为小蛇运动空间 
 11 
 12 void goto_xy(int x,int y)//光标移动函数   https://blog.csdn.net/konghouy/article/details/80230571
 13 {
 14     HANDLE handle_out=GetStdHandle(STD_OUTPUT_HANDLE);
 15     COORD pos;
 16     pos.X=x;
 17     pos.Y=y;
 18     SetConsoleCursorPosition(handle_out,pos);
 19 }
 20 
 21 void Init(void)//初始化函数 
 22 {
 23     int i,j;
 24     for(i=0;i<High;i++)
 25     {
 26         canvas[i][0]=-1;
 27         canvas[i][Width-1]=-1; 
 28     }
 29     for(j=0;j<Width;j++)
 30     {
 31         canvas[0][j]=-1;
 32         canvas[High-1][j]=-1;//以上为边框对应的数组元素值 
 33     }
 34     for(i=0;i<5;i++)
 35     {
 36         canvas[High/2][Width/2-i]=i+1;//蛇头蛇身对应数组元素值 
 37     }
 38     begin:food_x=rand()%(High-5)+2; //食物生成      https://blog.csdn.net/u010141928/article/details/71439570
 39     food_y=rand()%(Width-5)+2;
 40     if(canvas[food_x][food_y]==0)
 41         canvas[food_x][food_y]=-2;//食物对应数组元素值 
 42     else
 43         goto begin;        
 44 }
 45 
 46 void show(void)//显示函数 
 47 {
 48     goto_xy(0,0);//每次界面显示都从原点处光标开始 
 49     int i,j;
 50     for(i=0;i<High;i++)
 51     {
 52         for(j=0;j<Width;j++)
 53         {
 54             if(canvas[i][j]==-1)
 55                 printf("#");
 56             else if(canvas[i][j]==1)
 57                 printf("@");
 58             else if(canvas[i][j]>1)
 59                 printf("*");
 60             else if(canvas[i][j]==-2)
 61                 printf("F");
 62             else 
 63                 printf(" ");
 64         }
 65         printf("\n");
 66     }    
 67     Sleep(100);                 //https://zhidao.baidu.com/question/106833147.html
 68 }
 69 
 70 void Snake_Move(void)
 71 {
 72     int i,j;
 73     int max=1;
 74     int Old_Tail_x,Old_Tail_y,Old_Head_x,Old_Head_y,New_Head_x,New_Head_y; 
 75     for(i=1;i<High-1;i++)
 76         for(j=1;j<Width-1;j++)
 77         {
 78             if(canvas[i][j]>0)
 79             {
 80                 canvas[i][j]++;
 81                 if(canvas[i][j]==2)
 82                 {
 83                     Old_Head_x=i;
 84                     Old_Head_y=j;
 85                 }
 86                 if(max<canvas[i][j])
 87                 {
 88                     max=canvas[i][j];
 89                     Old_Tail_x=i;
 90                     Old_Tail_y=j;
 91                 }
 92             }
 93         }
 94     if(direction==1)             //‘D‘小蛇向右走    
 95     {
 96         New_Head_x=Old_Head_x;
 97         New_Head_y=Old_Head_y+1; 
 98     }
 99     else if(direction==2)        //‘W‘小蛇向上走 
100     {
101         New_Head_x=Old_Head_x-1;
102         New_Head_y=Old_Head_y; 
103     }
104     else if(direction==3)        //‘A‘小蛇向左走 
105     {
106         New_Head_x=Old_Head_x;
107         New_Head_y=Old_Head_y-1; 
108     }
109     else if(direction==4)        //‘S‘小蛇向下走 
110     {
111         New_Head_x=Old_Head_x+1;
112         New_Head_y=Old_Head_y; 
113     }
114     if(canvas[New_Head_x][New_Head_y]==-1||canvas[New_Head_x][New_Head_y]>0)//判定失败    
115     {
116         printf("游戏失败\n"); 
117         exit(-1);
118     }
119     else if(canvas[New_Head_x][New_Head_y]==-2)//吃到食物 
120     {
121         begin:food_x=rand()%(High-5)+2;            
122         food_y=rand()%(Width-5)+2;
123         if(canvas[food_x][food_y]==0)
124             canvas[food_x][food_y]=-2;
125         else
126             goto begin;
127         canvas[New_Head_x][New_Head_y]=1;
128     }
129     else
130     {
131         canvas[Old_Tail_x][Old_Tail_y]=0;
132         canvas[New_Head_x][New_Head_y]=1;
133     }
134 }
135 
136 int Direction_Control(void)
137 {
138     int input=0;
139     if(kbhit())            //https://blog.csdn.net/boreassoft/article/details/76728405
140     {
141         input=getch();
142         if(D==input)   //https://zhidao.baidu.com/question/309492191.html
143         {
144             direction=1;
145             Snake_Move();
146         }
147         else if(W==input)
148         {
149             direction=2;
150             Snake_Move();
151         }
152         else if(A==input)
153         {
154             direction=3;
155             Snake_Move();
156         }
157         else if(S==input)
158         {    direction=4;
159             Snake_Move();
160         }
161     }
162     return input;        
163 }
164 
165 int main(void)
166 {
167     int t;
168     Init();
169     direction=1;//设定初始运动方向向右 
170     while(1)
171     {
172         show();
173         t=Direction_Control();
174         if(t==0)
175         Snake_Move();
176     } 
177     return 0;
178 }
View Code

 

C语言贪吃蛇小程序

标签:dao   ||   void   idt   tps   new   \n   ons   line   

原文地址:https://www.cnblogs.com/bigdapi/p/9655656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!