Python implements tank battle (with source code)

Tank battle mini game based on pygame

Table of contents

Tank battle mini game based on pygame

1: Development environment

2. Project introduction

        2.1 Complete functions:

        2.2 Project screenshots:

        2.3 Project files

1: Development environment

Let’s take a look at my development environment first. The things I use are relatively simple:

Author: Duzhou yyds

Time: December 3, 2022

Integrated development tools: PyCharm 2021.1.3

Integrated development environment: python 3.10.6

Third-party modules: pygame, pygame_menu, Image, ImageSequence

2. Project introduction

        2.1 Complete functions:

  1. Menu selection: Users can make selections according to the menu, which improves the interactivity of the program.
  2. Multiple modes: Provides level mode (with 35 levels), endless mode, duel mode, and construction mode.
  3. Map editing: Users can freely edit the map using the keyboard, making it more interesting for players.
  4. Simple enemy AI: Enemies have simple AI behaviors, such as firing bullets, walking, etc.
  5. Animation effects: explosion effect of tanks when attacked; simple animation of enemy tanks resurrecting.
  6. Game loop: Unlike ordinary software, the game needs to use a main loop to update the scene state and redraw the screen. This program refreshes at a frame rate of 60FPS

        2.2 Project screenshots:

The main program entrance is in the main.py file, which can be run directly after installing pygame, pygame_menu and other modules. Below is a running screenshot:

The following is a screenshot of the official start of the game:

        2.3 Project files

(1)image、init、music

image is the image resource required by the project, init is the effect image resource when the project is loaded, and music is the music resource required by the project. Batch modifying image size and parsing GIF are two tools for temporary use and will not be explained.

(2)main.py

Main.py is the main entry of the project. Mainly for loading the initial interface menu. Create a game_loader object, and then run the corresponding method according to the mode selected in the initial interface. The following is the general framework of main.py:

(3)game_loader.py

The main module of our Tank War game is written in game_loder.py, and the TankWar class inside defines all the behaviors of the main game. Including initialization screen, initializing pygame module, creating enemy tanks, drawing maps, detecting collisions, monitoring events, etc.

The following is the general framework of game_loder.py:

(4) Other modules are object descriptions

bulletClass.py: This module is a description of the bullet class

enemyTank.py: This module is a description of the enemy tank class


 food.py: This module is a description of props (rewards carried by enemy tanks)

map_loader.py and maps.py are both map loading modules

 special_effects.py: module for special effects (explosion, protective cover, etc.)

 wall.py: This module describes map elements (bricks, stones, etc.)

 Because there is a lot of code, the code will not be introduced here. The game still has many shortcomings and will be updated in the future. The project will be uploaded to GitHub soon, everyone is welcome to fork. That’s it for today~

 Project address: 0911duzhou/python- · GitHub

 Pygame document address: https://www.pygame.org/docs/

Guess you like

Origin blog.csdn.net/zzp20031120/article/details/128167336