4-多阶段
原理
本章是对第2章的延续。
我们希望为游戏添加多个阶段,比如开始界面、游戏界面、结束界面等。
为此,我们可以使用一个类来管理游戏的不同阶段。
我们创建了GameManager
类,并通过self.status
属性表示不同的阶段。
intro
阶段:显示准备的背景。当用户按下鼠标时,将状态切换为main_game
main_game
阶段:进行游戏,更新游戏画面。
class GameManager:def __init__(self):self.status = "intro"def intro(self):for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()exit()if event.type == pygame.MOUSEBUTTONDOWN:self.status = "main_game"screen.blit(bg_img, (0, 0))screen.blit(ready_img, (screen_width//2, screen_height//2))pygame.display.flip()def main_game(self):for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()exit()if event.type == pygame.MOUSEBUTTONDOWN:crosshair.shoot()screen.blit(bg_img, (0, 0))target_group.update()target_group.draw(screen)crosshair_group.update()crosshair_group.draw(screen)pygame.display.flip()def start_game(self):if self.status == "intro":self.intro()elif self.status == "main_game":self.main_game()game_manager = GameManager()
while True:game_manager.start_game()clock.tick(60)
使用GameManager类管理游戏后,我们发现游戏的主循环变得很简单。
案例
完整代码。
添加了准备阶段。
# 使用pygame SPrite实现射击游戏
import pygame
import randomclass Crosshair(pygame.sprite.Sprite):def __init__(self, img_path):super().__init__()self.image = pygame.image.load(img_path)self.rect = self.image.get_rect()self.gun_shot = pygame.mixer.Sound('gunshot.wav')def shoot(self):self.gun_shot.play()pygame.sprite.spritecollide(crosshair, target_group, True)def update(self):self.rect.center = pygame.mouse.get_pos()class Target(pygame.sprite.Sprite):def __init__(self, img_path, x, y):super().__init__()self.image = pygame.image.load(img_path)self.rect = self.image.get_rect()self.rect.center = (x, y)def update(self):pass# init
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
crosshair = Crosshair(r'PNG\HUD\crosshair_blue_small.png')crosshair_group = pygame.sprite.Group()
crosshair_group.add(crosshair)target_group = pygame.sprite.Group()
for i in range(20):x,y = random.randint(0, screen_width), random.randint(0, screen_height)target = Target(r'PNG\Objects\duck_target_yellow.png', x, y)target_group.add(target)bg_img = pygame.image.load(r'PNG\Stall\bg_wood.png')
bg_img = pygame.transform.scale(bg_img, (screen_width, screen_height))ready_img = pygame.image.load(r'PNG\HUD\text_ready.png')class GameManager:def __init__(self):self.status = "intro"def intro(self):for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()exit()if event.type == pygame.MOUSEBUTTONDOWN:self.status = "main_game"screen.blit(bg_img, (0, 0))screen.blit(ready_img, (screen_width//2, screen_height//2))pygame.display.flip()def main_game(self):for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()exit()if event.type == pygame.MOUSEBUTTONDOWN:crosshair.shoot()screen.blit(bg_img, (0, 0))target_group.update()target_group.draw(screen)crosshair_group.update()crosshair_group.draw(screen)pygame.display.flip()def start_game(self):if self.status == "intro":self.intro()elif self.status == "main_game":self.main_game()game_manager = GameManager()
while True:game_manager.start_game()clock.tick(60)