单向场景切换版本 #1
No files matched your search
@@ -3,3 +3,9 @@ import pygame
|
||||
class BaseScene:
|
||||
def __init__(self, screen: pygame.Surface):
|
||||
self.screen = screen
|
||||
|
||||
def update(self):
|
||||
pass
|
||||
|
||||
def handle_event(self, events):
|
||||
pass
|
||||
@@ -4,6 +4,17 @@ from src.scenes.base_scene import BaseScene
|
||||
|
||||
|
||||
class MainMenuScene(BaseScene):
|
||||
def __init__(self, screen):
|
||||
super().__init__(screen)
|
||||
self.button_status = False
|
||||
|
||||
def update(self):
|
||||
pygame.draw.rect(self.screen, Color.white, [10,20,30,40])
|
||||
if self.button_status:
|
||||
pygame.draw.rect(self.screen, Color.white, [50,20,30,40])
|
||||
|
||||
def handle_event(self, event):
|
||||
if event.type == pygame.KEYDOWN:
|
||||
self.button_status = True
|
||||
if event.type == pygame.KEYUP:
|
||||
self.button_status = False
|
||||
@@ -16,6 +16,9 @@ class Game:
|
||||
# 设置标题
|
||||
pygame.display.set_caption(GameConfig.game_name)
|
||||
|
||||
# 当前场景
|
||||
self.scene = MainMenuScene(self.screen)
|
||||
|
||||
# 运行循环
|
||||
self.running = True
|
||||
self.run()
|
||||
@@ -23,16 +26,16 @@ class Game:
|
||||
def run(self):
|
||||
while self.running:
|
||||
# 事件处理
|
||||
for event in pygame.event.get():
|
||||
# 检查是否关闭
|
||||
for event in pygame.event.get(): # 这里已经获取了事件,事件队列被清空,在场景的update中再次获取将没有事件
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
self.scene.handle_event(event) # 当前场景接收事件
|
||||
|
||||
# 清屏
|
||||
self.screen.fill(GameConfig.bg_color)
|
||||
|
||||
# 场景每帧更新逻辑运行
|
||||
MainMenuScene(self.screen).update()
|
||||
self.scene.update()
|
||||
|
||||
# 更新显示
|
||||
pygame.display.update()
|
||||
|
||||
Reference in new issue
Block a user