Review past

Python implements the “bunny and Bun” game

Python to achieve eight notes small game

Python implements jigsaw puzzles

Python to achieve skiing games

Preface:

In this issue we will make a copy of the “classic 90 tank war” small game. Forget the nonsense without further ado, let’s begin happily

rendering

The development tools

Python version: 3.6.4 Related modules: PyGame module; And some of the modules that come with Python.

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

The principle is introduced

T_T feels that the overall logic of the code is clear, and there are a lot of necessary comments, so I’ll just cover the main ideas here. Please read my source code for implementation details.

Game rules:

The game has two modes of single player and double player. If your stronghold is destroyed or your tank is destroyed, the game will fail. If you successfully pass all the levels, the game will win. In addition, the player can shoot certain tanks to make a random item appear on the map, and if your tank picks it up, it triggers an event, such as an increase in the tank’s ability.

The player can do it as follows:

Player a:

WSAD key: up and down, left and right;

Space bar: shoot.

Player 2:

↑↓←→ key: up and down around;

Keyboard 0 key: shooting.

Step by step:

Step1: Define the Sprite class

Since the game definitely involves collision detection, we need to define some Sprite classes.

First of all, since this is a tank war, there must be tanks, right?

Your tanks:

The code above defines some attributes of the tank, such as speed, level, whether it is protected or not, and so on.

Of course, there is also an instance of a bullet class, which we will define later. Pretend to have the bullet class first, so that the main logic is complete. Otherwise, how can tanks shoot without bullets?

Of course, it’s not enough to have stats, we also need to give the tank some abilities, such as shooting above:

Of course, there are also moves up and down, left and right, because they are all similar, here is only the source code for moving up:

Ah, and tank level upgrades and demotions:

Finally, of course, tanks reset after death:

Enemy tank:

The source code for the enemy tank definition is similar to that of your own tank, except that its movement is random and it cannot be resurrected after death.

Now we can define bullets!

The bullet class:

Bullets should have attributes such as speed and strength, as well as the ability to choose direction and move:

Finally, let’s define the other object classes involved in collision detection.

Base:

There are two states: normal and destroyed:

Map obstacles:

Including brick walls, steel walls, forests, rivers, and ice:

Food props:

There are 7 items in total, and different items have different effects:

Step2: Design the game map

Emmmm, the big background of the game is black, and then the map design is completed by piling on some obstacles defined in Step 1. – steel walls can’t be destroyed by normal bullets, brick walls can be destroyed by any bullets, tanks can go through any obstacle except walls, but there is no additional effect:

Here I was lazy and only designed one map and two levels. If you are interested, you can also design more maps and levels on this basis.

Step3: Implement the main loop of the game

The code for the main loop is longer, but the logic is clear. First display the game start interface, the player in this interface to select the game mode to enter the game; In the game, a series of collision detection and a series of events triggered by the collision, and the current existence of all objects drawn; Finally, if the game failed, it will display the game failure interface, if the clearance, it will display the game success interface, because the code is too long to screenshot

All Done!

This is the end of the article, thank you for watching, Python24 mini games series, the next article imitates “FlappyBird” mini games

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Dry goods mainly include:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

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

⑤Python Learning Roadmap (Goodbye to Slow Learning)

⑥ Two days of Python crawler training camp live access

All done~ thumb up + Comments ~ See bio or private message for complete source code.