SoFunction
Updated on 2024-11-13

Childhood memoirs of the python version of 4399 eat beans game

preceding paragraph

Yesterday the family came over and my oldest sister's kids grabbed the computer to play with me. RESULTS........ A long, long time later!

Well, you thought wrong. I'm not the type to bully kids. I'm an honest man. Gave it to her three seconds later.

Saw her playing a little game on 4399 and felt it was great for little kids. emmmmmm。。。。。。

Inspiration comes to block can not stop, uh-huh ~ today is into the main topic, the development of a 4399 similar Pacman small game, waiting for !!!!

main body (of a book)

Python version: 3.6.4

Related Modules:

pygame module;

and some of the modules that come with Python.

Module self-installation, environmental installation package, such as the newbie white will not be able to directly chat privately with the editor to install the environment first sprinkle!

First, let's look at the effect of a wave of realized code:

​​

​​

Game description:

The player controls the game's protagonist Pac-Man by using the ↑↓←→ keys to eat all the beans hidden inside the labyrinth, and can't be caught by the ghosts.

If you can eat all the beans in the maze and not be caught by the ghosts, you win the game, otherwise the game is lost.

Progressive realization:

Step1: Define the game sprite class

First, let's start by clarifying which game sprite classes are needed for that game.

① Wall type

图片

② Food items (i.e., beans)

图片

③ Role Class

Character classes include Pac-Man and Ghosts, with Ghosts being controlled by the computer for their movement trajectory and Pac-Man being controlled by the player for his movement trajectory.

Obviously, they all need to have the ability to update the position of the character and change the direction of movement of the character, the source code of which is as follows:

图片

Step2: Design the game map

Using the game sprite class defined in Step1, we can start designing the game map. Due to time constraints, I have only written a game map for one level, interested partners can expand on this (it is very convenient to expand on the basis of my source code~). The design of the game map includes the following four aspects:

① Create a wall

图片

② Create a door (for closing the ghost at the beginning)

图片

③ Create a role

图片

④ Create food

Because food cannot overlap with the positions of walls, doors, and characters, it is important to create walls, doors, and characters before creating food in order to facilitate the design of the game map:

图片

Step3: Design the main loop of the game

Next, start designing the main game loop. First is the initialization:

Then define the main function:

图片

The startLevelGame function is used to start a certain level of the game, and its source code is as follows:

图片

The showText function is used to display a hint text in the game interface at the end of the game or when switching levels, and its source code is as follows:

图片

wind up

To this article on the childhood memoirs of the python version of 4399 Pacman game is introduced to this article, more related python Pacman content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!