欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python+pygame实现坦克大战小游戏的示例代码(可以自定义子弹速度)

程序员文章站 2022-09-22 20:06:13
python+pygame实现坦克大战小游戏—可以自定义子弹速度: 运行环境–python3.7、pycharm; 源码需要请:点赞留言邮箱; 正常版子弹速度:普通速度版加速版子弹速度:子弹加速...
  • python+pygame实现坦克大战小游戏—可以自定义子弹速度:
  • 运行环境–python3.7、pycharm;
  • 源码需要请:点赞留言邮箱;
  • 正常版子弹速度:

python+pygame实现坦克大战小游戏的示例代码(可以自定义子弹速度)

普通速度版

加速版子弹速度:

python+pygame实现坦克大战小游戏的示例代码(可以自定义子弹速度)

子弹加速版

另外还有多种道具,支持两人一起玩。main()方法如下:

def main():
  pygame.init()
  pygame.mixer.init()
  resolution = 630, 630
  screen = pygame.display.set_mode(resolution)
  pygame.display.set_caption("tank game ")
  # 加载图片,音乐,音效.
  background_image = pygame.image.load(r"..\image\background.png")
  home_image = pygame.image.load(r"..\image\home.png")
  home_destroyed_image = pygame.image.load(r"..\image\home_destroyed.png")
  bang_sound = pygame.mixer.sound(r"..\music\bang.wav")
  bang_sound.set_volume(1)
  fire_sound = pygame.mixer.sound(r"..\music\gunfire.wav")
  start_sound = pygame.mixer.sound(r"..\music\start.wav")
  start_sound.play()
  # 定义精灵组:坦克,我方坦克,敌方坦克,敌方子弹
  alltankgroup = pygame.sprite.group()
  mytankgroup = pygame.sprite.group()
  allenemygroup = pygame.sprite.group()
  redenemygroup = pygame.sprite.group()
  greenenemygroup = pygame.sprite.group()
  otherenemygroup = pygame.sprite.group() 
  enemybulletgroup = pygame.sprite.group()
  # 创建地图 
  bgmap = wall.map()
  # 创建食物/道具 但不显示
  prop = food.food()
  # 创建我方坦克
  mytank_t1 = mytank.mytank(1)
  alltankgroup.add(mytank_t1)
  mytankgroup.add(mytank_t1)
  mytank_t2 = mytank.mytank(2)
  alltankgroup.add(mytank_t2)
  mytankgroup.add(mytank_t2)
  # 创建敌方 坦克
  for i in range(1, 4):
      enemy = enemytank.enemytank(i)
      alltankgroup.add(enemy)
      allenemygroup.add(enemy)
      if enemy.isred == true:
        redenemygroup.add(enemy)
        continue
      if enemy.kind == 3:
        greenenemygroup.add(enemy)
        continue
      otherenemygroup.add(enemy)
  # 敌军坦克出现动画
  appearance_image = pygame.image.load(r"..\image\appear.png").convert_alpha()
  appearance = []
  appearance.append(appearance_image.subsurface(( 0, 0), (48, 48)))
  appearance.append(appearance_image.subsurface((48, 0), (48, 48)))
  appearance.append(appearance_image.subsurface((96, 0), (48, 48)))
  # 自定义事件
  # 创建敌方坦克延迟200
  delayevent = pygame.constants.userevent
  pygame.time.set_timer(delayevent, 200)
  # 创建 敌方 子弹延迟1000
  enemybulletnotcoolingevent = pygame.constants.userevent + 1
  pygame.time.set_timer(enemybulletnotcoolingevent, 1000)
  # 创建 我方 子弹延迟200
  mybulletnotcoolingevent = pygame.constants.userevent + 2
  pygame.time.set_timer(mybulletnotcoolingevent, 200)
  # 敌方坦克 静止8000
  notmoveevent = pygame.constants.userevent + 3
  pygame.time.set_timer(notmoveevent, 8000)
  delay = 100
  moving = 0
  movdir = 0
  moving2 = 0
  movdir2 = 0
  enemynumber = 3
  enemycouldmove   = true
  switch_r1_r2_image = true
  homesurvive     = true
  running_t1     = true
  running_t2     = true
  clock = pygame.time.clock()
  while true:
    for event in pygame.event.get():
      if event.type == pygame.quit:
        pygame.quit()
        sys.exit()
      # 我方子弹冷却事件
      if event.type == mybulletnotcoolingevent:
        mytank_t1.bulletnotcooling = true
      # 敌方子弹冷却事件
      if event.type == enemybulletnotcoolingevent:
        for each in allenemygroup:
          each.bulletnotcooling = true
      # 敌方坦克静止事件
      if event.type == notmoveevent:
        enemycouldmove = true
      # 创建敌方坦克延迟
      if event.type == delayevent:
        if enemynumber < 4:
          enemy = enemytank.enemytank()
          if pygame.sprite.spritecollide(enemy, alltankgroup, false, none):
            break
          allenemygroup.add(enemy)
          alltankgroup.add(enemy)
          enemynumber += 1
          if enemy.isred == true:
            redenemygroup.add(enemy)
          elif enemy.kind == 3:
            greenenemygroup.add(enemy)
          else:
            otherenemygroup.add(enemy)
      if event.type == pygame.keydown:
        if event.key == pygame.k_c and pygame.kmod_ctrl:
          pygame.quit()
          sys.exit()
        if event.key == pygame.k_e:
          mytank_t1.levelup()
        if event.key == pygame.k_q:
          mytank_t1.leveldown()
        if event.key == pygame.k_3:
          mytank_t1.levelup()
          mytank_t1.levelup()
          mytank_t1.level = 3
        if event.key == pygame.k_2:
          if mytank_t1.speed == 3:
            mytank_t1.speed = 24
          else:
            mytank_t1.speed = 3
        if event.key == pygame.k_1:
          for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
            bgmap.brick = wall.brick()
            bgmap.brick.rect.left, bgmap.brick.rect.top = 3 + x * 24, 3 + y * 24
            bgmap.brickgroup.add(bgmap.brick)        
        if event.key == pygame.k_4:
          for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
            bgmap.iron = wall.iron()
            bgmap.iron.rect.left, bgmap.iron.rect.top = 3 + x * 24, 3 + y * 24
            bgmap.irongroup.add(bgmap.iron)        
    # 检查用户的键盘操作
    key_pressed = pygame.key.get_pressed()
    # 玩家一的移动操作
    if moving:
      moving -= 1
      if movdir == 0:
        alltankgroup.remove(mytank_t1)
        if mytank_t1.moveup(alltankgroup, bgmap.brickgroup, bgmap.irongroup):
          moving += 1
        alltankgroup.add(mytank_t1)
        running_t1 = true
      if movdir == 1:
        alltankgroup.remove(mytank_t1)
        if mytank_t1.movedown(alltankgroup, bgmap.brickgroup, bgmap.irongroup):
          moving += 1
        alltankgroup.add(mytank_t1)
        running_t1 = true
      if movdir == 2:
        alltankgroup.remove(mytank_t1)
        if mytank_t1.moveleft(alltankgroup, bgmap.brickgroup, bgmap.irongroup):
          moving += 1
        alltankgroup.add(mytank_t1)
        running_t1 = true
      if movdir == 3:
        alltankgroup.remove(mytank_t1)
        if mytank_t1.moveright(alltankgroup, bgmap.brickgroup, bgmap.irongroup):
          moving += 1
        alltankgroup.add(mytank_t1)
        running_t1 = true
    if not moving:
      if key_pressed[pygame.k_w]:
        moving = 7
        movdir = 0
        running_t1 = true
        alltankgroup.remove(mytank_t1)
        if mytank_t1.moveup(alltankgroup, bgmap.brickgroup, bgmap.irongroup):
          moving = 0
        alltankgroup.add(mytank_t1)
      elif key_pressed[pygame.k_s]:
        moving = 7
        movdir = 1
        running_t1 = true
        alltankgroup.remove(mytank_t1)
        if mytank_t1.movedown(alltankgroup, bgmap.brickgroup, bgmap.irongroup):
          moving = 0
        alltankgroup.add(mytank_t1)
      elif key_pressed[pygame.k_a]:
        moving = 7
        movdir = 2
        running_t1 = true
        alltankgroup.remove(mytank_t1)
        if mytank_t1.moveleft(alltankgroup, bgmap.brickgroup, bgmap.irongroup):
          moving = 0
        alltankgroup.add(mytank_t1)
      elif key_pressed[pygame.k_d]:
        moving = 7
        movdir = 3
        running_t1 = true
        alltankgroup.remove(mytank_t1)
        if mytank_t1.moveright(alltankgroup, bgmap.brickgroup, bgmap.irongroup):
          moving = 0
        alltankgroup.add(mytank_t1)
    if key_pressed[pygame.k_j]:
      if not mytank_t1.bullet.life and mytank_t1.bulletnotcooling:
        fire_sound.play()
        mytank_t1.shoot()
        mytank_t1.bulletnotcooling = false
    # 玩家二的移动操作
    if moving2:
      moving2 -= 1
      if movdir2 == 0:
        alltankgroup.remove(mytank_t2)
        mytank_t2.moveup(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
        alltankgroup.add(mytank_t2)
        running_t2 = true
      if movdir2 == 1:
        alltankgroup.remove(mytank_t2)
        mytank_t2.movedown(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
        alltankgroup.add(mytank_t2)
        running_t2 = true
      if movdir2 == 2:
        alltankgroup.remove(mytank_t2)
        mytank_t2.moveleft(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
        alltankgroup.add(mytank_t2)
        running_t2 = true
      if movdir2 == 3:
        alltankgroup.remove(mytank_t2)
        mytank_t2.moveright(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
        alltankgroup.add(mytank_t2)
        running_t2 = true
    if not moving2:
      if key_pressed[pygame.k_up]:
        alltankgroup.remove(mytank_t2)
        mytank_t2.moveup(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
        alltankgroup.add(mytank_t2)
        moving2 = 7
        movdir2 = 0
        running_t2 = true
      elif key_pressed[pygame.k_down]:
        alltankgroup.remove(mytank_t2)
        mytank_t2.movedown(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
        alltankgroup.add(mytank_t2)
        moving2 = 7
        movdir2 = 1
        running_t2 = true
      elif key_pressed[pygame.k_left]:
        alltankgroup.remove(mytank_t2)
        mytank_t2.moveleft(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
        alltankgroup.add(mytank_t2)
        moving2 = 7
        movdir2 = 2
        running_t2 = true
      elif key_pressed[pygame.k_right]:
        alltankgroup.remove(mytank_t2)
        mytank_t2.moveright(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
        alltankgroup.add(mytank_t2)
        moving2 = 7
        movdir2 = 3
        running_t2 = true
    if key_pressed[pygame.k_kp0]:
      if not mytank_t2.bullet.life:
        # fire_sound.play()
        mytank_t2.shoot()
    
    # 画背景
    screen.blit(background_image, (0, 0))
    # 画砖块
    for each in bgmap.brickgroup:
      screen.blit(each.image, each.rect)    
    # 花石头
    for each in bgmap.irongroup:
      screen.blit(each.image, each.rect)    
    # 画home
    if homesurvive:
      screen.blit(home_image, (3 + 12 * 24, 3 + 24 * 24))
    else:
      screen.blit(home_destroyed_image, (3 + 12 * 24, 3 + 24 * 24))
    # 画我方坦克1
    if not (delay % 5):
      switch_r1_r2_image = not switch_r1_r2_image
    if switch_r1_r2_image and running_t1:
      screen.blit(mytank_t1.tank_r0, (mytank_t1.rect.left, mytank_t1.rect.top))
      running_t1 = false
    else:
      screen.blit(mytank_t1.tank_r1, (mytank_t1.rect.left, mytank_t1.rect.top))
    # 画我方坦克2
    if switch_r1_r2_image and running_t2:
      screen.blit(mytank_t2.tank_r0, (mytank_t2.rect.left, mytank_t2.rect.top))
      running_t2 = false
    else:
      screen.blit(mytank_t2.tank_r1, (mytank_t2.rect.left, mytank_t2.rect.top))  
    # 画敌方坦克
    for each in allenemygroup:
      # 判断5毛钱特效是否播放      
      if each.flash:
        # 判断画左动作还是右动作
        if switch_r1_r2_image:
          screen.blit(each.tank_r0, (each.rect.left, each.rect.top))
          if enemycouldmove:
            alltankgroup.remove(each)
            each.move(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
            alltankgroup.add(each)
        else:
          screen.blit(each.tank_r1, (each.rect.left, each.rect.top))
          if enemycouldmove:
            alltankgroup.remove(each)
            each.move(alltankgroup, bgmap.brickgroup, bgmap.irongroup)
            alltankgroup.add(each)          
      else:
        # 播放5毛钱特效
        if each.times > 0:
          each.times -= 1
          if each.times <= 10:
            screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
          elif each.times <= 20:
            screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
          elif each.times <= 30:
            screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
          elif each.times <= 40:
            screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
          elif each.times <= 50:
            screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
          elif each.times <= 60:
            screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
          elif each.times <= 70:
            screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
          elif each.times <= 80:
            screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
          elif each.times <= 90:
            screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
        if each.times == 0:
          each.flash = true
    # 绘制我方子弹1
    if mytank_t1.bullet.life:
      mytank_t1.bullet.move()  
      screen.blit(mytank_t1.bullet.bullet, mytank_t1.bullet.rect)
      # 子弹 碰撞 子弹
      for each in enemybulletgroup:
        if each.life:
          if pygame.sprite.collide_rect(mytank_t1.bullet, each):
            mytank_t1.bullet.life = false
            each.life = false
            pygame.sprite.spritecollide(mytank_t1.bullet, enemybulletgroup, true, none)
      # 子弹 碰撞 敌方坦克
      if pygame.sprite.spritecollide(mytank_t1.bullet, redenemygroup, true, none):
        prop.change()
        bang_sound.play()
        enemynumber -= 1
        mytank_t1.bullet.life = false
      elif pygame.sprite.spritecollide(mytank_t1.bullet,greenenemygroup, false, none):
        for each in greenenemygroup:
          if pygame.sprite.collide_rect(mytank_t1.bullet, each):
            if each.life == 1:
              pygame.sprite.spritecollide(mytank_t1.bullet,greenenemygroup, true, none)
              bang_sound.play()
              enemynumber -= 1
            elif each.life == 2:
              each.life -= 1
              each.tank = each.enemy_3_0
            elif each.life == 3:
              each.life -= 1
              each.tank = each.enemy_3_2
        mytank_t1.bullet.life = false
      elif pygame.sprite.spritecollide(mytank_t1.bullet, otherenemygroup, true, none):
        bang_sound.play()
        enemynumber -= 1
        mytank_t1.bullet.life = false  
      #if pygame.sprite.spritecollide(mytank_t1.bullet, allenemygroup, true, none):
      #  bang_sound.play()
      #  enemynumber -= 1
      #  mytank_t1.bullet.life = false
      # 子弹 碰撞 brickgroup
      if pygame.sprite.spritecollide(mytank_t1.bullet, bgmap.brickgroup, true, none):
        mytank_t1.bullet.life = false
        mytank_t1.bullet.rect.left, mytank_t1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
      # 子弹 碰撞 brickgroup
      if mytank_t1.bullet.strong:
        if pygame.sprite.spritecollide(mytank_t1.bullet, bgmap.irongroup, true, none):
          mytank_t1.bullet.life = false
          mytank_t1.bullet.rect.left, mytank_t1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
      else:  
        if pygame.sprite.spritecollide(mytank_t1.bullet, bgmap.irongroup, false, none):
          mytank_t1.bullet.life = false
          mytank_t1.bullet.rect.left, mytank_t1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
    # 绘制我方子弹2
    if mytank_t2.bullet.life:
      mytank_t2.bullet.move()  
      screen.blit(mytank_t2.bullet.bullet, mytank_t2.bullet.rect)
      # 子弹 碰撞 敌方坦克
      if pygame.sprite.spritecollide(mytank_t2.bullet, allenemygroup, true, none):
        bang_sound.play()
        enemynumber -= 1
        mytank_t2.bullet.life = false
      # 子弹 碰撞 brickgroup
      if pygame.sprite.spritecollide(mytank_t2.bullet, bgmap.brickgroup, true, none):
        mytank_t2.bullet.life = false
        mytank_t2.bullet.rect.left, mytank_t2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
      # 子弹 碰撞 brickgroup
      if mytank_t2.bullet.strong:
        if pygame.sprite.spritecollide(mytank_t2.bullet, bgmap.irongroup, true, none):
          mytank_t2.bullet.life = false
          mytank_t2.bullet.rect.left, mytank_t2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
      else:  
        if pygame.sprite.spritecollide(mytank_t2.bullet, bgmap.irongroup, false, none):
          mytank_t2.bullet.life = false
          mytank_t2.bullet.rect.left, mytank_t2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
    # 绘制敌人子弹
    for each in allenemygroup:
      # 如果子弹没有生命,则赋予子弹生命
      if not each.bullet.life and each.bulletnotcooling and enemycouldmove:
        enemybulletgroup.remove(each.bullet)
        each.shoot()
        enemybulletgroup.add(each.bullet)
        each.bulletnotcooling = false
      # 如果5毛钱特效播放完毕 并且 子弹存活 则绘制敌方子弹
      if each.flash:
        if each.bullet.life:
          # 如果敌人可以移动
          if enemycouldmove:
            each.bullet.move()
          screen.blit(each.bullet.bullet, each.bullet.rect)
          # 子弹 碰撞 我方坦克
          if pygame.sprite.collide_rect(each.bullet, mytank_t1):
            bang_sound.play()
            mytank_t1.rect.left, mytank_t1.rect.top = 3 + 8 * 24, 3 + 24 * 24 
            each.bullet.life = false
            moving = 0 # 重置移动控制参数
            for i in range(mytank_t1.level+1):
              mytank_t1.leveldown()
          if pygame.sprite.collide_rect(each.bullet, mytank_t2):
            bang_sound.play()
            mytank_t2.rect.left, mytank_t2.rect.top = 3 + 16 * 24, 3 + 24 * 24 
            each.bullet.life = false
          # 子弹 碰撞 brickgroup
          if pygame.sprite.spritecollide(each.bullet, bgmap.brickgroup, true, none):
            each.bullet.life = false
          # 子弹 碰撞 irongroup
          if each.bullet.strong:
            if pygame.sprite.spritecollide(each.bullet, bgmap.irongroup, true, none):
              each.bullet.life = false
          else:  
            if pygame.sprite.spritecollide(each.bullet, bgmap.irongroup, false, none):
              each.bullet.life = false
    # 最后画食物/道具
    if prop.life:
      screen.blit(prop.image, prop.rect)
      # 我方坦克碰撞 食物/道具
      if pygame.sprite.collide_rect(mytank_t1, prop):
        if prop.kind == 1: # 敌人全毁
          for each in allenemygroup:
            if pygame.sprite.spritecollide(each, allenemygroup, true, none):
              bang_sound.play()
              enemynumber -= 1
          prop.life = false
        if prop.kind == 2: # 敌人静止
          enemycouldmove = false
          prop.life = false
        if prop.kind == 3: # 子弹增强
          mytank_t1.bullet.strong = true
          prop.life = false
        if prop.kind == 4: # 家得到保护
          for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
            bgmap.iron = wall.iron()
            bgmap.iron.rect.left, bgmap.iron.rect.top = 3 + x * 24, 3 + y * 24
            bgmap.irongroup.add(bgmap.iron)        
          prop.life = false
        if prop.kind == 5: # 坦克无敌
          prop.life = false
          pass
        if prop.kind == 6: # 坦克升级
          mytank_t1.levelup()
          prop.life = false
        if prop.kind == 7: # 坦克生命+1
          mytank_t1.life += 1
          prop.life = false
           
    # 延迟
    delay -= 1
    if not delay:
      delay = 100  
    pygame.display.flip()
    clock.tick(60)

总结

到此这篇关于python+pygame实现坦克大战小游戏---可以自定义子弹速度的文章就介绍到这了,更多相关python+pygame实现坦克大战小游戏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!