Article \ | wild guest

Source: Python Technology “ID: pythonall”

Tanks Wars is a strategy flat shooter released by Namco games in 1985. Despite the fact that there have been many spin-off games, the game is still popular with a large number of people. In this article, we’ll take a look at how to implement it in Python. The main Python library used for the game implementation is PyGame.

Introduction to the

Tank war consists of scenes, tanks, bullets, food and base camp. In essence, it is a tower defense game. The goal of the game is to defend the base camp and destroy enemy tanks.

implementation

First of all, let’s realize the game scene, the scene mainly includes: stone wall, steel wall, ice, river, tree, map, we temporarily do two levels, the code implementation is as follows:

Class Brick(pygame.sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.brick = pygame.image.load('images/scene/brick.png') self.rect = self.brick.get_rect() self.being = False # def __init__(self): pygame.sprite.Sprite.__init__(self) self.iron = pygame.image.load('images/scene/iron.png'Self.rect = self.iron.get_rect() self.being = False # def __init__(self): pygame.sprite.Sprite.__init__(self) self.ice = pygame.image.load('images/scene/ice.png') self.rect = self.ice.get_rect() self.being = False # stream class River(pygame.sprite.sprite): def __init__(self, kind=None): pygame.sprite.Sprite.__init__(self)if kind is None:
   self.kind = random.randint(0.1)
  self.rivers = ['images/scene/river1.png'.'images/scene/river2.png'] self. River = pygame.image.load(self. Rivers [self. Kind]) self Tree(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.tree = pygame.image.load('images/scene/tree.png') self.rect = self.tree.get_rect() self.being = False # Map class Map(): def __init__(self, stage): self.brickGroup = pygame.sprite.Group() self.ironGroup = pygame.sprite.Group() self.iceGroup = pygame.sprite.Group() self.riverGroup = pygame.sprite.Group() self.treeGroup = pygame.sprite.Group()if stage == 1:
   self.stage1()
  elif stage == 2: self.stage2() # def stage1(self):for x in [2.3.6.7.18.19.22.23] :for y in [2.3.4.5.6.7.8.9.10.17.18.19.20.21.22.23]:
    self.brick = Brick()
    self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
    self.brick.being = True
    self.brickGroup.add(self.brick)
  for x in [10.11.14.15] :for y in [2.3.4.5.6.7.8.11.12.15.16.17.18.19.20]:
    self.brick = Brick()
    self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
    self.brick.being = True
    self.brickGroup.add(self.brick)
  for x in [4.5.6.7.18.19.20.21] :for y in [13.14]:
    self.brick = Brick()
    self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
    self.brick.being = True
    self.brickGroup.add(self.brick)
  for x in [12.13] :for y in [16.17]:
    self.brick = Brick()
    self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
    self.brick.being = True
    self.brickGroup.add(self.brick)
  for x, y in [(11.23), (12.23), (13.23), (14.23), (11.24), (14.24), (11.25), (14.25)]:
   self.brick = Brick()
   self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
   self.brick.being = True
   self.brickGroup.add(self.brick)
  for x, y in [(0.14), (1.14), (12.6), (13.6), (12.7), (13.7), (24.14), (25.14)]:
   self.iron = Iron()
   self.iron.rect.left, self.iron.rect.top = 3 + x * 24.3 + y * 24Self.iron. Being = True self.irongroup. add(self.iron) # def stage2(self):for x in [2.3.6.7.18.19.22.23] :for y in [2.3.4.5.6.7.8.9.10.17.18.19.20.21.22.23]:
    self.brick = Brick()
    self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
    self.brick.being = True
    self.brickGroup.add(self.brick)
  for x in [10.11.14.15] :for y in [2.3.4.5.6.7.8.11.12.15.16.17.18.19.20]:
    self.brick = Brick()
    self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
    self.brick.being = True
    self.brickGroup.add(self.brick)
  for x in [4.5.6.7.18.19.20.21] :for y in [13.14]:
    self.brick = Brick()
    self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
    self.brick.being = True
    self.brickGroup.add(self.brick)
  for x in [12.13] :for y in [16.17]:
    self.brick = Brick()
    self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
    self.brick.being = True
    self.brickGroup.add(self.brick)
  for x, y in [(11.23), (12.23), (13.23), (14.23), (11.24), (14.24), (11.25), (14.25)]:
   self.brick = Brick()
   self.brick.rect.left, self.brick.rect.top = 3 + x * 24.3 + y * 24
   self.brick.being = True
   self.brickGroup.add(self.brick)
  for x, y in [(0.14), (1.14), (12.6), (13.6), (12.7), (13.7), (24.14), (25.14)]:
   self.iron = Iron()
   self.iron.rect.left, self.iron.rect.top = 3 + x * 24.3 + y * 24
   self.iron.being = True
   self.ironGroup.add(self.iron)
 def protect_home(self):
  for x, y in [(11.23), (12.23), (13.23), (14.23), (11.24), (14.24), (11.25), (14.25)]:
   self.iron = Iron()
   self.iron.rect.left, self.iron.rect.top = 3 + x * 24.3 + y * 24
   self.iron.being = True
   self.ironGroup.add(self.iron)
Copy the code

We then look at the implementation of the base camp, the implementation of the base camp is relatively simple, its essence is a marker, code implementation is as follows:

class Home(pygame.sprite.Sprite):
 def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.homes = ['images/home/home1.png'.'images/home/home2.png'.'images/home/home_destroyed.png']
  self.home = pygame.image.load(self.homes[0])
  self.rect = self.home.get_rect()
  self.rect.left, self.rect.top = (3 + 12 * 24.3 + 24 * 24Def set_dead(self): self. Home = pygame.image.load(self. Homes [)- 1])
  self.alive = False
Copy the code

Then look at the realization of food, food is mainly used to improve tank ability, such as: tank upgrade, increase life, etc., the code is as follows:

Class Food(pygame.sprite.sprite): def __init__(self): Pygame.sprite.__init__ (self) # Destroy all current enemies self.food_boom ='images/food/food_boom.png'# Current enemies stand still for a period of time self.food_clock ='images/food/food_clock.png'# Make tank bullets breakable plate self.food_gun ='images/food/food_gun.png'# Change the citadel walls to steel self.food_iron ='images/food/food_gun.png'# Tanks gain protection from self.food_protect = for a period of time'images/food/food_protect.png'# Tank upgrade self.food_star ='images/food/food_star.png'# Tank Life +1
  self.food_tank = 'images/food/food_tank.png'# all foods = [self. Food_boom, self. Food_clock, self. Food_gun, self. Self.food_tank] self.kind = None self.food = None self.rect = None # self. Being = False # Self1000Def generate(self): self. Kind = random. Randint (self.0.6)
  self.food = pygame.image.load(self.foods[self.kind]).convert_alpha()
  self.rect = self.food.get_rect()
  self.rect.left, self.rect.top = random.randint(100.500), random.randint(100.500)
  self.being = True
Copy the code

Then look at the realization of tanks, tanks including our tanks and enemy tanks, our tanks by the player’s own control of movement, shooting and other operations, enemy tanks to achieve automatic movement, shooting and other operations, the code is as follows:

# def myTank(pygame.sprite.sprite): def __init__(self, player): pygame.sprite.sprite.__init__ (self) # myTank(pygame.sprite.sprite): def __init__(self) : pygame.sprite.sprite.1/2Self. player = player # Use different tanks for different players (different levels correspond to different maps)if player == 1:
   self.tanks = ['images/myTank/tank_T1_0.png'.'images/myTank/tank_T1_1.png'.'images/myTank/tank_T1_2.png']
  elif player == 2:
   self.tanks = ['images/myTank/tank_T2_0.png'.'images/myTank/tank_T2_1.png'.'images/myTank/tank_T2_2.png']
  else:
   raise ValueError('myTank class -> player value error.'# Tank level (initial0)
  self.level = 0Tank = pygame.image.load(self.tanks[self.level]).convert_alpha() self.tank_0 = self.tank.subsurface((0.0), (48.48))
  self.tank_1 = self.tank.subsurface((48.0), (48.48) self.rect = self.tank_0.get_rect() # self.protected_mask = pygame.image.load()'images/others/protect.png').convert_alpha()
  self.protected_mask1 = self.protected_mask.subsurface((0.0), (48.48))
  self.protected_mask2 = self.protected_mask.subsurface((48.0), (48.48Self.direction_x, self.direction_y =0.- 1# Different players are born in different positionsif player == 1:
   self.rect.left, self.rect.top = 3 + 24 * 8.3 + 24 * 24
  elif player == 2:
   self.rect.left, self.rect.top = 3 + 24 * 16.3 + 24 * 24
  else:
   raise ValueError('myTank class -> player value error.') # Tank speed self.speed =3Self. Being = True # How many lives does self.life =3Self.protected = False # bullet self.bullet = bullet () # def shoot(self): self.bullet.being = True self.bullet.turn(self.direction_x, self.direction_y)if self.direction_x == 0 and self.direction_y == - 1:
   self.bullet.rect.left = self.rect.left + 20
   self.bullet.rect.bottom = self.rect.top - 1
  elif self.direction_x == 0 and self.direction_y == 1:
   self.bullet.rect.left = self.rect.left + 20
   self.bullet.rect.top = self.rect.bottom + 1
  elif self.direction_x == - 1 and self.direction_y == 0:
   self.bullet.rect.right = self.rect.left - 1
   self.bullet.rect.top = self.rect.top + 20
  elif self.direction_x == 1 and self.direction_y == 0:
   self.bullet.rect.left = self.rect.right + 1
   self.bullet.rect.top = self.rect.top + 20
  else:
   raise ValueError('myTank class -> direction value error.')
  if self.level == 0:
   self.bullet.speed = 8
   self.bullet.stronger = False
  elif self.level == 1:
   self.bullet.speed = 12
   self.bullet.stronger = False
  elif self.level == 2:
   self.bullet.speed = 12
   self.bullet.stronger = True
  elif self.level == 3:
   self.bullet.speed = 16
   self.bullet.stronger = True
  else:
   raise ValueError('myTank class -> level value error.'Def up_level(self):if self.level < 3:
   self.level += 1
  try:
   self.tank = pygame.image.load(self.tanks[self.level]).convert_alpha()
  except:
   self.tank = pygame.image.load(self.tanks[- 1].convert_alpha() # def down_level(self):if self.level > 0:
   self.level -= 1Tank = pygame.image.load(self.tanks[self.level]).convert_alpha() # def def move_up(self, tankGroup, brickGroup, tankGroup) ironGroup, myhome): self.direction_x, self.direction_y =0.- 1Self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y) self.tank_0 = self.tank.subsurface((0.0), (48.48))
  self.tank_1 = self.tank.subsurface((48.0), (48.48Is_move = True # top of mapif self.rect.top < 3: self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = Falseifpygame.sprite.spritecollide(self, brickGroup, False, None) or \ pygame.sprite.spritecollide(self, ironGroup, False, None): self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = False # crash other tanksifpygame.sprite.spritecollide(self, tankGroup, False, None): Self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = False #if pygame.sprite.collide_rect(self, myhome):
   self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
   is_move = False
  returnDef move_down(self, tankGroup, brickGroup, ironGroup, myHOME): self.direction_x, self.direction_y =0.1Self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y) self.tank_0 = self.tank.subsurface((0.48), (48.48))
  self.tank_1 = self.tank.subsurface((48.48), (48.48Is_move = True # bottom of mapif self.rect.bottom > 630 - 3: self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = Falseifpygame.sprite.spritecollide(self, brickGroup, False, None) or \ pygame.sprite.spritecollide(self, ironGroup, False, None): self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = False # crash other tanksifpygame.sprite.spritecollide(self, tankGroup, False, None): Self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = False #if pygame.sprite.collide_rect(self, myhome):
   self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
   is_move = False
  returnIs_move # def move_left(self, tankGroup, brickGroup, ironGroup, myHOME): self.direction_x, self.direction_y =- 1.0Self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y) self.tank_0 = self.tank.subsurface((0.96), (48.48))
  self.tank_1 = self.tank.subsurface((48.96), (48.48Is_move = True # left end of mapif self.rect.left < 3: self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = Falseifpygame.sprite.spritecollide(self, brickGroup, False, None) or \ pygame.sprite.spritecollide(self, ironGroup, False, None): self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = False # crash other tanksifpygame.sprite.spritecollide(self, tankGroup, False, None): Self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = False #if pygame.sprite.collide_rect(self, myhome):
   self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
   is_move = False
  returnDef move_right(self, tankGroup, brickGroup, ironGroup, myHOME): self.direction_x, self.direction_y =1.0Self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y) self.tank_0 = self.tank.subsurface((0.144), (48.48))
  self.tank_1 = self.tank.subsurface((48.144), (48.48Is_move = True # right side of mapif self.rect.right > 630 - 3: self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = Falseifpygame.sprite.spritecollide(self, brickGroup, False, None) or \ pygame.sprite.spritecollide(self, ironGroup, False, None): self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = False # crash other tanksifpygame.sprite.spritecollide(self, tankGroup, False, None): Self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y) is_move = False #if pygame.sprite.collide_rect(self, myhome):
   self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
   is_move = False
  returnIs_move # resets def reset(self): self.level =0
  self.protected = False
  self.tank = pygame.image.load(self.tanks[self.level]).convert_alpha()
  self.tank_0 = self.tank.subsurface((0.0), (48.48))
  self.tank_1 = self.tank.subsurface((48.0), (48.48))
  self.rect = self.tank_0.get_rect()
  self.direction_x, self.direction_y = 0.- 1
  if self.player == 1:
   self.rect.left, self.rect.top = 3 + 24 * 8.3 + 24 * 24
  elif self.player == 2:
   self.rect.left, self.rect.top = 3 + 24 * 16.3 + 24 * 24
  else:
   raise ValueError('myTank class -> player value error.')
  self.speed = 3Def __init__(self, x=None, kind=None, is_red=None): def __init__(self, x=None, kind=None, is_red=None): Pygame.sprite.sprite.__init__ (self) # Used to play the birth effect self.born = True self.times = for the newly generated tank90# Tank type numberif kind is None:
   self.kind = random.randint(0.3)
  else: self.kind = kind # all tanks self.tanks1 = ['images/enemyTank/enemy_1_0.png'.'images/enemyTank/enemy_1_1.png'.'images/enemyTank/enemy_1_2.png'.'images/enemyTank/enemy_1_3.png']
  self.tanks2 = ['images/enemyTank/enemy_2_0.png'.'images/enemyTank/enemy_2_1.png'.'images/enemyTank/enemy_2_2.png'.'images/enemyTank/enemy_2_3.png']
  self.tanks3 = ['images/enemyTank/enemy_3_0.png'.'images/enemyTank/enemy_3_1.png'.'images/enemyTank/enemy_3_2.png'.'images/enemyTank/enemy_3_3.png']
  self.tanks4 = ['images/enemyTank/enemy_4_0.png'.'images/enemyTank/enemy_4_1.png'.'images/enemyTank/enemy_4_2.png'.'images/enemyTank/enemy_4_3.png'[tanks = [self. Tanks1, self. Tanks2, self. Tanks3, self.if is_red is None:
   self.is_red = random.choice((True, False, False, False, False))
  else: self.is_red = is_red # Tanks of the same type have different colors, red tanks have more health than their peersif self.is_red:
   self.color = 3
  else:
   self.color = random.randint(0.2# Load (two tanks are for wheels) self.tank = pygame.image.load(self.tanks[self.kind][self.color]).convert_alpha() self.tank_0 = self.tank.subsurface((0.48), (48.48))
  self.tank_1 = self.tank.subsurface((48.48), (48.48)) self.rect = self.tank_0.get_rect() # tank positionif x is None:
   self.x = random.randint(0.2)
  else:
   self.x = x
  self.rect.left, self.rect.top = 3 + self.x * 12 * 24.3Self. can_move = True # Tank speed self.speed = Max (3 - self.kind, 1Self.direction_x, self.direction_y =0.1Self. Being = True # bullet = bullet () # def shoot(self): self.bullet.being = True self.bullet.turn(self.direction_x, self.direction_y)if self.direction_x == 0 and self.direction_y == - 1:
   self.bullet.rect.left = self.rect.left + 20
   self.bullet.rect.bottom = self.rect.top - 1
  elif self.direction_x == 0 and self.direction_y == 1:
   self.bullet.rect.left = self.rect.left + 20
   self.bullet.rect.top = self.rect.bottom + 1
  elif self.direction_x == - 1 and self.direction_y == 0:
   self.bullet.rect.right = self.rect.left - 1
   self.bullet.rect.top = self.rect.top + 20
  elif self.direction_x == 1 and self.direction_y == 0:
   self.bullet.rect.left = self.rect.right + 1
   self.bullet.rect.top = self.rect.top + 20
  else:
   raise ValueError('enemyTank class -> direction value error.'Def move(self, tankGroup, brickGroup, ironGroup, myhome): self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_y) is_move = Trueif self.direction_x == 0 and self.direction_y == - 1:
   self.tank_0 = self.tank.subsurface((0.0), (48.48))
   self.tank_1 = self.tank.subsurface((48.0), (48.48))
   if self.rect.top < 3:
    self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
    self.direction_x, self.direction_y = random.choice(([0.1], [0.- 1], [1.0], [- 1.0]))
    is_move = False
  elif self.direction_x == 0 and self.direction_y == 1:
   self.tank_0 = self.tank.subsurface((0.48), (48.48))
   self.tank_1 = self.tank.subsurface((48.48), (48.48))
   if self.rect.bottom > 630 - 3:
    self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
    self.direction_x, self.direction_y = random.choice(([0.1], [0.- 1], [1.0], [- 1.0]))
    is_move = False
  elif self.direction_x == - 1 and self.direction_y == 0:
   self.tank_0 = self.tank.subsurface((0.96), (48.48))
   self.tank_1 = self.tank.subsurface((48.96), (48.48))
   if self.rect.left < 3:
    self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
    self.direction_x, self.direction_y = random.choice(([0.1], [0.- 1], [1.0], [- 1.0]))
    is_move = False
  elif self.direction_x == 1 and self.direction_y == 0:
   self.tank_0 = self.tank.subsurface((0.144), (48.48))
   self.tank_1 = self.tank.subsurface((48.144), (48.48))
   if self.rect.right > 630 - 3:
    self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
    self.direction_x, self.direction_y = random.choice(([0.1], [0.- 1], [1.0], [- 1.0]))
    is_move = False
  else:
   raise ValueError('enemyTank class -> direction value error.')
  if pygame.sprite.spritecollide(self, brickGroup, False, None) \
   or pygame.sprite.spritecollide(self, ironGroup, False, None) \
   or pygame.sprite.spritecollide(self, tankGroup, False, None):
   self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
   self.direction_x, self.direction_y = random.choice(([0.1], [0.- 1], [1.0], [- 1.0]))
   is_move = False
  if pygame.sprite.collide_rect(self, myhome):
   self.rect = self.rect.move(self.speed*-self.direction_x, self.speed*-self.direction_y)
   self.direction_x, self.direction_y = random.choice(([0.1], [0.- 1], [1.0], [- 1.0]))
   is_move = False
  returnIs_move # def reload(self): self.tank = pygame.image.load(self.tanks[self.kind][self.color]).convert_alpha() self.tank_0 = self.tank.subsurface((0.48), (48.48))
  self.tank_1 = self.tank.subsurface((48.48), (48.48))
Copy the code

Then look at the implementation of the bullet, the main attributes of the bullet include: direction, speed, whether to survive, whether to strengthen the version, etc., the code is as follows:

# Bullet class Bullet(pygame.sprite): def __init__(self): Pygame.sprite.sprite.__init__ (self) # Self. Bullets = ['images/bullet/bullet_up.png'.'images/bullet/bullet_down.png'.'images/bullet/bullet_left.png'.'images/bullet/bullet_right.png'# self.direction_x, self.direction_y =0.- 1
  self.bullet = pygame.image.load(self.bullets[0]) self.rect = self.bullet. Get_rect () # assign the actual value to the tank class self.rect.left, self.rect.right =0.0Self.speed =6Def turn(self, direction_x, direction_y): self.being = False self.stronger = False self.direction_x, self.direction_y = direction_x, direction_yif self.direction_x == 0 and self.direction_y == - 1:
   self.bullet = pygame.image.load(self.bullets[0])
  elif self.direction_x == 0 and self.direction_y == 1:
   self.bullet = pygame.image.load(self.bullets[1])
  elif self.direction_x == - 1 and self.direction_y == 0:
   self.bullet = pygame.image.load(self.bullets[2])
  elif self.direction_x == 1 and self.direction_y == 0:
   self.bullet = pygame.image.load(self.bullets[3])
  else:
   raise ValueError('Bullet class -> direction value error.'Def move(self): self.rect = self.rect.move(self.speed*self.direction_x, self.speed*self.direction_yif (self.rect.top < 3) or (self.rect.bottom > 630 - 3) or (self.rect.left < 3) or (self.rect.right > 630 - 3):
   self.being = False
Copy the code

Finally, let’s look at the program’s main initialization code, as follows:

# Initialize pygame.init() pygame.mixer.init() screen = Pygame.display.set_mode ((630.630))
pygame.display.set_caption("TANK"Bg_img = pygame.image.load()"images/others/background.png"Add_sound = Pygame.mixer.sound ()"audios/add.wav")
add_sound.set_volume(1)
bang_sound = pygame.mixer.Sound("audios/bang.wav")
bang_sound.set_volume(1)
blast_sound = pygame.mixer.Sound("audios/blast.wav")
blast_sound.set_volume(1)
fire_sound = pygame.mixer.Sound("audios/fire.wav")
fire_sound.set_volume(1)
Gunfire_sound = pygame.mixer.Sound("audios/Gunfire.wav")
Gunfire_sound.set_volume(1)
hit_sound = pygame.mixer.Sound("audios/hit.wav")
hit_sound.set_volume(1)
start_sound = pygame.mixer.Sound("audios/start.wav")
start_sound.set_volume(1Num_player = show_start_interface(screen,630.630Start_sound.play () # stage =0
num_stage = 2Is_gameover = False # clock = pygame.time.clock ()Copy the code

Take a look at the implementation:

Let’s talk about the operation keys of player 1 and player 2. The movement keys of player 1 and Player 2 are RESPECTIVELY WASD and ←→↑↓, and the shooting keys of player 1 and Player 2 are respectively J and 0.

conclusion

In this article, we have implemented the basic functions of Tank Wars using Python, which is still to be improved. If you are interested, you can make further improvements and extensions to the game.

PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan! \

Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!

[Code access ****]

Identify the qr code at the end of the text, reply: 200628