From 15f734b1d53f80d75f40df49ac5156d27111fd48 Mon Sep 17 00:00:00 2001 From: chicrobot Date: Sun, 26 Jul 2026 02:33:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=BA=E6=99=AF=E8=8E=B7=E5=8F=96=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=EF=BC=9A=E4=B8=BB=E5=BE=AA=E7=8E=AF=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=90=8E=EF=BC=8C=E4=BA=8B=E4=BB=B6=E8=A2=AB?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=EF=BC=8C=E5=9C=A8=E5=9C=BA=E6=99=AF=E4=B8=AD?= =?UTF-8?q?=E5=86=8D=E6=AC=A1=E8=8E=B7=E5=8F=96=E5=B0=86=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E3=80=82=E6=89=80=E4=BB=A5=E5=9C=A8=E4=B8=BB=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=97=B6=E5=8F=91=E9=80=81=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E7=BB=99=E5=9C=BA=E6=99=AF=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scenes/base_scene.py | 6 ++++++ src/scenes/main_menu_scene.py | 13 ++++++++++++- start.py | 9 ++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/scenes/base_scene.py b/src/scenes/base_scene.py index 8ac5734..1759103 100644 --- a/src/scenes/base_scene.py +++ b/src/scenes/base_scene.py @@ -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 \ No newline at end of file diff --git a/src/scenes/main_menu_scene.py b/src/scenes/main_menu_scene.py index df928a7..1cfaeb7 100644 --- a/src/scenes/main_menu_scene.py +++ b/src/scenes/main_menu_scene.py @@ -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]) \ No newline at end of file + 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 \ No newline at end of file diff --git a/start.py b/start.py index c788cb2..856d05c 100644 --- a/start.py +++ b/start.py @@ -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()