【python游戏】这年头塔除了拆还能干什么?这款好玩上瘾的塔防游戏,了解一下嘛( 二 )


完整代码 点击此处跳转文末名片免费获取噢~
3)游戏进行中界面
import sysimport jsonimport mathimport randomimport pygamesys.path.append('..')from sprites import Enemyfrom sprites import Turretfrom interface import PAUSEfrom pygame.locals import *from collections import namedtuple# 按钮类: 位置、文本、点击触发的事件Button = namedtuple('Button', ['rect', 'text', 'onClick'])# 定义一些颜色info_color = (120, 20, 50)red = (255, 0, 0)green = (0, 255, 0)black = (0, 0, 0)white = (255, 255, 255)grey = (127, 127, 127)button_color1 = (0, 200, 0)button_color2 = (0, 100, 0)'''游戏进行中界面'''class GAMING():def __init__(self, WIDTH=800, HEIGHT=600):self.WIDTH = WIDTHself.HEIGHT = HEIGHT# 游戏地图大小map_w = WIDTHmap_h = 500# 按钮大小和位置button_w = 60button_h = 60button_y = 520# 间隙gap = 20# 按钮放在工具栏, 工具栏两端各有一个信息显示框toolbar_w = gap * 7 + button_w * 6info_w = (WIDTH - toolbar_w) // 2info_h = HEIGHT - map_htoolbar_h = HEIGHT - map_h# 界面布置self.map_rect = pygame.Rect(0, 0, map_w, map_h)self.map_surface = pygame.Surface((map_w, map_h))self.leftinfo_rect = pygame.Rect(0, map_h, info_w, info_h)self.rightinfo_rect = pygame.Rect(WIDTH-info_w, map_h, info_w, info_h)self.toolbar_rect = pygame.Rect(info_w, map_h, toolbar_w, toolbar_h)# 草self.grass = pygame.image.load("./resource/imgs/game/grass.png")# 岩石(铺路用的)self.rock = pygame.image.load("./resource/imgs/game/rock.png")# 污垢self.dirt = pygame.image.load("./resource/imgs/game/dirt.png")# 水self.water = pygame.image.load("./resource/imgs/game/water.png")# 灌木self.bush = pygame.image.load("./resource/imgs/game/bush.png")# 纽带self.nexus = pygame.image.load("./resource/imgs/game/nexus.png")# 洞穴self.cave = pygame.image.load("./resource/imgs/game/cave.png")# 获取地图元素的大小,请保证素材库里组成地图的元素图大小一致self.elementSize = int(self.grass.get_rect().width)# 一些字体self.info_font = pygame.font.Font('./resource/fonts/Calibri.ttf', 14)self.button_font = pygame.font.Font('./resource/fonts/Calibri.ttf', 20)# 可以放炮塔的地方self.placeable = {0: self.grass}# 地图元素字典(数字对应.map文件中的数字)self.map_elements = {0: self.grass,1: self.rock,2: self.dirt,3: self.water,4: self.bush,5: self.nexus,6: self.cave}# 用于记录地图中的道路self.path_list = []# 当前的地图,将地图导入到这里面self.currentMap = dict()# 当前鼠标携带的图标(即选中道具) -> [道具名, 道具]self.mouseCarried = []# 在地图上建造好了的炮塔self.builtTurretGroup = pygame.sprite.Group()# 所有的敌人self.EnemiesGroup = pygame.sprite.Group()# 所有射出的箭self.arrowsGroup = pygame.sprite.Group()# 玩家操作用的按钮self.buttons = [Button(pygame.Rect((info_w+gap), button_y, button_w, button_h), 'T1', self.takeT1),Button(pygame.Rect((info_w+gap*2+button_w), button_y, button_w, button_h), 'T2', self.takeT2),Button(pygame.Rect((info_w+gap*3+button_w*2), button_y, button_w, button_h), 'T3', self.takeT3),Button(pygame.Rect((info_w+gap*4+button_w*3), button_y, button_w, button_h), 'XXX', self.takeXXX),Button(pygame.Rect((info_w+gap*5+button_w*4), button_y, button_w, button_h), 'Pause', self.pauseGame),Button(pygame.Rect((info_w+gap*6+button_w*5), button_y, button_w, button_h), 'Quit', self.quitGame)]'''开始游戏'''def start(self, screen, map_path=None, difficulty_path=None):# 读取游戏难度对应的参数with open(difficulty_path, 'r') as f:difficulty_dict = json.load(f)self.money = difficulty_dict.get('money')self.health = difficulty_dict.get('health')self.max_health = difficulty_dict.get('health')difficulty_dict = difficulty_dict.get('enemy')# 每60s生成一波敌人GenEnemiesEvent = pygame.constants.USEREVENT + 0pygame.time.set_timer(GenEnemiesEvent, 60000)# 生成敌人的flag和当前已生成敌人的总次数genEnemiesFlag = FalsegenEnemiesNum = 0# 每0.5秒出一个敌人GenEnemyEvent = pygame.constants.USEREVENT + 1pygame.time.set_timer(GenEnemyEvent, 500)genEnemyFlag = False# 防止变量未定义enemyRange = NonenumEnemy = None# 是否手动操作箭塔射击Manualshot = Falsehas_control = Falsewhile True:if self.health