Snake game design based on Python

Collect and follow to avoid getting lost


Preface

  No matter what kind of software it is, it should realize its corresponding functions. As a small stand-alone game, Snake is one of the ways to provide people with diverse leisure and entertainment options. Simplicity, there is no doubt that this is the characteristic of Snake. Whether it is simple operation or simple game content, it can be opened to people of all ages. For those who devote themselves to work and run around all day, it is impossible for them to invest too much time and energy in games, so small games such as Snake are a good choice and can also meet their entertainment needs. It is precisely because of its simplicity that Snake does not have the fun and richness of other large games. Therefore, in order to increase the competitiveness of the game, this system will add a game difficulty selection module.

1. Function introduction

  The implementation of this snake game system is to draw a small snake in the screen display area, and at the same time draw a food in the screen display area that does not coincide with the body of the small snake. The game player uses the direction on the keyboard Use the keys to control the direction of the snake to eat food. After the food is eaten, another food will randomly appear in the display area. Each time the snake eats a piece of food, its body will grow by a length. If this is repeated, the longer the snake will be. The more difficult it is for gamers to operate. When the snake's head hits the edge of the screen or any part of the snake's body, the game ends [11].
The key to realizing the snake game lies in the formation of the snake body graphics and the drawing of the snake's trajectory. We can draw a small snake by drawing a small rectangular block (rect attribute). Every time the little snake moves, delete the last rectangular block of the snake's body and add a new rectangular block to the snake's head, which is equivalent to Add the last rectangular piece of the snake to the position of the snake's head. Every time the snake moves one unit, the screen is redrawn, so that the dynamic effect of the snake's movement can be achieved. When the snake eats food, the snake's head covers the food, a small rectangular block is added to the snake's tail, and another food is randomly drawn using the random module (the previous food disappears at this time) [12].

2. Development environment

Development language: Python
Software version: python3.7/python3.8
Database tool: Navicat11
Development software: PyCharm/vs code

————————————————

3. Program flow design

1.Program flow chart

  The flow chart of the game program running this time is shown in Figure 4-1.
Insert image description here

Figure 4-1 Program flow diagram

2.Interface design

First define the size, background color, etc. of the program running interface to facilitate unified display on each interface and provide players with a better look and feel.
The game interface is divided into: start interface, game interface and end interface. In the start interface, the custom game name is displayed in the upper left corner of the program running window, the imported background image is displayed in the center, and the gameplay instructions and operation guide are displayed below; in the game interface, the entire program window is used for the game, and the snake can be displayed in the window Move anywhere in the game, the food is displayed as a blue circle, and the player's score is displayed in the upper right corner. Each piece of food eaten increases one point; when the game ends, the end interface is displayed, with the word "GameOver" in the center of the window, and the game is displayed at the bottom It prompts "Please press any key to restart the game or the Esc key to exit the game."

3. Functional design

In addition to the implementation of the basic functions of the game, in order to increase the playability of the game, functions such as F1 acceleration, F2 deceleration, and F3 invincibility are set up. A pause function has also been added to the game, and players can pause/continue the game through the SPACE key. Whenever the player eats 5 pieces of food, a barrier with a length between 1 and 5 will be randomly generated, and the speed will also increase. At the end of the game, you can press any key except ESC to restart the game. Press the ESC key to exit the program.
(3) Game process
When the game starts, the little snake will have an initial direction. If the player does not enter a valid direction operation, the little snake will keep pressing direction, and the little snake cannot move in the opposite direction, that is to say, the snake's tail cannot be used as the snake's head to move. When the user enters a valid direction, the snake head will first move in the direction given by the player, and the snake body will move with the movement of the snake head (covering the previous trajectory). The way to realize the movement of the greedy snake is to draw a rectangular block in the moving direction as the snake's head, and delete a rectangular block for the snake's tail, and the entire snake is redrawn and refreshed on the screen, so that it looks like the snake has moved forward one unit. distance.
In this system, the collision detection of the game includes three situations: one is when the snake eats food, at this time a rectangular block is added to the snake body; the other is the collision between the snake head and the snake body, at this time The game ends; the third is the collision between the snake's head and an obstacle, and the game will end under normal circumstances (the greedy snake will not die in invincible mode).

4. System implementation

The main modules and functions used in this system design are shown in Table 5-1 below.
Table 5-1 Main modules of the Snake system
Insert image description here

(1) Function Overview
The main functions and functions used in this system design are shown in Table 5-2 below.
Table 5-2 Main functions of the Snake system
Insert image description here

5. Effect drawing

Insert image description here

Insert image description here

Contents
Abstract I
Abstract II
1. Introduction 1
(1) Research status and background 1
(2) Operational analysis 2
2. Demand analysis 2
(1) Functional requirements 2
(2) Non-functional requirements 3
3. Introduction to games and development tools 3
(1) Introduction to the game 3
(2) Introduction to Python 3
1. How Python operates 3
2. Characteristics of Python 4< /span> 5. System implementation 8 (3) Game process 8 3. Functional design 8 2. Interface Design 7 1. Program flow chart 6 (2) Program flow design 6 (1) Function introduction 6 4. Outline Design 6 (3) Introduction to PyCharm 5
3. Introduction to PyGame 5 (2) Construction start interface 9 (3) Drawing of snakes and food 10 (4) Snake movement 11 (5) Game running body 12 5.6 Collision detection 14 6 System Test 15 6.1 Start the game 15 6.2 Generate obstacles 15 6.3 Invincibility function 16 6.4 System Test 17 7. Optimization and Summary 18 (1) Optimization and Upgrade Idea 18 (2) Project Summary 18 Acknowledgments 20 References 21
























Guess you like

Origin blog.csdn.net/QQ2743785109/article/details/133393431