Come in quickly! ! ! Teach you step by step how to eat snakes

1. General framework

Insert image description here
This is the same as the game written before. The general framework is like this (there is a brief directory). We mainly encapsulate the play() function.
Since we referenced our own header file "game.h", we can put the preprocessing instructions of the library functions we want to reference in the header file "game.h".
If you are careful, you will find that the array we defined is 20 rows and 40 columns, because when we designed the map, the map was surrounded by '-' and '|'. You can find that the latter is twice the length of the former. In order to form For a square map, the number of rows must be twice the number of columns.

2. Map initialization

Insert image description here
This is the initialization of the array. If we want to print '-', then store 1 in the array. Similarly, '|' stores 2 in the array, and store 0 in other positions. The snake head is 3 and the food is 4. 0 1 2 3 4 is stored in the array here for the convenience of printing the map later. When we traverse the array, we print '-' when we encounter 1, print a space when we encounter 0, and so on.

3. Map printing

Insert image description here
As mentioned above, 1 represents '-', 2 represents '|', 0 represents space, 3 represents snake head, and 4 represents food.

4. The movement of snakes

Insert image description here
The movement of the snake is actually a visual effect. If we continuously change the coordinates of the snake's head and then clear the screen, we will get the moving effect.
Two functions are also introduced here:
1. kbhit: (Header file: conio) Its function is to return a true value when data is input. When we play with Snake, if the direction of the snake's body is not changed, it will keep moving forward. This is related to kbhit.
Insert image description here
When we don't enter it, he won't enter this statement.

2. getch: (Header file: conio) This function does not need to press Enter when reading data, it will automatically complete the next step. If we use getchar here, it will be very embarrassing. We still need to press Enter when entering a character. , otherwise the snake would not move.
Insert image description here
We input wsad from the keyboard, which is converted into assigning different values ​​​​to the direction, so that the coordinates of the snake head change. Some people here may wonder why two switch statements are used, one to receive ch and one to receive direction, because when we do not enter wsad, the snake will move in one direction. This is because we defined a direction variable at the beginning and assigned it to the initial value.

5. How to make the snake body gradually longer

Insert image description here
This is when our snake head coordinates coincide with the food coordinates, we reset the food coordinates, and score +1; and our count is also related to the snake body length. This
Insert image description here
code may not be easy to understand. Its function is to assign the previous coordinate in the structure to the next coordinate.
Insert image description here
Here you can see that when our snake head keeps moving forward, the snake body will also move forward, but we need to clean up the last section of the snake body.
Insert image description here

6. Final code display

// 
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
void initmap(int arr[20][40]);
void printmap(int arr[20][40]);
void playmap(int arr[20][40]);

game.c

// 
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
typedef struct Snake
{
	int x;
	int y;
}S;
	S snake[800] = { {15,20} };
	int arr[20][40] = { 0 };
	int food_x = 2;
	int food_y = 6;
	int count = 0;
	int i = 0;
void initmap(int arr[20][40])
{
	int i = 0;
	int j = 0;
	
	for (i = 0; i < 20; i++)
	{
		for (j = 0; j < 40; j++)
		{
			if (0 == i || 19 == i)//1,-,    2,|,      0,打印空格    3,打印蛇头'*'
				arr[i][j] = 1;
			if (0 == j || 39 == j)
				arr[i][j] = 2;
		}
	}
}
void printmap(int arr[20][40])
{
	int i = 0;
	int j = 0;
	for (i = 0; i < 20; i++)
	{
		for (j = 0; j < 40; j++)
		{
			switch (arr[i][j])
			{
			case 1:printf("-"); break;
			case 2:printf("|"); break;
			case 0:printf(" "); break;
			case 3:printf("*"); break;
			case 4:printf("$"); break;
			}
		}
		printf("\n");
	}
}
void playmap(int arr[20][40])
{
	int direction = 1;
	while (1)
	{
		arr[food_x][food_y] = 4;
		arr[snake[count].x][snake[count].y] = 0;//最后一节不断消失
		char ch = 0;
		if (_kbhit())
		{
			ch = _getch();
			switch (ch)
			{
			case 'w':direction = 1 ;break;
			case 's':direction = 2; break;
			case 'a':direction = 3; break;
			case 'd':direction = 4; break;
			default:break;
			}
		}
		for (i = count; i > 0; i--)
		{
			snake[i].x = snake[i - 1].x;
			snake[i].y = snake[i - 1].y;
			arr[snake[i].x][snake[i].y] = 3;
		}
		switch (direction)
		{
			case 1:snake[0].x--; break;
			case 2:snake[0].x++; break;
			case 3:snake[0].y--; break;
			case 4:snake[0].y++; break;
			default:break;
		}
		arr[snake[0].x][snake[0].y] = 3;
		Sleep(50);
		system("cls");
			
		if (snake[0].x == 0 || snake[0].x == 19 || snake[0].y == 0 || snake[0].y == 39)
			exit(0);
		if (snake[0].x == food_x && snake[0].y == food_y)
		{
				
			arr[food_x][food_y] = 0;
			food_x = rand() % 18+1;
			food_y = rand() % 38+1;
			arr[food_x][food_y] = 4;
			count++;
				
				
		}
		printmap(arr);
		printf("\ncount=%d",count);
			
	}
}

test.c

// 
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void menu()
{
	printf("**********************************\n");
	printf("*********** 1.play ***************\n");
	printf("*********** 0.exit ***************\n");
	printf("**********************************\n");
}
void play()
{
	int snake_x = 10;
	int snake_y = 15;
	int arr[20][40]={0};
	initmap(arr);
	printmap(arr);
	playmap(arr);
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;;
	do
	{
		menu();
		printf("请输入:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:play(); break;
		case 2:printf("退出游戏\n"); break;
		default:printf("输入错误,请重新输入\n"); break;
		}
	} while (input);
	return 0;
}

Thanks for watching! ! !

Guess you like

Origin blog.csdn.net/Djsnxbjans/article/details/126271124
Recommended