Review past

Python to achieve “little rabbit and Bun” game

Python eight notes to achieve small games

Python to achieve the puzzle game

Python to achieve ski games

Python for classic 90 tank wars

Python FlappyBird small game

Python dinosaur jump a small game

Introduction:

Recently, I found that many people are interested in making small games in Python, so I spent half a day to make a small game of tower defense, here to share with you, I hope it will be helpful to you. Let’s get started happily ~

rendering

Ps: Cover renderings for reference only, tort deletion

The development tools

Python version: 3.6.4

Related modules:

Pygame module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required modules.

The principle is introduced

Game Rules:

The player builds towers of arrows to defend against enemy attacks.

Every once in a while, a wave of enemies will emerge from the cave in the upper left corner and head for the castle in the lower right corner. All you have to do is build towers of arrows to kill enemies to keep your castle from being attacked by enemies, and every time you shoot an enemy, you get a monetary reward.

Game failure:

Every time an enemy rushes into the castle, the health of the castle drops by a certain amount, and when the health of the castle reaches zero, the game fails.

Game victory:

As time goes on, the enemy will become stronger and more numerous, so there is no way you can win, lol.

Game interface description:

T1-t3 represents different tower types, and different tower types have different prices and capabilities;

XXX stands for selling the arrow tower (half price);

Pause means to Pause the game;

Quit indicates to Quit the game.

The player left-clicks the Arrow Tower/sell arrow Tower logo and right-clicks to discard the currently selected content. The middle arrow of the mouse is used to control the direction of the arrow tower shooting, otherwise the arrow tower will shoot freely.

Select the arrow tower, move it to the location you think is appropriate, and click the left button to build an arrow tower in that location (of course, if you can afford an arrow tower on your account, otherwise you will have to save money). After selecting the arrow tower for sale, put the arrow Tower for Sale logo on the built arrow tower and click the left mouse button to sell the selected arrow tower.

Step by step:

Step1: identify the main logic of the game

The main game logic should look like this:

The code is described as follows:

Step2: Implement the game start interface

The main point here is that when the mouse is detected moving over the button, the button color is performed, and when the mouse is detected where the button is clicked, the start screen pops up. The main code is as follows:

Step3: Implement the game selection interface

The main points are the same as step 2. The main code is as follows:

The game difficulty is defined in a.json folder, such as:

Step4: Realize the game interface

First, design the map in a.map file, with different numbers representing different map elements, such as:

This makes it easy to import different maps:

The second is the drawing toolbar:

In this way, the prototype of the game interface was developed. The next step is to create a batch of enemies every once in a while:

Use buttons to detect and define functions that allow users to interact with the game, such as tower building. The key detection code is as follows:

The definition of the operation function is as follows:

Of course, when the pause button is detected, you need to switch the game to the pause screen:

The realization of key flashing mainly depends on displaying the key every short period of time. Other points are the same as step 2.

Finally, make the arrow tower shoot continuously and use collision detection to determine if the arrow hit the enemy (the enemy’s health drops if it hits, and the enemy dies when it drops to 0), and draw all the elements that still exist in the game interface:

Of course, if the castle dies, the game is over:

Step5: realize the end interface

The main points are the same as step 2. The main code is as follows:

That’s the end of this article, thanks for watching, Python24 mini games series, next article will share fruit and gold mini games

To thank the readers, I’d like to share some of my recent programming gems to give back to each and every one of you.

Dry goods are mainly:

① More than 2000 Python ebooks (both mainstream and classic)

Python Standard Library (Chinese version)

(3) project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis videos (suitable for small white learning)

⑤Python Learning Roadmap

⑥ Two days of Python crawler boot camp live permissions

See profile or private message for full source code.