How to make zero-based Snake - (4) to explain the function of the remaining

emmm, no update yesterday ,, Well. Or because of the STM32 ,,, get it get a serial communication yesterday afternoon plus half the night ,, plus cooled down. . Cold hands ,, no written yesterday. . Today, because of course the open net sister, and I gave her former home desktop put together ,, it wasted a lot of time. . Here Insert Picture Description
Well, did not talk much, we have to start today's theme, this is the last chapter, I'll tell the rest of the functions it. After reading you can try it yourself with a write, or find wisdom pixels Niaoa, backgammon ah wisdom to try, the principle remains the pass. In addition to these projects can stimulate interest, but also can deepen your understanding of the c language.

moveSnake function

This function can be said that the core function of the game, we must understand the logic of the snake moving in the whole game.
First, snake when initialization of the map we have been printed out, but in the game, even if we do not operate, the snake is automatically moved, is moved upward direction when the initialization, other times is our last operation when direction.
How to display a snake " movement " of this effect le? Hey, by the coordinate calculation, the value of the other body of a snake moving forward on it.

	for (k = snake.len - 1; k>0; k--)//蛇的身子一格一格的移动
	{
		snake.x[k] = snake.x[k - 1];
		snake.y[k] = snake.y[k - 1];
	}

And snake when moving, if not to eat the fruit, it is not grown up. So we render when the snake moves forward, the last cell of the tail "#" should become "" .
Also, when using the keyboard to control the snake to move, if the snake is to go up, then you enter "S" down is invalid. The need to consider the rationality of the operation.
The complete code for the function:

void moveSnake()
{

	if (!grow)//如果没有长大,则上次的最后一个点,即尾巴,的地方显示空.这里的grow是在更新食物时我们标记的
	{
		gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]);
		printf(" ");//用空格覆盖之前的蛇尾巴
	}
	for (k = snake.len - 1; k>0; k--)//蛇的身子一格一格的移动
	{
		snake.x[k] = snake.x[k - 1];
		snake.y[k] = snake.y[k - 1];
	}
	if (kbhit())//检查当前是否有键盘输入
	{
		ch = getch();
		if (ch == ' ')												//如果输入空格表示暂停,使用while函数一直卡着。
		while (getch() != ' '){};
		if (ch != UP && ch != DOWN && ch != LEFT && ch != RIGHT)	//如果输入的时其他的键,则使用默认方向。这里的UP啊啥的都是之前定义好的
		{
			ch = Direction;
			goto sport;//无条件跳转
		}

		if (Direction != ch)//如果输入的不默认方向
		{
			if ((Direction == UP &&ch != DOWN) || (Direction == DOWN &&ch != UP) || (Direction == LEFT && ch != RIGHT) || (Direction == RIGHT && ch != LEFT)) //Prevent reverse movement
			{//确保输入的方向与当前方向逻辑上不违背,即确保蛇不能倒退
				Direction = ch;
				goto sport;
			}
			else
			{//如果违背了。则保持当前
				ch = Direction;
				goto sport;
			}

		}
		else
		{
			goto sport;
		}
	}
	else
	{
	sport:
		switch (ch)							//移动蛇的头部
		{
		case UP: snake.x[0]--; break;//注意,在窗口中,坐标与我们常用的坐标是倒过来的
		case DOWN:  snake.x[0]++; break;
		case LEFT: snake.y[0]--; break;
		case RIGHT: snake.y[0]++; break;
		default: break;

		}
	}

	gotoxy(snake.x[0], snake.y[0]);
	printf("@");							//输出蛇的头
	grow = 0;//把grow标记回0
	gotoxy(FRAME_HEIGHT, 0);//把光标始终弄在结尾处
}

emm, in order to facilitate understanding, I added a lot of comment in its original basis, to facilitate understanding.

isAlive function

This snake is used to determine whether the survival of a function. The main logic is to determine whether the snake hit the border and hit himself.
This function is very simple, is a function of judge

int isAlive(void)
{//判断蛇是否还活着,即判断蛇有没有撞上自己的身体,有没有碰到边界
	if (snake.x[0] == 0 || snake.x[0] == FRAME_HEIGHT - 1 || snake.y[0] == FRAME_WIDTH - 1 || snake.y[0] == 0)   //When you touch a wall or eat your own body, you die
		return 0;
	for (k = 1; k<snake.len; k++)
	if (snake.x[k] == snake.x[0] && snake.y[k] == snake.y[0])
		return 0;
	return 1;
}

getSpeed ​​function

This function is used to determine the length of the snake snake speed. But our main speed logic is not implemented here, so this function is not complicated.

void getSpeed(void)//根据长度修改速度
{
	if (snake.len <= 6)
		snake.speed = 200;
	else if (snake.len <= 10)
		snake.speed = 175;
	else if (snake.len <= 20)
		snake.speed = 150;
	else if (snake.len <= 30)
		snake.speed = 100;
	else snake.speed = 50;
	if (snake.len == 40)
		finish = 0;//长度40为通关
}

The main function main

Hey, on snake.h and its function finally finished. Next, since we can complete the last step. I said before, we just at the beginning of the header file that contains snake.h come in, you can directly use. I will not explain in detail the specific logic, but still give you a comment I play quite detailed.Here Insert Picture Description

#include "snake.h"
#include <windows.h>
#include <math.h>
//Transposed structure object
extern  struct Food food;//全局变量申明
extern  struct Snake snake;

//main function
int main()
{
	ret = menu();//显示初始化菜单,并且判断用户是否想玩游戏
	if (ret == 1)//输入1,表示是
	{
		while (finish)
		{
			initMap();																	//初始化地图
			while (TRUE)
			{
				updataFood();														//updata food
				getSpeed();															//speed
				moveSnake();														//move snake
				Sleep(snake.speed);	//是计算机处理的程序的函数。。功能是暂停,单位是毫秒,它就是用来控制蛇的快慢的,嘿嘿。其实就是通过这个函数让系统进程变慢,从而控制蛇的快慢
				printFont(FONT_WINDOW_STARTX, FONT_WINDOW_STARTY, snake.speed);		//显示游戏信息
				if (!(isAlive()))//如果没有存活,则结束
					break;
			}
			if (finish)//如果没有标记通关,则显示你这次的长度														//Death or customs clearance
			{
				system("cls");//清屏函数
				gotoxy(22, 5);
				printf("您的游戏结束,您操作的贪吃蛇长度为%d,请再接再厉",snake.len);
				gotoxy(24, 5);
				printf("按(1)键退出游戏,按(2)键重玩");
                char ch;
                while((ch=getch(),ch)!= 50)
                {
                    if(ch==49) return 0;
                    if(ch==50) break;
                }
				system("cls");
			}
			else//如果通关了
			{
				system("cls");
				gotoxy(22, 5);
				printf("恭喜你通关了");
				gotoxy(24, 5);
				printf("按(1)键退出游戏,按(2)键重玩");
                char ch;
                while((ch=getch(),ch)!= 50)
                {
                    if(ch==49) return 0;//注意这里的49没有引号,是数字,因为“1”的ASIIC码值就是49,所以可以这样判断
                    if(ch==50) break;
                }
				system("cls");
			}

		}

	}
}

Snake explain about this little project on here, you can go try, perfect the next. In fact, during the actual writing of class-based, written in order not order me to explain, I just order to facilitate understanding. Wrote a class-based children's shoes all know, most are their first given a general framework and then gradually improve and not write like this one breath.
Finally, if we do not understand are also welcome in the comments section to ask me. You can also group qq group to discuss technical bacteria

Published 26 original articles · won praise 20 · views 3458

Guess you like

Origin blog.csdn.net/qq_44886056/article/details/104210057