开发工具
Python版本: 3.7.4
相关模块:
pygame模块;
以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
原理简介
首先,我们打开原版游戏的开始界面,发现是这样的:
具体而言,我们的思路是先定义一个按钮类,来模拟原始游戏中的“开始游戏”,“游戏说明”和“离开游戏”这三个按键的功能:
'''按钮类'''
class Button(pygame.sprite.Sprite):
def __init__(self, text, fontpath, fontsize, position, color_selected=(255, 0, 0), color_default=(255, 255, 255)):
pygame.sprite.Sprite.__init__(self)
self.text = text
self.color_selected = color_selected
self.color_default = color_default
self.font = pygame.font.Font(fontpath, fontsize)
self.font_render = self.font.render(text, True, color_default)
self.rect = self.font_render.get_rect()
self.rect.center = position
'''更新函数: 不断地更新检测鼠标是否在按钮上'''
def update(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.font_render = self.font.render(self.text, True, self.color_selected)
else:
self.font_render = self.font.render(self.text, True, self.color_default)
'''绑定到屏幕上'''
def draw(self, screen):
screen.blit(self.font_render, self.rect)
复制代码
主要的原理就是不断检测鼠标是否在对应的按钮区域,如果在,则对按钮的颜色做出改变(这里是变成红色),否则按钮使用默认的颜色(这里是白色),以此来向玩家表明这是可点击的按钮。
然后,我们来实例化它三次来添加这三个按钮到游戏的开始界面中:
'''游戏开始界面'''
class StartGameInterface():
def __init__(self, cfg):
self.cfg = cfg
self.play_btn = Button('开始游戏', cfg.FONTPATH_CN, 50, (cfg.SCREENSIZE[0]//2, cfg.SCREENSIZE[1] - 400))
self.intro_btn = Button('游戏说明', cfg.FONTPATH_CN, 50, (cfg.SCREENSIZE[0]//2, cfg.SCREENSIZE[1] - 300))
self.quit_btn = Button('离开游戏', cfg.FONTPATH_CN, 50, (cfg.SCREENSIZE[0]//2, cfg.SCREENSIZE[1] - 200))
'''外部调用'''
def run(self, screen):
# 魔塔
font = pygame.font.Font(self.cfg.FONTPATH_CN, 80)
font_render_cn = font.render('魔塔', True, (255, 255, 255))
rect_cn = font_render_cn.get_rect()
rect_cn.center = self.cfg.SCREENSIZE[0] // 2, 200
# Magic Tower
font = pygame.font.Font(self.cfg.FONTPATH_EN, 80)
font_render_en = font.render('Magic Tower', True, (255, 255, 255))
rect_en = font_render_en.get_rect()
rect_en.center = self.cfg.SCREENSIZE[0] // 2, 350
# (Ver 1.12)
font = pygame.font.Font(self.cfg.FONTPATH_CN, 40)
font_render_version = font.render('(Ver 1.12)', True, (255, 255, 255))
rect_ver = font_render_version.get_rect()
rect_ver.center = self.cfg.SCREENSIZE[0] // 2, 400
clock = pygame.time.Clock()
while True:
screen.fill((0, 0, 0))
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):
pygame.quit()
sys.exit(0)
elif self.intro_btn.rect.collidepoint(mouse_pos):
self.showgameintro(screen)
for btn in [self.intro_btn, self.play_btn, self.quit_btn]:
btn.update()
btn.draw(screen)
for fr, rect in zip([font_render_cn, font_render_en, font_render_version], [rect_cn, rect_en, rect_ver]):
screen.blit(fr, rect)
pygame.display.flip()
clock.tick(self.cfg.FPS)
'''显示游戏简介'''
def showgameintro(self, screen):
font = pygame.font.Font(self.cfg.FONTPATH_CN, 20)
font_renders = [
font.render('魔塔小游戏.', True, (255, 255, 255)),
font.render('游戏素材来自: http://www.4399.com/flash/1749_1.htm.', True, (255, 255, 25