Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Pygame Tank Battle Game

Shulou Source: shulou.com Published: 2022-06-01 15:39:54 09月30日 Update

This article will explain in detail how to implement the Pygame tank war game. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Text 1) rules of the game:

The process of the game is like this, players operate tanks to destroy computer-controlled tanks and protect their bases. The base icon is an arrogant eagle with wings. Since I was a child

Tragedies that have mistakenly blown a flying eagle into a roast chicken often occur.

< 1 and enemytanks_now < 1: is_gameover = False break for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == genEnemyEvent: if enemytanks_total >

0: if enemytanks_now

< enemytanks_now_max: enemytank = tanks.enemyTank() if not pygame.sprite.spritecollide(enemytank, tanksGroup, False, None): tanksGroup.add(enemytank) enemytanksGroup.add(enemytank) enemytanks_now += 1 enemytanks_total -= 1 if event.type == recoverEnemyEvent: for each in enemytanksGroup: each.can_move = True if event.type == noprotectMytankEvent: for each in mytanksGroup: mytanksGroup.protected = False # 检查用户键盘操作 key_pressed = pygame.key.get_pressed() # 玩家一 # WSAD ->

Up and down # the space bar shoots if key_ preset [pygame.kw]: tanksGroup.remove (tank_player1) tank_player1.move_up (tanksGroup, map_stage.brickGroup, map_stage.ironGroup) Myhome) tanksGroup.add (tank_player1) player1_moving = True elif key_ preset [pygame.K _ s]: tanksGroup.remove (tank_player1) tank_player1.move_down (tanksGroup, map_stage.brickGroup, map_stage.ironGroup Myhome) tanksGroup.add (tank_player1) player1_moving = True elif key_ preset [pygame.K _ a]: tanksGroup.remove (tank_player1) tank_player1.move_left (tanksGroup, map_stage.brickGroup, map_stage.ironGroup Myhome) tanksGroup.add (tank_player1) player1_moving = True elif key_ preset [pygame.K _ d]: tanksGroup.remove (tank_player1) tank_player1.move_right (tanksGroup, map_stage.brickGroup, map_stage.ironGroup Myhome) tanksGroup.add (tank_player1) player1_moving = True elif key_ preset [pygame.K _ SPACE]: if not tank_player1.bullet.being: fire_sound.play () tank_player1.shoot () # player 2 # ↑ ↓←→-> up, down, left and right # Keyboard 0 shooting if num_player > 1: if key_ preset [pygame.K _ UP]: tanksGroup.remove (tank_player2) tank_player2.move_up (tanksGroup Map_stage.brickGroup, map_stage.ironGroup, myhome) tanksGroup.add (tank_player2) player2_moving = True elif key_ preset [pygame.K _ DOWN]: tanksGroup.remove (tank_player2) tank_player2.move_down (tanksGroup, map_stage.brickGroup, map_stage.ironGroup Myhome) tanksGroup.add (tank_player2) player2_moving = True elif key_ preset [pygame.K _ LEFT]: tanksGroup.remove (tank_player2) tank_player2.move_left (tanksGroup, map_stage.brickGroup, map_stage.ironGroup Myhome) tanksGroup.add (tank_player2) player2_moving = True elif key_ preset [pygame.K _ RIGHT]: tanksGroup.remove (tank_player2) tank_player2.move_right (tanksGroup, map_stage.brickGroup, map_stage.ironGroup Myhome) tanksGroup.add (tank_player2) player2_moving = True elif key_ preset [pygame.K _ KP0]: if not tank_player2.bullet.being: fire_sound.play () tank_player2.shoot () # Background screen.blit (bg_img (0,0)) # Stone Wall for each in map_stage.brickGroup: screen.blit (each.brick, each.rect) # Steel Wall for each in map_stage.ironGroup: screen.blit (each.iron Each.rect) # Ice for each in map_stage.iceGroup: screen.blit (each.ice, each.rect) # River for each in map_stage.riverGroup: screen.blit (each.river Each.rect) # Tree for each in map_stage.treeGroup: screen.blit (each.tree Each.rect) time + = 1 if time = = 5: time = 0 is_switch_tank = not is_switch_tank # our tank if tank_player1 in mytanksGroup: if is_switch_tank and player1_moving: screen.blit (tank_player1.tank_0, (tank_player1.rect.left) Tank_player1.rect.top)) player1_moving = False else: screen.blit (tank_player1.tank_1, (tank_player1.rect.left, tank_player1.rect.top)) if tank_player1.protected: screen.blit (tank_player1.protected_mask1, (tank_player1.rect.left) Tank_player1.rect.top)) if num_player > 1: if tank_player2 in mytanksGroup: if is_switch_tank and player2_moving: screen.blit (tank_player2.tank_0, (tank_player2.rect.left) Tank_player2.rect.top)) player1_moving = False else: screen.blit (tank_player2.tank_1, (tank_player2.rect.left, tank_player2.rect.top)) if tank_player2.protected: screen.blit (tank_player1.protected_mask1, (tank_player2.rect.left) Tank_player2.rect.top)) # enemy tank for each in enemytanksGroup: # birth special effects if each.born: if each.times > 0: each.times-= 1 if each.times

Tags: Tanks bullets enemies wars levels interfaces events big books base effects collisions codes bases quantity clocks modules articles can move up and down up and down Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Microsoft Huawei MySQL