Create a Pac-Man game in Python in 300 lines of code or less (tutorial includes source code)

Pac-Man is a classic platform game that probably everyone knows today. The name "Pac-Man" comes from the Japanese word "paku," which means to open and close the mouth. Creator Toru Iwatani was inspired by a Japanese story about a creature that protects children from monsters by eating them. When creating the game, he used key words in the story as a springboard, and the verb "eat" became the basis of everything.

Please add image description
Monsters are represented as four ghosts that attack the player in succession, similar to Space Invaders. Each ghost also has a unique personality. There is also a more important element in the story, the concept of life force "Kokoro", which allows creatures to eat monsters. In the game, this energy manifests as energy cookies, giving Pac-Man the ability to eat monsters for a short period of time.

In this tutorial I'll first walk you through the basic setup, then we'll create game objects for the maze walls, Pac-Man, and the ghost, ensure pathfinding in the maze, make the ghost move randomly, implement arrow controls for the player, and finally , placing food in the form of cookies throughout the maze. To better illustrate, I will attach pictures and GIFs.

basic settings

The generated game is about 300 lines of code, so I'll only list the most important parts here. The complete code can be found in my GitHub repository. The first step is to install the necessary packages. We will need pygame, numpy and tcod. Install all these tools using the pip tool (you can find how to do this in the article about Python applications). If you are using an IDE like PyCharm (which I recommend), the installation will happen after clicking on the missing package error message.

First, we will create a game window in a similar way to the previous tutorial on the Space Invaders game (only 100 lines

Guess you like

Origin blog.csdn.net/iCloudEnd/article/details/128453224