Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make tank games with Python

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you how to use Python to make tank games. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Development tools

Python version: 3.6.4

Related modules:

Pygame module

And some modules that come with Python.

Environment building

Install Python and add it to the environment variable, and pip installs the relevant modules you need.

Introduction of principle

Taper feels that the logic of its code as a whole is very clear and has made a lot of necessary comments, so I will only talk about the main ideas here.

Rules of the game:

There are two modes of the game: single player and double player. If your own stronghold is broken or your tank is annihilated, the game will fail, and if you successfully pass all the levels, the game will win. In addition, players can make a prop appear randomly on the map by shooting a specific tank, and if their own tank picks up the prop, it will trigger an event, such as an enhancement of the tank's ability.

The way players operate is as follows:

Player 1:

Wsad key: up and down

Space bar: shooting.

Player 2:

↑↓←→ key: up and down

Keypad 0 key: shooting.

Step by step:

Step1: define sprite classes

Because the game must involve collision detection, we need to define some elves.

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

Own tanks:

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

Of course, there is also an instantiation of a bullet class, which we will define later, first pretending to have this bullet class, so that the main logic is complete, otherwise how can a tank shoot without a bullet class?

Of course, having attributes is not enough, we also need to give the tank some abilities, such as the shooting mentioned above:

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

Ah, and the level rise and fall of tanks:

Finally, of course, the tank was reset after death:

Enemy tanks:

The source code of the definition of enemy tank and own tank is very similar, except that the movement is random and cannot be revived after death, so there is no screenshot.

Now, we can define bullets!

Bullets:

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

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

Base camp:

There are two states: normal and destroyed:

Map obstacles:

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

Food props:

There are seven kinds of props, and different props correspond to different effects:

Step2: designing game maps

Emmmm, the background of the game is black, and then you can complete the map design by stacking some of the obstacles defined in step 1. Among them, the steel wall can not be broken by ordinary bullets, the brick wall can be broken by any bullet, except the wall, the tank can pass through any obstacle, but there is no additional effect (interested partners can expand themselves ~ such as the speed of the tank on the ice, etc.):

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

Step3: realize the main cycle of the game

The code of the main loop is long, but the logic is clear. First of all, show the game start interface, in which the player chooses the game mode and then enters the game; in the game, it needs to carry out a series of collision detection and trigger a series of events generated by the collision, and draw all the existing objects; finally, if the game fails, the game failure interface is displayed, and if the customs clearance is cleared, the game success interface is displayed.

These are all the contents of the article "how to make Tank Games with Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report