反塔防游戏,
本文主要介绍如何利用Python中的Pygame模块制作一个简单的塔防游戏。文章中的示例代码有详细的解释,感兴趣的朋友可以和边肖一起尝试一下。
00-1010 1.环境要求。游戏介绍1。游戏目标2。游戏效果图3。完成开发流程1。项目主体结构2。详细配置3。敌人、塔和子弹类别的定义。游戏开始:选择难度图5、游戏开始界面6、游戏运行7、游戏暂停8、游戏结束和分数9。音频、图片和地图介绍。
目录
Windows系统,python3.6
安装模块
pip安装pyqt5
pip安装游戏
一、环境要求
二、游戏介绍
根据关卡,设计不同的塔防地图(博主只设计了三个关卡,有兴趣的同学可以学习后自己绘制地图),设置三种堡垒,每种堡垒发射不同的炮弹,每种炮弹对敌人的杀伤力不同,每种
堡的价格也不一样。玩家通过硬币购买堡垒,有切割出堡垒的操作(有点类似《植物大战僵尸》中的铁锹)。敌人成功到达塔下,游戏结束。
1、游戏目标
2、先上游戏效果图
三、完整开发流程
首先,梳理一下项目的主体结构。其实看主体结构就基本清楚了。
1、项目主结构
配置. py
在配置文件中,需要引入os模块,配置打开游戏的屏幕大小,将参考资料中引用的图片和音频插入适当的位置。
因为我们的迷宫游戏,我们需要削减模块。
个人资料
导入操作系统
屏幕大小
屏幕大小=(800,600)
图片路径
图像路径={
选择 : {
load _ game : OS . path . join(OS . getcwd(), resources/images/choice/load _ game . png ),
map 1 : OS . path . join(OS . get CWD(), resources/images/choice/map 1 . png ),
map 1 _ black : OS . path . join(OS . getcwd(), resources/images/choice/map 1 _ black . png ),
map 1 _ red : OS . path . join(OS . getcwd(), resources/images/choice/map 1 _ red . png ),
map 2 : OS . path . join(OS . get CWD(), resources/images/choice/map 2 . png ),
map 2 _ black : OS . path . join(OS . getcwd(), resources/images/choice/map 2 _ black . png ),
map 2 _ red : OS . path . join(OS . getcwd(), resources/images/choice/map 2 _ red . png ),
map 3 : OS . path . join(OS . get CWD(), resources/images/choice/map 3 . png ),
map 3 _ black : OS . path . join(OS . getcwd(), resources/images/choice/map 3 _ black . png ),
map3_red: os.path
join(os.getcwd(), resources/images/choice/map3_red.png),
},
end: {
gameover: os.path.join(os.getcwd(), resources/images/end/gameover.png),
continue_red: os.path.join(os.getcwd(), resources/images/end/continue_red.png),
continue_black: os.path.join(os.getcwd(), resources/images/end/continue_black.png),
},
game: {
arrow1: os.path.join(os.getcwd(), resources/images/game/arrow1.png),
arrow2: os.path.join(os.getcwd(), resources/images/game/arrow2.png),
arrow3: os.path.join(os.getcwd(), resources/images/game/arrow3.png),
basic_tower: os.path.join(os.getcwd(), resources/images/game/basic_tower.png),
boulder: os.path.join(os.getcwd(), resources/images/game/boulder.png),
bush: os.path.join(os.getcwd(), resources/images/game/bush.png),
cave: os.path.join(os.getcwd(), resources/images/game/cave.png),
dirt: os.path.join(os.getcwd(), resources/images/game/dirt.png),
enemy_blue: os.path.join(os.getcwd(), resources/images/game/enemy_blue.png),
enemy_pink: os.path.join(os.getcwd(), resources/images/game/enemy_pink.png),
enemy_red: os.path.join(os.getcwd(), resources/images/game/enemy_red.png),
enemy_yellow: os.path.join(os.getcwd(), resources/images/game/enemy_yellow.png),
godark: os.path.join(os.getcwd(), resources/images/game/godark.png),
golight: os.path.join(os.getcwd(), resources/images/game/golight.png),
grass: os.path.join(os.getcwd(), resources/images/game/grass.png),
healthfont: os.path.join(os.getcwd(), resources/images/game/healthfont.png),
heavy_tower: os.path.join(os.getcwd(), resources/images/game/heavy_tower.png),
med_tower: os.path.join(os.getcwd(), resources/images/game/med_tower.png),
nexus: os.path.join(os.getcwd(), resources/images/game/nexus.png),
othergrass: os.path.join(os.getcwd(), resources/images/game/othergrass.png),
path: os.path.join(os.getcwd(), resources/images/game/path.png),
rock: os.path.join(os.getcwd(), resources/images/game/rock.png),
tiles: os.path.join(os.getcwd(), resources/images/game/tiles.png),
unitfont: os.path.join(os.getcwd(), resources/images/game/unitfont.png),
water: os.path.join(os.getcwd(), resources/images/game/water.png),
x: os.path.join(os.getcwd(), resources/images/game/x.png),
},
pause: {
gamepaused: os.path.join(os.getcwd(), resources/images/pause/gamepaused.png),
resume_black: os.path.join(os.getcwd(), resources/images/pause/resume_black.png),
resume_red: os.path.join(os.getcwd(), resources/images/pause/resume_red.png),
},
start: {
play_black: os.path.join(os.getcwd(), resources/images/start/play_black.png),
play_red: os.path.join(os.getcwd(), resources/images/start/play_red.png),
quit_black: os.path.join(os.getcwd(), resources/images/start/quit_black.png),
quit_red: os.path.join(os.getcwd(), resources/images/start/quit_red.png),
start_interface: os.path.join(os.getcwd(), resources/images/start/start_interface.png),
},
}
地图路径
MAPPATHS = {
1: os.path.join(os.getcwd(), resources/maps/1.map),
2: os.path.join(os.getcwd(), resources/maps/2.map),
3: os.path.join(os.getcwd(), resources/maps/3.map),
}
字体路径
FONTPATHS = {
Calibri: os.path.join(os.getcwd(), resources/fonts/Calibri.ttf),
m04: os.path.join(os.getcwd(), resources/fonts/m04.ttf),
Microsoft Sans Serif: os.path.join(os.getcwd(), resources/fonts/Microsoft Sans Serif.ttf),
}
不同难度的settings
DIFFICULTYPATHS = {
easy: os.path.join(os.getcwd(), resources/difficulties/easy.json),
hard: os.path.join(os.getcwd(), resources/difficulties/hard.json),
medium: os.path.join(os.getcwd(), resources/difficulties/medium.json),
}
音频路径
AUDIOPATHS = {
bgm: os.path.join(os.getcwd(), resources/audios/bgm.mp3),
}
3、定义敌人、塔楼、子弹的类
turrent.py 以炮塔类为例
炮塔首先需要初始化,即init函数,另外需要有射击属性、以及重置属性(即我们可以将已经建好的炮塔删除,然后重新添加)
import pygamefrom .arrow import Arrow
炮塔类
class Turret(pygame.sprite.Sprite):
def __init__(self, turret_type, cfg):
assert turret_type in range(3)
pygame.sprite.Sprite.__init__(self)
self.cfg = cfg
self.turret_type = turret_type
self.imagepaths = [cfg.IMAGEPATHS[game][basic_tower], cfg.IMAGEPATHS[game][med_tower], cfg.IMAGEPATHS[game][heavy_tower]]
self.image = pygame.image.load(self.imagepaths[turret_type])
self.rect = self.image.get_rect()
# 箭
self.arrow = Arrow(turret_type, cfg)
# 当前的位置
self.coord = 0, 0
self.position = 0, 0
self.rect.left, self.rect.top = self.position
self.reset()
射击
def shot(self, position, angle=None):
arrow = None
if not self.is_cooling:
arrow = Arrow(self.turret_type, self.cfg)
arrow.reset(position, angle)
self.is_cooling = True
if self.is_cooling:
self.cool_time -= 1
if self.cool_time == 0:
self.reset()
return arrow
重置
def reset(self):
if self.turret_type == 0:
# 价格
self.price = 500
# 射箭的冷却时间
self.cool_time = 30
# 是否在冷却期
self.is_cooling = False
elif self.turret_type == 1:
self.price = 1000
self.cool_time = 50
self.is_cooling = False
elif self.turret_type == 2:
self.price = 1500
self.cool_time = 100
self.is_cooling = False
4、游戏开始:选择难度地图
在这里我们把游戏主界面初始化,并把三个难度的地图列出来,让玩家选择。
import sysimport pygame
游戏选择主界面
class MainInterface(pygame.sprite.Sprite):
def __init__(self, cfg):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(cfg.IMAGEPATHS[choice][load_game]).convert()
self.rect = self.image.get_rect()
self.rect.topleft = (0, 0)
更新函数
def update(self):
pass
地图1
class MapButton1(pygame.sprite.Sprite):
def __init__(self, cfg, position=(175, 240)):
pygame.sprite.Sprite.__init__(self)
self.image_1 = pygame.image.load(cfg.IMAGEPATHS[choice][map1_black]).convert()
self.image_2 = pygame.image.load(cfg.IMAGEPATHS[choice][map1_red]).convert()
self.image_3 = pygame.image.load(cfg.IMAGEPATHS[choice][map1]).convert()
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.center = position
更新函数: 不断地更新检测鼠标是否在按钮上
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.image = self.image_2
else:
self.image = self.image_1
地图2
class MapButton2(pygame.sprite.Sprite):
def __init__(self, cfg, position=(400, 240)):
pygame.sprite.Sprite.__init__(self)
self.image_1 = pygame.image.load(cfg.IMAGEPATHS[choice][map2_black]).convert()
self.image_2 = pygame.image.load(cfg.IMAGEPATHS[choice][map2_red]).convert()
self.image_3 = pygame.image.load(cfg.IMAGEPATHS[choice][map2]).convert()
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.center = position
更新函数: 不断地更新检测鼠标是否在按钮上
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.image = self.image_2
else:
self.image = self.image_1
地图3
class MapButton3(pygame.sprite.Sprite):
def __init__(self, cfg, position=(625, 240)):
pygame.sprite.Sprite.__init__(self)
self.image_1 = pygame.image.load(cfg.IMAGEPATHS[choice][map3_black]).convert()
self.image_2 = pygame.image.load(cfg.IMAGEPATHS[choice][map3_red]).convert()
self.image_3 = pygame.image.load(cfg.IMAGEPATHS[choice][map3]).convert()
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.center = position
更新函数: 不断地更新检测鼠标是否在按钮上
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.image = self.image_2
else:
self.image = self.image_1
信息显示框
class InfoBox(pygame.sprite.Sprite):
def __init__(self, position=(400, 475)):
pygame.sprite.Sprite.__init__(self)
self.ori_image = pygame.Surface((625, 200))
self.ori_image.fill((255, 255, 255))
self.ori_image_front = pygame.Surface((621, 196))
self.ori_image_front.fill((0, 0, 0))
self.ori_image.blit(self.ori_image_front, (2, 2))
self.rect = self.ori_image.get_rect()
self.rect.center = position
更新函数
def update(self, btns):
self.image = self.ori_image
mouse_pos = pygame.mouse.get_pos()
for btn in btns:
if btn.rect.collidepoint(mouse_pos):
self.image.blit(btn.image_3, (225, 25))
break
简单难度按钮
class EasyButton(pygame.sprite.Sprite):
def __init__(self, cfg, position=(400, 150)):
pygame.sprite.Sprite.__init__(self)
self.image_1 = pygame.Surface((285, 100))
self.image_1_front = pygame.Surface((281, 96))
self.image_1.fill((255, 255, 255))
self.image_1_front.fill((0, 0, 0))
self.image_1.blit(self.image_1_front, (2, 2))
self.image_2 = pygame.Surface((285, 100))
self.image_2_front = pygame.Surface((281, 96))
self.image_2.fill((255, 255, 255))
self.image_2_front.fill((24, 196, 40))
self.image_2.blit(self.image_2_front, (2, 2))
self.text = easy
self.font = pygame.font.Font(cfg.FONTPATHS[m04], 42)
self.text_render = self.font.render(self.text, 1, (255, 255, 255))
self.image_1.blit(self.text_render, (60, 29))
self.image_2.blit(self.text_render, (60, 29))
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.center = position
更新函数: 不断地更新检测鼠标是否在按钮上
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.image = self.image_2
else:
self.image = self.image_1
中等难度按钮
class MediumButton(pygame.sprite.Sprite):
def __init__(self, cfg, position=(400, 300)):
pygame.sprite.Sprite.__init__(self)
self.image_1 = pygame.Surface((285, 100))
self.image_1_front = pygame.Surface((281, 96))
self.image_1.fill((255, 255, 255))
self.image_1_front.fill((0, 0, 0))
self.image_1.blit(self.image_1_front, (2, 2))
self.image_2 = pygame.Surface((285, 100))
self.image_2_front = pygame.Surface((281, 96))
self.image_2.fill((255, 255, 255))
self.image_2_front.fill((24, 30, 196))
self.image_2.blit(self.image_2_front, (2, 2))
self.text = medium
self.font = pygame.font.Font(cfg.FONTPATHS[m04], 42)
self.text_render = self.font.render(self.text, 1, (255, 255, 255))
self.image_1.blit(self.text_render, (15, 29))
self.image_2.blit(self.text_render, (15, 29))
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.center = position
更新函数: 不断地更新检测鼠标是否在按钮上
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.image = self.image_2
else:
self.image = self.image_1
困难难度按钮
class HardButton(pygame.sprite.Sprite):
def __init__(self, cfg, position=(400, 450)):
pygame.sprite.Sprite.__init__(self)
self.image_1 = pygame.Surface((285, 100))
self.image_1_front = pygame.Surface((281, 96))
self.image_1.fill((255, 255, 255))
self.image_1_front.fill((0, 0, 0))
self.image_1.blit(self.image_1_front, (2, 2))
self.image_2 = pygame.Surface((285, 100))
self.image_2_front = pygame.Surface((281, 96))
self.image_2.fill((255, 255, 255))
self.image_2_front.fill((196, 24, 24))
self.image_2.blit(self.image_2_front, (2, 2))
self.text = hard
self.font = pygame.font.Font(cfg.FONTPATHS[m04], 42)
self.text_render = self.font.render(self.text, 1, (255, 255, 255))
self.image_1.blit(self.text_render, (60, 29))
self.image_2.blit(self.text_render, (60, 29))
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.center = position
更新函数: 不断地更新检测鼠标是否在按钮上
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.image = self.image_2
else:
self.image = self.image_1
游戏地图和困难选择界面
class ChoiceInterface():
def __init__(self, cfg):
# part1
self.main_interface = MainInterface(cfg)
self.map_btn1 = MapButton1(cfg)
self.map_btn2 = MapButton2(cfg)
self.map_btn3 = MapButton3(cfg)
self.info_box = InfoBox()
# part2
self.easy_btn = EasyButton(cfg)
self.medium_btn = MediumButton(cfg)
self.hard_btn = HardButton(cfg)
外部调用
def update(self, screen):
clock = pygame.time.Clock()
# part1
self.map_btns = pygame.sprite.Group(self.map_btn1, self.map_btn2, self.map_btn3)
map_choice, difficulty_choice = None, None
while True:
clock.tick(60)
self.main_interface.update()
self.map_btns.update()
self.info_box.update(self.map_btns)
screen.blit(self.main_interface.image, self.main_interface.rect)
self.map_btns.draw(screen)
screen.blit(self.info_box.image, self.info_box.rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_pos = pygame.mouse.get_pos()
idx = 0
for btn in self.map_btns:
idx += 1
if btn.rect.collidepoint(mouse_pos):
map_choice = idx
if map_choice:
break
# part2
self.difficulty_btns = pygame.sprite.Group(self.easy_btn, self.medium_btn, self.hard_btn)
while True:
clock.tick(60)
screen.fill((0, 0, 0))
self.difficulty_btns.update()
self.difficulty_btns.draw(screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_pos = pygame.mouse.get_pos()
idx = 0
for btn in self.difficulty_btns:
idx += 1
if btn.rect.collidepoint(mouse_pos):
difficulty_choice = btn.text
if difficulty_choice:
break
return map_choice, difficulty_choice
5、游戏开始界面
包括开始按钮,退出游戏等操作
start.py
import sysimport pygame
游戏开始主界面
class MainInterface(pygame.sprite.Sprite):
def __init__(self, cfg):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(cfg.IMAGEPATHS[start][start_interface]).convert()
self.rect = self.image.get_rect()
self.rect.center = cfg.SCREENSIZE[0] / 2, cfg.SCREENSIZE[1] / 2
更新函数
def update(self):
pass
开始游戏按钮
class PlayButton(pygame.sprite.Sprite):
def __init__(self, cfg, position=(220, 415)):
pygame.sprite.Sprite.__init__(self)
self.image_1 = pygame.image.load(cfg.IMAGEPATHS[start][play_black]).convert()
self.image_2 = pygame.image.load(cfg.IMAGEPATHS[start][play_red]).convert()
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.center = position
更新函数: 不断地更新检测鼠标是否在按钮上
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.image = self.image_2
else:
self.image = self.image_1
结束游戏按钮
class QuitButton(pygame.sprite.Sprite):
def __init__(self, cfg, position=(580, 415)):
pygame.sprite.Sprite.__init__(self)
self.image_1 = pygame.image.load(cfg.IMAGEPATHS[start][quit_black]).convert()
self.image_2 = pygame.image.load(cfg.IMAGEPATHS[start][quit_red]).convert()
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.center = position
更新函数: 不断地更新检测鼠标是否在按钮上
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.image = self.image_2
else:
self.image = self.image_1
游戏开始界面
class StartInterface():
def __init__(self, cfg):
self.main_interface = MainInterface(cfg)
self.play_btn = PlayButton(cfg)
self.quit_btn = QuitButton(cfg)
self.components = pygame.sprite.LayeredUpdates(self.main_interface, self.play_btn, self.quit_btn)
外部调用
def update(self, screen):
clock = pygame.time.Clock()
while True:
clock.tick(60)
self.components.update()
self.components.draw(screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_pos = pygame.mouse.get_pos()
if self.play_btn.rect.collidepoint(mouse_pos):
return True
elif self.quit_btn.rect.collidepoint(mouse_pos):
return False
6、游戏运行
gaming.py
import sysimport json
import math
import random
import pygame
from ..sprites import Enemy
from ..sprites import Turret
from .pause import PauseInterface
from collections import namedtuple
游戏进行中界面
class GamingInterface():
def __init__(self, cfg):
self.cfg = cfg
# 游戏地图大小
map_w = self.cfg.SCREENSIZE[0]
map_h = 500
# 按钮大小和位置
button_w = 60
button_h = 60
button_y = 520
# 间隙
gap = 20
# 按钮放在工具栏, 工具栏两端各有一个信息显示框
toolbar_w = gap * 7 + button_w * 6
info_w = (self.cfg.SCREENSIZE[0] - toolbar_w) // 2
info_h = self.cfg.SCREENSIZE[1] - map_h
toolbar_h = self.cfg.SCREENSIZE[1] - 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(self.cfg.SCREENSIZE[0] - 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(cfg.IMAGEPATHS[game][grass])
# 岩石(铺路用的)
self.rock = pygame.image.load(cfg.IMAGEPATHS[game][rock])
# 污垢
self.dirt = pygame.image.load(cfg.IMAGEPATHS[game][dirt])
# 水
self.water = pygame.image.load(cfg.IMAGEPATHS[game][water])
# 灌木
self.bush = pygame.image.load(cfg.IMAGEPATHS[game][bush])
# 纽带
self.nexus = pygame.image.load(cfg.IMAGEPATHS[game][nexus])
# 洞穴
self.cave = pygame.image.load(cfg.IMAGEPATHS[game][cave])
# 获取地图元素的大小,请保证素材库里组成地图的元素图大小一致
self.element_size = int(self.grass.get_rect().width)
# 一些字体
self.info_font = pygame.font.Font(cfg.FONTPATHS[Calibri], 14)
self.button_font = pygame.font.Font(cfg.FONTPATHS[Calibri], 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.current_map = dict()
# 当前鼠标携带的图标(即选中道具) -> [道具名, 道具]
self.mouse_carried = []
# 在地图上建造好了的炮塔
self.built_turret_group = pygame.sprite.Group()
# 所有的敌人
self.enemies_group = pygame.sprite.Group()
# 所有射出的箭
self.arrows_group = pygame.sprite.Group()
# 玩家操作用的按钮
Button = namedtuple(Button, [rect, text, onClick])
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生成一波敌人
generate_enemies_event = pygame.constants.USEREVENT + 0
pygame.time.set_timer(generate_enemies_event, 60000)
# 生成敌人的flag和当前已生成敌人的总次数
generate_enemies_flag = False
num_generate_enemies = 0
# 每0.5秒出一个敌人
generate_enemy_event = pygame.constants.USEREVENT + 1
pygame.time.set_timer(generate_enemy_event, 500)
generate_enemy_flag = False
# 防止变量未定义
enemy_range = None
num_enemy = None
# 是否手动操作箭塔射击
manual_shot = False
has_control = False
# 游戏主循环
while True:
if self.health <= 0:
return
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quitGame()
if event.type == pygame.MOUSEBUTTONUP:
# --左键选物品
if event.button == 1:
# ----鼠标点击在地图上
if self.map_rect.collidepoint(event.pos):
if self.mouse_carried:
if self.mouse_carried[0] == turret:
self.buildTurret(event.pos)
elif self.mouse_carried[0] == XXX:
self.sellTurret(event.pos)
# ----鼠标点击在工具栏
elif self.toolbar_rect.collidepoint(event.pos):
for button in self.buttons:
if button.rect.collidepoint(event.pos):
if button.text == T1:
button.onClick()
elif button.text == T2:
button.onClick()
elif button.text == T3:
button.onClick()
elif button.text == XXX:
button.onClick()
elif button.text == Pause:
button.onClick(screen)
elif button.text == Quit:
button.onClick()
break
# --右键释放物品
if event.button == 3:
self.mouse_carried = []
# --按中间键手动控制炮塔射箭方向一。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。