虽然游戏已经能正常运行了,是不是总觉得少了点什么,下面我们给游戏添加一个开始按钮,
首先创建一个新类
play.py
#导入pygame.font模块(渲染文本到屏幕上)
import pygame.font
class Button():
#初始化按钮属性
def __init__(self, my_setting, screen, msg):
self.screen = screen
self.screen_rect = screen.get_rect()
#设置按钮的大小,颜色,
self.width, self.height = 66, 33
self.button_color = (255, 0, 0)
self.text_color = (255, 255, 255)
self.font = pygame.font.SysFont(None, 48)
#按钮居中
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = self.screen_rect.center
self.prep_msg(msg)
def prep_msg(self, msg):
#将msg渲染为图片,居按钮中间
self.msg_image = self.font.render(msg, True, self.text_color, self.button_color)
self.msg_image_rect = self.msg_image.get_rect()
self.msg_image_rect.center = self.rect.center
#绘制按钮
def draw_button(self):
self.screen.fill(self.button_color, self.rect)
self.screen.blit(self.msg_image, self.msg_image_rect)
在主程序中创建按钮
..................
#先导入Button()类
from play import Button
.. .....
def run_game():
.....................
pygame.display.set_caption('外星人入侵')
play_button = Button(my_setting, screen, "PLAY")
while True:
.............................
ck.update_screen(my_setting, screen, start, ship, bullets, aliens, play_button)
g_fun.py
def update_screen(my_setting, screen, start, ship, bullets, aliens, play_button):
...........................
aliens.draw(screen)
if not start.game_active:
play_button.draw_button()
#让最近绘制的屏幕可见
pygame.display.flip()
对了,刚开始还要游戏处于非活跃状态
g_start.py
self.game_active = True 变为 self.game_active = False
此时运行程序,会看到屏幕中间的PLAY按钮
但是,此时鼠标点击PLAY按钮,游戏没反应,我们需要修改check_events()来达到点击PLAY,激活游戏
g_fun.py
def check_events(my_setting, screen, start, play_button, ship, bullets):
...........................
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(start, play_button, mouse_x, mouse_y)
def check_play_button(start, play_button, mouse_x, mouse_y):
if play_button.rect.collidepoint(mouse_x, mouse_y):
start.game_active = True
main.py
while True:
...............
ck.check_events(my_setting, screen, start, play_button, ship, bullets)
.........................
现在运行程序,点击PLAY按钮,游戏开始,游戏结束时(命数用完),重新显示PLAY按钮
为了在此点击PLAY后,游戏还能继续,我们设置单机PLAY后游戏重置
g_fun.py
def check_play_button(my_setting, screen, start, play_button, mouse_x, mouse_y, ship, aliens, bullets):
if play_button.rect.collidepoint(mouse_x, mouse_y):
start.reset_start()
start.game_active = True
aliens.empty()
bullets.empty()
alien_fleet(my_setting, screen, aliens, ship)
def check_events(my_setting, screen, start, play_button, ship, bullets, aliens):
#监视键盘和鼠标事件
for event in pygame.event.get():
......................................
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(my_setting, screen, start, play_button, mouse_x, mouse_y, ship, aliens, bullets)
main.py
while True:
ck.check_events(my_setting, screen, start, play_button, ship, bullets, aliens)
.....................
现在每次点击PLAY游戏都能重置
接下来给游戏添加一背景音乐
def run_game():
...........................
play_button = Button(my_setting, screen, "PLAY")
background_sound = pygame.mixer.Sound("game_music.ogg")
..............................
当游戏处于激活状态时背景音乐响起,游戏结束,音乐停止
while True:
.............................
if start.game_active:
background_sound.play()
..............................
else:
background_sound.stop()
...........................
音乐文件点这里http://pan.baidu.com/s/1i5viDSx 密码:ceti