Plants vs. Zombies: The "modifier [Python]" in the game must be cheating? Make good use of it to increase the fun!

The game "Plants vs. Zombies" is a very strategic mini game that integrates elements such as real-time strategy, tower defense and card collection. Players control plants to resist the attacks of zombies and protect this botanical garden. I remember that when I first started working, I used to hold the iPad and played late at night.

Recently, I was visiting Github and found a "Plants vs. Zombies" built in pure Python. I used the mouse to collect sunlight, select plant cards and plant plants. It is original and full of memories. At present, the project only supports adventure mode.

The old rules, you need packaged software to pay attention to the editor, QQ group: 721195303 to receive.

01

Install and run

1). Download the project code

git clone [email protected]:marblexu/PythonPlantsVsZombies.git

2). Switch to python3 environment, the official recommendation is to use python3.7 and install pygame

pip install pygame==1.9.6

3). Switch to the project directory and run the project code

python main.py

The effect of the successful installation project is as follows:

02

How to play

Use the mouse to collect sunlight, collect plant cards and plant seeds, just click the mouse

You can change the starting level by changing the value

1). When START_LEVEL_NUM is 1 and 2, it means the start is daylight mode, as shown in the figure below:

2). When START_LEVEL_NUM is 3, it means the start is night mode, as shown below:

3). When START_LEVEL_NUM is 4, it means the start is in conveyor mode, as shown in the figure below:

4). When START_LEVEL_NUM is 5, it means the start is the nut bowling mode, which is very fun, simple and crude! As shown below:

03

List of project codes

The project code is mainly divided into these large modules

The biggest feature of the operations of Plants vs. Zombies is that there are more operations to click with the mouse. So, let's take a look at how this project is related to mouse operation, which can be learned through the following code:

It uses an event_loop method. Through the for event in pg.event.get() loop, all mouse and keyboard operations can be obtained. This is a very flexible function in pygame. After getting the mouse click action, we need to get The position of the mouse click. The code pointed to by the arrow above is to get the value of the horizontal and vertical coordinates of the mouse click, so that the position of a mouse click is known.

There are different zombies, different plants, and even different maps in Plants vs. Zombies. For example, there are many kinds of plants: the most basic sunflower, pea shooter, and nuts. Because different plants have different attributes, we need to use different classes to represent plants.

According to the above code, we will write a Plant base class first, and then other plant classes will inherit this Plant base class

In this game, the operation of zombie movement is continuous frame. It is not completely translational. You can see how the author handles the animation sequence operation. As shown below:

This is a group of ordinary zombies. If you don't look carefully, you will think that each picture is the same. In fact, when you look carefully, you will find that the movements of zombies in each picture are slightly different. In the process of walking, the zombie is actually constantly switching a group of pictures. In this way, the movements of the zombies we see are coherent.

 

04

How to cheat

According to the above introduction, the project uses json files to store level data (such as the location and time of zombies, background information). We can modify the game parameters by modifying the content of the json file, such as adjusting the amount of initial sunlight and adjusting the appearance of zombies. s position. For example, if the value of START_LEVEL_NUM in source/constants.py is 3, modify source/data/map/level_3.json accordingly

init_sun_value corresponds to the value of initial sunlight, time is the time when the zombie appears, in milliseconds, map_y is the position where the zombie appears, counting from top to bottom on the map, the initial position is 0.

Interested friends can download this library, it is a good choice to learn Python and practice hands.


I still want to recommend the Python learning group I built myself : 721195303 , all of whom are learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and share dry goods from time to time (only Python software development related), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021. Welcome friends who are in advanced and interested in Python to join!

Guess you like

Origin blog.csdn.net/aaahtml/article/details/112917692