添加一些注释、优化代码显示

This commit is contained in:
chicrobot committed 2026-07-27 23:04:24 +08:00
1 parent 805704f0e5
commit eefb8da9c6
3 files changed
+36 -10

No files matched your search

+2 -1
View File
@@ -13,4 +13,5 @@ def data_path():
data_path = os.path.join(project_path, "data")
return data_path
print(data_path())
if __name__ == '__main__':
print(data_path())
+12
View File
@@ -2,15 +2,27 @@ import pygame
class BaseScene:
def __init__(self, screen: pygame.Surface, clock: pygame.time.Clock):
"""
初始化场景,接收并保存画布、计时器
"""
self.screen = screen
self.clock = clock
self.next_scene = None
def update(self):
"""
场景的每帧更新逻辑
"""
pass
def handle_event(self, events):
"""
接收主循环传来的事件并处理
"""
pass
def switch_to_scene(self, scene_class):
"""
切换场景
"""
self.next_scene = scene_class(self.screen, self.clock)
+22 -9
View File
@@ -9,24 +9,28 @@ from src.scenes.game_scene import GameScene
class MainMenuScene(BaseScene):
"""
主菜单场景,显示提示信息,展示雨天特效
"""
def __init__(self, screen, clock):
"""
初始化函数,初始化按钮按下状态、雨滴列表
"""
super().__init__(screen, clock)
self.button_status = False
self.debug_status = False
# 设置字体
font_file = os.path.join(data_path(),"Dymon-ShouXieTi.otf")
self.font = pygame.font.Font(font_file,16)
# 文本
self.game_info_text = self.font.render('按任意键开始游戏,鼠标点击退出', True, Color.white)
# 雨滴列表
self.rain_max = 50
self.rain_list = []
# 雨滴生命周期
self.rain_live_max = 6
# 设置字体
font_file = os.path.join(data_path(),"Dymon-ShouXieTi.otf")
self.font = pygame.font.Font(font_file,16)
# 文本
self.game_info_text = self.font.render('按任意键开始游戏,鼠标点击退出', True, Color.white)
# 初始化雨滴
self.init_rain(self.rain_max)
@@ -36,13 +40,13 @@ class MainMenuScene(BaseScene):
self.switch_to_scene(GameScene)
# 显示提示文本
screen_rect = self.screen.get_rect() # 获取屏幕矩形
text_rect = self.game_info_text.get_rect() # 获取文本矩形
self.__show(text_rect, (0, 15))
text_rect.center = screen_rect.center # 将文本居中到屏幕
self.screen.blit(self.game_info_text, text_rect.topleft)
# 调试信息,debug模式下显示
self.__show(screen_rect)
self.__show(text_rect,(0,30))
self.__show(text_rect.center,(0,45))
@@ -51,12 +55,18 @@ class MainMenuScene(BaseScene):
self.rain_fall()
def handle_event(self, event):
"""
接收主循环传来的事件并处理
"""
if event.type == pygame.KEYDOWN:
self.button_status = True
elif event.type == pygame.MOUSEBUTTONDOWN:
return "exit" # 主循环接收返回,为exit时设置running为False,退出游戏
def __show(self, info, dest=(0, 0)):
"""
显示调试文本
"""
if not self.debug_status:
return
if not isinstance(info, str):
@@ -65,6 +75,9 @@ class MainMenuScene(BaseScene):
self.screen.blit(info_text, dest)
def __rain_list_append(self, status=0):
"""
向列表中添加一个雨滴,默认状态为0
"""
x = random.randint(0, int(GameConfig.window_x))
y = random.randint(0, int(GameConfig.window_y))
rain = {