Qt mini game: greedy snake

1. Project purpose:

This project mainly learns by writing a snake game, and is familiar with the classes encapsulated in Qt.

2. Compilation environment

VS2019+Qt5.9

3. Function realization

It mainly realizes the basic functions listed below:

control greedy snake eat food

means snake

Control the snake to move up, down, left, and right

random allocation of control food

Controlling Snake Growth

pause game

End Game

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

4. Demand Analysis

4.1. Control greedy snakes to eat food

4.1.1. Representing snakes

The core function of the greedy snake is to control the greedy snake to eat food. We can easily think of using small squares to represent the body of the snake, so what kind of container should we use to store these small squares?

The movement of the snake can be understood as adding a small square at the head of the snake and deleting a small square at the tail of the snake, as shown in the figure below:

 

Therefore, the insertion operation will be performed at the head of the container, so the container whose underlying data structure is an array must not be selected, because the time complexity of inserting the head of the array is o(n), and the linked list is a very suitable data structure, because it is in The time complexity of head insertion is o(1), so we should choose the underlying data structure as the container of the linked list. Here we choose QList, which is a class encapsulated in Qt. Use this container to store the body of the snake, initialize it in the constructor, and then draw it in the interface through the rendering function.

//SnakeWidget.h文件
class Snake : public QMainWindow
{
    Q_OBJECT
 
public:
    Snake(QWidget *parent = nullptr);
    ~Snake();
 
 
    private:
    Ui::SnakeClass ui;
 
    //表示蛇
    QList<QRectF> snake;
 
    //表示蛇身的小方块的宽高
    int nodeWidth = 20;
    int nodeHeight = 20;
 
 
    //渲染(绘图)
    void paintEvent(QPaintEvent* event);
 
};

4.1.2. Control the snake to move up and down

Use the QTimer class to define a timer, which continuously sends out the timeout signal, and the corresponding slot function will continuously receive the signal to perform actions to control the movement of the snake, and then continuously re-render to achieve the effect of the snake moving continuously . As mentioned earlier, the movement of the snake is actually to add a small square to the head and delete a small square to the tail, so we can encapsulate 4 member functions to control the increase of the snake's head in 4 directions, and at the same time encapsulate a member function Indicates deletion of the tail.

//SnakeWidget.h文件
class Snake : public QMainWindow
{
    Q_OBJECT
 
public:
    Snake(QWidget *parent = nullptr);
    ~Snake();
 
 
private:
    Ui::SnakeClass ui;
 
    //表示蛇
    QList<QRectF> snake;
 
    //表示蛇身的小方块的宽高
    int nodeWidth = 20;
    int nodeHeight = 20;
 
    //定时器
    QTimer* timer;
    int time = 100;//超时时间间隔,单位毫秒
 
    //渲染(绘图)
    void paintEvent(QPaintEvent* event);
 
    //4个成员函数控制蛇4个方向头部的增加
    void addUp();
    void addDown();
    void addLeft();
    void addRight();
 
    //删除尾部
    void deleteLast();
 
private slots:
    void timeout();
 
};
 

4.1.3. Random distribution of control food

Food can be represented by a small square, and then encapsulate a member function to randomly allocate the position of the small square of food, initialize it in the constructor, and finally draw it on the interface through the rendering function.

//SnakeWidget.h文件
class Snake : public QMainWindow
{
    Q_OBJECT
 
public:
    Snake(QWidget *parent = nullptr);
    ~Snake();
 
 
private:
    Ui::SnakeClass ui;
 
    //表示蛇
    QList<QRectF> snake;
 
    //食物
    QRectF food;
 
    //表示蛇身的小方块的宽高
    int nodeWidth = 20;
    int nodeHeight = 20;
 
 
    //渲染(绘图)
    void paintEvent(QPaintEvent* event);
 
    //4个成员函数控制蛇4个方向的移动
    void addUp();
    void addDown();
    void addLeft();
    void addRight();
 
    //添加食物
    void addFood();
};

4.1.4. Control the growth of snakes

We only need to judge whether the small square of the snake's head overlaps with the small square of the food. Once it coincides, we add one more small square to the head, which means that the snake has grown. For example, if the snake moves to the right, we add a small square at the head and delete a small square at the tail to indicate the movement of the snake. Now, we add two small squares at the head and delete a small square at the tail to indicate the growth of the snake. up.

4.2. Pause the game

When the timer calls the stop() function, it will stop sending the timeout() signal, and then the slot function corresponding to the timeout() signal will stop executing. The slot function corresponding to timeout() controls the movement of the snake, and the slot function corresponding to timeout Stop the execution, then the corresponding snake will stop moving, and it will show the effect of pausing.

timer->stop();

4.3. End the game

A member function can be encapsulated to determine whether the game is over. Here, the sign of judging the end of the game is that the snake head touches the snake body.

bool SnakeWideget::checkContact()
{
	for (int i = 0; i < snake.length(); i++)
	{
		for (int j = i+1;j < snake.length(); j++)
		{
			if (snake[i] == snake[j])
				return true;
		}
	}
	
	return false;
}

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

おすすめ

転載: blog.csdn.net/m0_73443478/article/details/131686966