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
Preface:
This issue we will make a copy of the “classic 90 tank wars” small game. Forget the nonsense, let’s start happily ~
rendering
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
T_T feels that the overall logic of his code is very clear, and has made a lot of necessary comments, so I will only talk about the main idea here, some implementation details please read my source code.
Rules of the game:
The game has single player and two player two modes. If your base camp is broken or your tank is annihilated, the game will fail. If you successfully pass all levels, the game will win. In addition, the player can make a random item appear on the map by shooting a specific tank, and if your tank picks up the item, it triggers an event, such as an increase in the tank’s ability.
Here’s what the player can do:
Player a:
Wsad key: up, down, around;
Space bar: shooting.
Player 2:
↑↓←→ Key: up and down left and right;
Keypad key 0: shoot.
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 it’s a battle of tanks, there have to be tanks, right?
Your own tanks:
The above code defines the attributes of the tank, such as speed, level, whether it is protected, and so on.
Of course, there is also a bullet class instantiated here, which we will define later, but pretend to have this bullet class so that the main logic is complete, otherwise how can the tank shoot without bullets?
Of course, it’s not enough to have stats, we also need to give tanks some abilities, such as shooting:
Of course, there are also moves up and down, left and right, because they are similar, here is only the source code for moving up:
Ah, and the tanks’ level ups and downs:
Finally, of course, the tank is reset after death:
Enemy tanks:
The source code for the definition of enemy tanks is similar to that of your own tanks, except that their movements are random and they do not reanimate after death.
Now we can define the bullet class!
The bullet class:
Bullets should have attributes such as speed, strength, and the ability to choose direction and movement:
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 items:
There are seven items, and each item has a different effect:
Step2: design the game map
Emmmm, the big background of the game is black, and the map design is completed by stacking the obstacles defined in Step 1 on top of it. The steel wall can’t be broken by ordinary bullets, the brick wall can be broken by any bullets, except for the wall, the tank can go through any obstacle, but there is no additional effect.
I was lazy and only designed one map and two levels, but interested partners can also design more maps and levels based on this.
Step3: implement the main game loop
The code for the main loop is longer, but the logic is clear. First of all, the game start interface is displayed, where players select the game mode and enter the game. In the game, you need to do a series of collision detection and trigger a series of events resulting from the collision, and draw all the objects that currently exist; Finally, if the game fails, the game failure interface will be displayed; if the game is finished, the game success interface will be displayed. Because the code is too long, we cannot take screenshots
All Done!
That’s the end of the article, thanks for watching, Python24 mini games series, the next article is a mini game based on “FlappyBird”
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 complete source code.