Introduction:

Today is a day to recall my childhood, the article game code small prepared quite a lot also, and so when I have time to sort out the notes to put it!!

Tank Wars was one of the most popular games in the world.

Despite its simple graphics, the game has all the ingredients of classic success

Easy to get started, upgraded, levels rich, you can play doubles, and even the conscience of the editing mode.

Although it is a low K game, but in the eyes of the People at that time, this game bought very valuable!

Today xiaobian with you to write a classic version of the tank war game project, look forward to the ~

The body of the

** (1) Prepare a lot of picture material classification processing: ** The following is part of the screenshots.

​​

** (2) Different musical backgrounds: ** When the familiar background music is playing, the whole childhood will come to you!

​​

(3) Game Rules:

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

​​

Here’s how you do it:

Player a:

Wsad key: up and down around;

Space bar: shoot.

Player 2:

↑↓←→ Key: up or down, left or right;

Small keyboard 0 key: shooting.

If tank_player1.num_lifes >= 0: if key_pressed[pygame.k_w]: player_tanks_group.remove(tank_player1) tank_player1.move('up', self.scene_elems, player_tanks_group, enemy_tanks_group, home) player_tanks_group.add(tank_player1) elif key_pressed[pygame.K_s]: player_tanks_group.remove(tank_player1) tank_player1.move('down', self.scene_elems, player_tanks_group, enemy_tanks_group, home) player_tanks_group.add(tank_player1) elif key_pressed[pygame.K_a]: player_tanks_group.remove(tank_player1) tank_player1.move('left', self.scene_elems, player_tanks_group, enemy_tanks_group, home) player_tanks_group.add(tank_player1) elif key_pressed[pygame.K_d]: player_tanks_group.remove(tank_player1) tank_player1.move('right', self.scene_elems, player_tanks_group, enemy_tanks_group, home) player_tanks_group.add(tank_player1) elif key_pressed[pygame.K_SPACE]: bullet = tank_player1.shoot() if bullet: self.sounds['fire'].play() if tank_player1.tanklevel < 2 else self.sounds['Gunfire'].play() Player_bullets_group. add(bullet) # if self.is_dual_mode and (tank_player2.num_lifes >= 0): if key_pressed[pygame.K_UP]: player_tanks_group.remove(tank_player2) tank_player2.move('up', self.scene_elems, player_tanks_group, enemy_tanks_group, home) player_tanks_group.add(tank_player2) elif key_pressed[pygame.K_DOWN]: player_tanks_group.remove(tank_player2) tank_player2.move('down', self.scene_elems, player_tanks_group, enemy_tanks_group, home) player_tanks_group.add(tank_player2) elif key_pressed[pygame.K_LEFT]: player_tanks_group.remove(tank_player2) tank_player2.move('left', self.scene_elems, player_tanks_group, enemy_tanks_group, home) player_tanks_group.add(tank_player2) elif key_pressed[pygame.K_RIGHT]: player_tanks_group.remove(tank_player2) tank_player2.move('right', self.scene_elems, player_tanks_group, enemy_tanks_group, home) player_tanks_group.add(tank_player2) elif key_pressed[pygame.K_KP0]: bullet = tank_player2.shoot() if bullet: player_bullets_group.add(bullet) self.sounds['fire'].play() if tank_player2.tanklevel < 2 else self.sounds['Gunfire'].play()Copy the code

Loading game material:

​​

The attached:

Sounds = {} for key, value in cfg.audio_paths.items (): sounds[key] = pygame.mixer.Sound(value) sounds[key].set_volume(1)Copy the code

(4) There must be a collision when the tank is moving, hitting the opponent, etc. :

Collision detection in the # # -- bullets and brick wall pygame. Sprite. Groupcollide (player_bullets_group, self. Scene_elems. Get (' brick_group), True, True) pygame.sprite.groupcollide(enemy_bullets_group, self.scene_elems.get('brick_group'), True, True) # -- Bullet and Iron Wall for bullet in player_bullets_group: if pygame.sprite.spritecollide(bullet, self.scene_elems.get('iron_group'), bullet.is_stronger, None): player_bullets_group.remove(bullet) pygame.sprite.groupcollide(enemy_bullets_group, self.scene_elems.get('iron_group'), True, False) # -- the bullet hit the bullet pygame. Sprite. Groupcollide (player_bullets_group enemy_bullets_group, True, True) # -- Our bullets hit enemy tanks: if pygame.sprite.spritecollide(tank, player_bullets_group, True, None): if tank.food: foods_group.add(tank.food) tank.food = None if tank.decreaseTankLevel(): Self.sounds ['bang'].play() self.total_enemy_num -= 1 # if pygame.sprite.spritecollide(tank, enemy_bullets_group, True, None): if tank.is_protected: self.sounds['blast'].play() else: if tank.decreaseTankLevel(): self.sounds['bang'].play() if tank.num_lifes < 0: Player_tanks_group. Remove (tank) # -- our bullets hit our base camp if pygame. Sprite. Spritecollide (home, player_bullets_group, True, None) : Is_win = False is_running = False home. SetDead () # -- the enemy bullets hit our warehouse if pygame. Sprite. Spritecollide (home, enemy_bullets_group, True, None): is_win = False is_running = False home.setDead()Copy the code

(5) Randomly generated food in tank games:

# -- For player_tank in player_tanks_group: for food in foods_group: if pygame.sprite.collide_rect(player_tank, food): if food.name == 'boom': self.sounds['add'].play() for _ in enemy_tanks_group: self.sounds['bang'].play() self.total_enemy_num -= len(enemy_tanks_group) enemy_tanks_group = pygame.sprite.Group() elif  food.name == 'clock': self.sounds['add'].play() for enemy_tank in enemy_tanks_group: enemy_tank.setStill() elif food.name == 'gun': self.sounds['add'].play() player_tank.improveTankLevel() elif food.name == 'iron': self.sounds['add'].play() self.__pretectHome() elif food.name == 'protect': self.sounds['add'].play() player_tank.setProtected() elif food.name == 'star': self.sounds['add'].play() player_tank.improveTankLevel() player_tank.improveTankLevel() elif food.name == 'tank': self.sounds['add'].play() player_tank.addLife() foods_group.remove(food)Copy the code

Game effects:

conclusion

This game is a classic, I did not play the first pass, we explore.

To be honest, there is a lot of code, how much do you think tank battle will be restored?

Remember three even oh ~ love you!

Previous articles are welcome to read:

1. Idiom Solitaire game project. 2. Tower Defense game project. 3. Memorizing card flip items. 4. Pac Man mini game project.

5. Alien invasion game project. 6. Count your game items. 7. Brain exercise games. 8. Garbage classification mini game project.

9. Thunder Fighter Game project. 10. My Rabbit game project. 11. Eight-note game project. 12. Jigsaw puzzle project.

13. Ski games. 14. Desktop pet projects.

. Stay tuned for more at………..