One of the little games ------ greedy snake tutorial

1. In my opinion, the most important thing to learn programming is multi action handwritten code. Only when you really write, can you find your own problems, find and correct them, and your ability can be improved. For most people, you can be a good programmer by understanding and applying what you have learned.

2. It's a good choice to write some games for practice. The amount of code is not too much or too little. It can consolidate knowledge and improve interest.

3. The most important thing of the game is the logical realization rather than the screen performance, so it doesn't matter if the black window is used. It's important to understand the principle.

Don't talk too much. Start.

1) How snakes move

The movement of the snake is a static movement, that is, the position of the snake is constantly redrawn. Because the redrawing speed is very fast, it seems to the human eye that it is moving.

2) The law of Snake Movement

The movement of a snake can be seen as only the head of the snake changing. For example, when a snake moves to the right, it means that the head moves one space to the right. The original position of the head changes to the position of the first section of the body. The original position of the first section of the body changes to the position of the second section, and so on.

3) Knowledge used

Array, structure, pointer

4) General idea

Define a structure to represent "point" (x,y). Use an array to store snakes. Each snake is actually a coordinate point. It's easy to know the coordinates of the snake. Just draw the snake in the corresponding coordinates.

How to move to the corresponding coordinates: using an API function ------ gotoxy(int x,int y), the cursor will be moved to the corresponding position.

How to get the user's input: kbhit() function can get the user's input key, return the ASCII of the key, non blocking.

5) Game effect

  

The code has specific comments, about 200 lines, which is very suitable for hands-on training.

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<conio.h> 
  4 #include<Windows.h>
  5 //Game screen size 
  6 #define HEIGHT 25//high 
  7 #define WIDE 45//wide 
  8 typedef struct
  9 {
 10     int x;//x coordinate 
 11     int y;//y coordinate 
 12     
 13 }Point;
 14 
 15 char direction='w';//direction 
 16 int point=0;//Fraction 
 17 
 18 void init(Point*snake,int bodyNum);//Initialization, setting the coordinates of the snake body 
 19 void move(Point*snake,int *bodyNum);//
 20 void printSnake(Point * snake,int bodyNum);//Print snake 
 21 char controlMove();//Control mobile 
 22 
 23 void change(Point*snake,int bodyNum);//Change the snake's coordinates 
 24 void clean(Point*snake,int bodyNum);//Clear previous snakes on screen 
 25 
 26 void printWall();//Print walls 
 27 int isDead(Point*snake,int bodyNum);//Judge whether the snake is dead, return 1, not return 0 
 28 void food(Point*snake,int bodyNum);//Randomly produce food 
 29 int eatFood(Point*snake,int *bodyNum);//Judge whether the food is returned 1, not 0 
 30 
 31 void HideCursor();//hide cursor 
 32 void gotoxy (int x,int y);//Move cursor position to specified coordinates 
 33 
 34 void HideCursor()
 35 {
 36      CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 37      SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
 38 }
 39 void gotoxy (int x,int y)
 40 {  
 41    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
 42    COORD pos;
 43    pos.X=x;
 44    pos.Y=y;
 45    SetConsoleCursorPosition(handle,pos);
 46 }
 47 
 48 int main()
 49 {
 50     Point snake[64];//Snakes
 51     int bodyNum=6;//Number of snake bodies,Initialize 6 
 52     srand(time(NULL));//Set random seed 
 53      
 54     HideCursor();
 55     init(snake,bodyNum);
 56     printWall();
 57     food(snake,bodyNum);
 58     printSnake(snake,bodyNum);
 59     
 60     while(1)
 61     {    
 62         move(snake,&bodyNum);
 63         Sleep(200-point);//The higher the score, the faster 
 64     }
 65         
 66 }
 67 
 68 void init(Point*snake,int bodyNum)
 69 {
 70     int i;
 71     snake[0].x=HEIGHT/2;
 72     snake[0].y=WIDE/2;
 73     for(i=1;i<bodyNum;i++)
 74     {        //Let the snake start in the middle of the picture 
 75             snake[i].x=HEIGHT/2+i;
 76             snake[i].y=WIDE/2;
 77     }    
 78 }
 79 
 80 void move(Point*snake,int *bodyNum)
 81 {    
 82     char dir=controlMove();//Key to get user input 
 83     clean(snake,*bodyNum);//Remove the snake before 
 84     change(snake,*bodyNum);//Change coordinates every time you move
 85     //Change the coordinates of the snake head 
 86     switch(dir)
 87     {
 88         case 'w':
 89             snake[0].y--;
 90             break;
 91         case 's':
 92             snake[0].y++;
 93             break;
 94         case 'a':
 95             snake[0].x--;
 96             break;
 97         case 'd':
 98             snake[0].x++;
 99             break;
100         default:
101             break;    
102     }
103     
104     if(eatFood(snake,bodyNum))//If you get food 
105     {    
106     
107         if(point>100)
108             point+=5; 
109         else
110             point+=10;
111              
112         food(snake,*bodyNum);//Randomly produce new food 
113     }
114         
115     printSnake(snake,*bodyNum);//Print new Snake 
116     if(isDead(snake,*bodyNum))//If death 
117     {
118         gotoxy(WIDE,HEIGHT);
119         printf("\n\n\nGAVE OVER");
120         exit(0);//Quit game 
121     } 
122 } 
123 
124 void printSnake(Point * snake,int bodyNum)
125 {    
126     int i;
127     gotoxy(snake[0].x,snake[0].y);//Position of snake head 
128     printf("@"); //Snake head 
129     for(i=1;i<bodyNum;i++)
130     {    
131         
132         gotoxy(snake[i].x,snake[i].y);
133         printf("*");//Snake body    
134     } 
135 }
136 
137 char controlMove()//Control mobile 
138 {    
139     char input; 
140      if(kbhit())//Check whether there is input, return 1, return 0 (Nonblocking function) 
141     {
142         input=getch();//Back to pressed key
143         if(input=='w'||input=='s'||input=='a'||input=='d')
144             direction=input;
145     }
146   return direction;
147 }
148 
149 void change(Point*snake,int bodyNum)
150 {
151     int i;
152     //Move the original snake head coordinate to the first snake body, and so on,
153     for(i=bodyNum-1;i>0;i--)
154         snake[i]=snake[i-1];
155 }
156 
157 
158 
159 void clean(Point*snake,int bodyNum)
160 {
161     int i;
162     for(i=0;i<bodyNum;i++)
163     {
164         gotoxy(snake[i].x,snake[i].y);
165         printf(" ");
166     
167     }
168 }
169 void printWall()
170 {
171     int i;
172     for(i=0;i<WIDE;i++)
173     {    
174         gotoxy(i,0);
175         printf("#");
176         gotoxy(i,HEIGHT);
177         printf("#");
178     }
179     for(i=0;i<HEIGHT;i++)
180     {    
181         gotoxy(0,i);
182         printf("#");
183         gotoxy(WIDE,i);
184         printf("#");
185     }
186 }
187 
188 int isDead(Point*snake,int bodyNum)
189 {    
190     int i;
191     if(snake[0].x==WIDE||snake[0].x==0)
192         return 1;
193     if(snake[0].y==HEIGHT||snake[0].y==0)
194         return 1;
195     for(i=1;i<bodyNum;i++)
196     {
197         if(snake[0].x==snake[i].x&&snake[0].y==snake[i].y)
198             return 1;
199     }
200     return 0;
201 }
202 
203 void food(Point*snake,int bodyNum)
204 {
205     int x,y;
206     x=rand()%(WIDE-1);
207     y=rand()%(HEIGHT-1);
208     gotoxy(x,y);
209     printf("o");
210     //Store the coordinates of the food in the array 
211     snake[bodyNum].x=x;
212     snake[bodyNum].y=y;
213 }
214 
215 int eatFood(Point*snake,int *bodyNum)
216 {    
217     
218     if(snake[0].x==snake[*bodyNum].x&&snake[0].y==snake[*bodyNum].y)
219     {    
220         gotoxy(snake[*bodyNum].x,snake[*bodyNum].y);
221         printf(" ");
222         snake[*bodyNum]=snake[*bodyNum-1];
223         snake[*bodyNum].x++;
224         (*bodyNum)++;
225         return 1;
226     }
227     return 0;
228 }

If you don't understand, you can ask me.

In the future, we will continue to update the small game series.

Tags: C Mobile Programming ascii Windows

Posted on Tue, 17 Mar 2020 02:58:16 -0700 by FloridaNutz