Python lottery games
Related modules:
Pygame module;
And some modules that come with Python.
Install Python and add it to the environment variable, and pip can install the required related modules.
I don't need to introduce the rules of the snake game, T _ T, writing a snake game is actually very simple. First, let's initialize the game:
Then define a snake:
Head_coord is used to record the position of the snake head, and tail _ coords is a two-dimensional array used to record the positions of all snake bodies. At first, the length of the snake was 3, and its position was randomly generated. The user controls the action of the snake by pressing the key:
It should be noted that the snake can't turn 180, but can only turn 90 degrees. For example, a snake that is moving to the left cannot move to the right immediately. Specifically, the code is implemented as follows:
Then, we need to randomly generate a food, and we need to ensure that the position of the food is different from that of the snake:
When updating the greedy snake, if it eats food, the length of the snake will increase by one, otherwise it will only move in a given direction without changing the length of the snake:
At the same time, when a snake eats food, it needs to regenerate a new food:
Finally, when the greedy snake hits the wall or the snake head hits the snake body, the game is over:
And show the game end interface:
Players control the game protagonist Pac-Man to eat all the beans hidden in the maze through the keys, and can't be caught by ghosts.
If you can finish all the beans in the maze smoothly and don't get caught by ghosts, the game will win, otherwise the game will fail.
Gradually realize:
Step 1: Define the game wizard class.
First of all, let's make it clear which game elves the game needs.
① wall grade
② Food (such as peas)
③ Role class
Character classes include PAC-Man and Ghost. Ghosts are controlled by computers, and Pac-Man is controlled by players.
Obviously, it must have the ability to update the position and change the direction of the role. Its source code is as follows:
Step 2: Design the game map
Using the game wizard class defined in Step 1, we can start designing the game map. Due to the limited time, I only wrote a game map of a level, and interested friends can expand it on this basis (it is very convenient to expand it on the basis of my source code ~). The design of the game map includes the following four aspects:
① Create a wall
(2) Create a door (originally used to close the ghost)
image.gif
③ Create a role
④ Create food.
Because food cannot overlap with the positions of walls, doors and people, in order to facilitate the design of game maps, it is necessary to create walls, doors and people before creating food:
Step 3: Design the main loop of the game.
Next, start designing the main loop of the game. The first is initialization:
Then define the main function:
The startLevelGame function is used to start a game, and its source code is as follows:
ShowText function is used to display suggestive text on the game interface when the game ends or the level is switched. Its source code is as follows: