添加字体与文本显示

This commit is contained in:
chicrobot committed 2026-07-26 03:55:27 +08:00
1 parent 97b870d4bf
commit 2d0cea6ec3
3 files changed
+45 -3

No files matched your search

Binary file not shown.
+12 -1
View File
@@ -1,5 +1,16 @@
import os
class GameConfig:
game_name = "Test Game"
window_x = 1920/2
window_y = 1080/2
bg_color = (0, 0, 0)
bg_color = (0, 0, 0)
def data_path():
file_path = os.path.abspath(__file__)
project_path = os.path.dirname(os.path.dirname(os.path.dirname(file_path)))
data_path = os.path.join(project_path, "data")
return data_path
print(data_path())
+33 -2
View File
@@ -1,5 +1,8 @@
import os
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
@@ -8,14 +11,42 @@ class MainMenuScene(BaseScene):
def __init__(self, screen):
super().__init__(screen)
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)
def update(self):
pygame.draw.rect(self.screen, Color.white, [10,20,30,40])
# 按键按下则切换场景
if self.button_status:
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)
self.__show(screen_rect)
self.__show(text_rect,(0,30))
self.__show(text_rect.center,(0,45))
def handle_event(self, event):
if event.type == pygame.KEYDOWN:
self.button_status = True
if event.type == pygame.KEYUP:
self.button_status = False
self.button_status = False
def __show(self, info, dest=(0, 0)):
if not self.debug_status:
return
if not isinstance(info, str):
info = str(info)
info_text = self.font.render(info, True, Color.white)
self.screen.blit(info_text, dest)