pygame泡泡坦克大战_射击游戏

""" pygame泡泡坦克大战.py 在游戏中用鼠标操作我方坦克射击彩色泡泡,打击敌方坦克,有三叉泡泡道具,连发道具,生命道具。
NPC坦克会时不时的瞄准我方坦克进行攻击,游戏可是有一定的难度哦,不过有了三叉和连发道具后就能所向披靡了。
本程序相对于前一个本版主要改动的内容为NPCTank增加了Shell属性,Tank的发射方法增加了Shell参数。各个类已经单独列为模块。
本程序的前身是李兴球先生用scratch2.0制作的泡泡坦克大战。

"""
__author__ = "李兴球"
__date__ = "2018年12月左右"

import os,math
import pygame
from pygame.locals import *
from random import randint,choice
from prop import Prop              # 从道具模块导入道具类
from explosion import Explosion    # 从爆炸模块导入爆炸类          
from shell import Shell            # 从炮弹模块导入炮弹类
from npctank import NPCTank        # 从npctank导入NPCTank类,敌方AI坦克
from tank import Tank              # 坦克模块导入Tank类,这是我方坦克
        
def collision_check():
    global npc_dead_amounts
    "玩家炮弹组和npc的碰撞检测"
    sprite_dict = pygame.sprite.groupcollide(group_player_shell,group_npc,True,True) # 返回的是一个字典
    if sprite_dict:  # 返回的可能是这样:{<Shell sprite(in 0 groups)>: [<NPCTank sprite(in 0 groups)>, <NPCTank sprite(in 0 groups)>]}                                                               #  有可能一次击中多个npc
        #print(sprite_dict)
        npc_dead_amounts += len(sprite_dict.values())                               # 统计阵亡的坦克数量
        hitted_tank = list(sprite_dict.values())[0]            
        position = hitted_tank[0].rect.center
        Explosion(explosion_images,position,group_explosion,screen,explosion_sound)                 # 生成爆炸效果实例对象
        
    "玩家和npc的碰撞检测"
    collided_npc_tank = pygame.sprite.spritecollideany(player,group_npc)            # 返回被撞到的npc
    if collided_npc_tank and not player.died():                                     # 注意防止连续碰撞
        player.die()
        collided_npc_tank.die()
        npc_dead_amounts += 1                                                       # 统计阵亡的坦克数量
        group_npc.remove(collided_npc_tank)
        position_npc = collided_npc_tank.rect.center
        Explosion(explosion_images,position_npc,group_explosion,screen,explosion_sound)
        
        position_player = player.rect.center
        Explosion(explosion_images,position_player,group_explosion,screen,explosion_sound)
        
    "玩家炮弹和npc炮弹碰撞检测,即我方炮弹可以抵销敌方炮弹"
    sprite_dict2 = pygame.sprite.groupcollide(group_player_shell,group_npc_shell,True,True) # 返回的是一个字典
    if sprite_dict2:                                              #字典的key是炮弹,只是利用它取个坐标
        shell = list(sprite_dict2.keys())[0]
        Explosion(explosion_images,shell.rect.center,group_explosion,screen,explosion_sound)
        
     
    "玩家和npc炮弹的碰撞检测"
    hit_shell = pygame.sprite.spritecollideany(player,group_npc_shell)            # 返回敌方的炮弹
    if hit_shell and not player.died():
         player.die()
         hit_shell.die()                                                          # 使用die方法                                                      
         position_shell = hit_shell.rect.center
         Explosion(explosion_images,position_shell,group_explosion,screen,explosion_sound)
        
         position_player = player.rect.center
         Explosion(explosion_images,position_player,group_explosion,screen,explosion_sound)

def playmusic(music):
    pygame.mixer.music.load(music)
    pygame.mixer.music.play(-1,0)
    
def display_cover(cover_image):
    """显示封面的函数"""
    clock = pygame.time.Clock()
    continue_game = True
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                continue_game = False
                running = False
                break
            if event.type == KEYDOWN:
                if event.key == K_SPACE:
                    running = False
                    break
        screen.blit(cover_image,(0,0))
        pygame.display.update()
        clock.tick(30)
            
    return continue_game

You must log in to read the rest of this article. Please log in or register as a user.