Pygame基础4-多阶段

简介: Pygame基础4-多阶段

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 random

class 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)
相关文章
|
2月前
|
编解码 数据可视化 固态存储
CV目标检测 Task02: 练死劲儿-网络设计 打卡笔记
CV目标检测 Task02: 练死劲儿-网络设计 打卡笔记
27 0
|
1月前
|
Python 容器
Pygame入门 2022 (4) 使用精灵类重构
Pygame入门 2022 (4) 使用精灵类重构
|
1月前
|
算法 Linux API
Pygame的主要优势
【6月更文挑战第12天】Pygame的主要优势
14 3
|
1月前
|
开发者 Python
Pygame提供了一套全面的工具和接口
【6月更文挑战第11天】,Pygame提供了一套全面的工具和接口
18 4
|
1月前
|
Linux API 开发工具
使用Pygame库进行2D游戏开发的优缺点有哪些?
【6月更文挑战第10天】使用Pygame库进行2D游戏开发的优缺点有哪些?
18 1
|
1月前
|
存储 缓存 开发者
如何利用Pygame实现动画效果?
【6月更文挑战第10天】如何利用Pygame实现动画效果?
29 1
|
2月前
|
前端开发 测试技术 API
深入探究 Playwright:Frame 操作技巧
Playwright Python 框架提供API处理Web页面中的iframe。通过`frame()`方法进入iframe,如`page.frame(name='frame_name')`,并可使用CSS选择器选择。完成操作后,用`main_frame()`返回主文档。在iframe内,可执行点击、填充表单等操作,简化自动化测试和网页爬取任务。
56 6
深入探究 Playwright:Frame 操作技巧
|
1月前
|
Python
Pygame基础8-碰撞
Pygame基础8-碰撞
|
1月前
|
数据可视化 Python
Pygame基础10-物理模拟
Pygame基础10-物理模拟
|
1月前
|
Python
Pygame基础3-动画
Pygame基础3-动画