Coding With Fun
Home Docker Django Node.js Articles FAQ

Python Makes Small Games (V)


Jun 01, 2021 Article blog



This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column

Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn

Lead

In this issue we will make a mini-game modelled on the "Classic 90 Tank Battle". A h, I remember the last time I played this game, I was in elementary school. T_T is really an age-revealing game.

Forget the nonsense and don't say much, let's start happily

Related documents

Baidu web download link: https://pan.baidu.com/s/1Rl6gvHTGfWxuU3KjWYWbBw

Password: eksw

The picture footage comes from the Internet, and it's apologetic and deleted.

Develop tools

Python version: 3.6.4

Related modules:

pygame module;

and some Python's own modules.

Environment construction

Install Python and add it to the environment variable, and pip installs the relevant modules that are required.

Take a quick look


Introduction to the principle

T_T feel that the overall logic of my code is still very clear, but also made a lot of necessary comments, so here I only talk about the main ideas, some implementation details please read my source code.

Rules of the game:

The game has two modes of single and double, their own base camp is broken or their own tanks are annihilated then the game failed, successfully through the relevant card game victory. I n addition, players can randomly appear a prop on the map by shooting a specific tank, triggering an event, such as an increase in tank capability, if the tank picks up the prop.

The player operates as follows:

Player One:

wsad key: up and down around;

Space bar: Shoot.

Player 2:

the key ←→: up and down left and right;

Keypad 0 keys: Shoot.

Step-by-step:

Step1: Define the elf class

Because the game definitely involves collision detection, we need to define some elf classes.

First of all, since it's a tank war, you have to have a tank, right?

Own tanks:

 Python Makes Small Games (V)1

The code above defines some of the tank's properties, such as speed, rank, whether it is protected, and so on.

Of course, here also instantiated a bullet class, this we then define, first pretend to have this bullet class, so that the main logic is complete, otherwise the tank does not have bullet class how to shoot?

Of course, having attributes is not enough, and we have to give the tank some capabilities, such as the shooting mentioned above:

 Python Makes Small Games (V)2

Of course there are up and down movements, because they are all similar, here is only the source code for upward movement:

 Python Makes Small Games (V)3

Ah, and tanks are upgraded and dropped:

 Python Makes Small Games (V)4

Finally, of course, the tank reset after death:

 Python Makes Small Games (V)5

Enemy tanks:

Enemy tanks and their own tank definition of the source code is very similar, but the movement is random, after death is unreturnable, do not take screenshots of the T_T.

Now we can define the bullet class!

Bullets:

Bullet classes should have attributes such as speed, strength, and the ability to select directions and movements:

 Python Makes Small Games (V)6

Finally, let's define other classes of objects that involve collision detection.

base camp:

There are two states: normal and destroyed:

 Python Makes Small Games (V)7

Map obstacles:

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

 Python Makes Small Games (V)8

Food items:

There are 7 kinds of props, different props corresponding to different effects:

 Python Makes Small Games (V)9

Step2: Design game maps

Emmmm, the big background of the game is black, and then on top of some of the obstacles defined in step one can complete the map design. Among them, the steel wall can not be broken by ordinary bullets, brick walls can be broken by any bullet, in addition to the wall, tanks can pass through any obstacle, but there is no additional effect (interested partners can expand their own - such as the speed of the tank on the ice, etc.):

 Python Makes Small Games (V)10

I'm lazy to design only one map and two levels, and interested partners can also design more maps and levels on that basis.

Step3: Implement the main loop of the game

The code for the main loop is long, but the logic is clear. First show the game start interface, the player in this interface to select the game mode to enter the game, in the game, need to carry out a series of collision detection and trigger collision generated by a series of events, and draw all the objects that currently exist, and finally, if the game fails, then show the game failure interface, if customs clearance, show the game success interface (I am lazy, the interface design is relatively simple, interested partners can expand.

Here's a screenshot of the code (the screenshot is more troublesome T_T)

All Done!

Complete source code and game materials as well as packaged game files are in the relevant files, self-download to view


more

Code as of 2018-07-19 is correct (T_T I haven't actually tested all the features, so if there's a bug you can tell me in a private letter that I want to fix it).

Interested partners are also welcome to further expand the game.