实现多场景来回切换

This commit is contained in:
2026-07-27 23:51:23 +08:00
parent eefb8da9c6
commit 6050bca933
5 changed files with 51 additions and 8 deletions
+8
View File
@@ -0,0 +1,8 @@
from src.scenes.main_menu_scene import MainMenuScene
from src.scenes.game_scene import GameScene
scene_dict = {
"MainMenuScene": MainMenuScene,
"GameScene": GameScene,
}
+2 -2
View File
@@ -21,8 +21,8 @@ class BaseScene:
"""
pass
def switch_to_scene(self, scene_class):
def switch_to_scene(self, scene_name):
"""
切换场景
"""
self.next_scene = scene_class(self.screen, self.clock)
self.next_scene = scene_name
+32 -2
View File
@@ -1,12 +1,24 @@
import os
import pygame
from src.config.game_config import GameConfig
from src.config.game_config import data_path, GameConfig
from src.scenes.base_scene import BaseScene
class GameScene(BaseScene):
name = "GameScene"
def __init__(self, screen, clock):
super().__init__(screen, clock)
# 初始化音乐
pygame.mixer.init()
# 播放并循环音乐
pygame.mixer_music.load(os.path.join(data_path(), "vespidaze-upbeat-rpg-battle-460971.mp3"))
pygame.mixer_music.play(-1)
self.back_status = False
# 状态值
self.dirty_x = 400
self.dirty_y = 300
@@ -21,6 +33,8 @@ class GameScene(BaseScene):
self.BLUE = (0, 0, 255)
def update(self):
if self.back_status:
self.switch_to_scene("MainMenuScene")
if self.Left_status and self.dirty_x >= 0:
self.dirty_x -= 1
if self.right_status and self.dirty_x <= GameConfig.window_x - 20:
@@ -29,11 +43,26 @@ class GameScene(BaseScene):
self.dirty_y -= 1
if self.down_status and self.dirty_y <= GameConfig.window_y - 30:
self.dirty_y += 1
# 填充窗口颜色
self.screen.fill((255, 255, 255))
# 绘制矩形(移动体和固定体)
dirty = pygame.draw.rect(self.screen, self.BLUE, [self.dirty_x, self.dirty_y, 20, 30])
enemy = pygame.draw.rect(self.screen, self.RED, [40, 60, 10, 10])
# 碰撞监测
if dirty.colliderect(enemy):
print("die")
self.dirty_x = 400
self.dirty_y = 300
dirty = pygame.draw.rect(self.screen, self.BLUE, [self.dirty_x, self.dirty_y, 20, 30])
def handle_event(self, event):
# 按下按键
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
if event.key == pygame.K_ESCAPE:
self.back_status = True
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
self.Left_status = True
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self.right_status = True
@@ -41,6 +70,7 @@ class GameScene(BaseScene):
self.up_status = True
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
self.down_status = True
# 松开按键
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
self.Left_status = False
+2 -2
View File
@@ -5,10 +5,10 @@ import pygame
from src.config.color import Color
from src.config.game_config import data_path, GameConfig
from src.scenes.base_scene import BaseScene
from src.scenes.game_scene import GameScene
class MainMenuScene(BaseScene):
name = "MainMenuScene"
"""
主菜单场景,显示提示信息,展示雨天特效
"""
@@ -37,7 +37,7 @@ class MainMenuScene(BaseScene):
def update(self):
# 按键按下则切换场景
if self.button_status:
self.switch_to_scene(GameScene)
self.switch_to_scene("GameScene")
# 显示提示文本
screen_rect = self.screen.get_rect() # 获取屏幕矩形
+7 -2
View File
@@ -1,6 +1,7 @@
import pygame
from src.config.game_config import GameConfig
from src.config.scenes import scene_dict
from src.scenes.main_menu_scene import MainMenuScene
@@ -16,8 +17,10 @@ class Game:
# 设置计时器
self.clock = pygame.time.Clock()
# 场景字典
self.game_scenes = scene_dict
# 当前场景
self.scene = MainMenuScene(self.screen, self.clock)
self.scene = self.game_scenes["MainMenuScene"](self.screen, self.clock)
# 运行循环
self.running = True
@@ -35,7 +38,9 @@ class Game:
# 检查是否切换场景
if self.scene.next_scene is not None:
self.scene = self.scene.next_scene
if self.scene.next_scene in self.game_scenes.keys():
scene_class = self.game_scenes[self.scene.next_scene]
self.scene = scene_class(self.screen, self.clock)
# 清屏
self.screen.fill(GameConfig.bg_color)