flyps 发表于 2024-2-25 17:40:37

pygame字体问题

import pygame
import sys
from pygame.locals import *
pygame.init()
size = width, height = 600, 400
white = (255, 255, 255)
fullscreen = False
modes = pygame.display.list_modes()
clock = pygame.time.Clock()
bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (650, 450))
pygame.display.set_caption("MyUndertale")
screen = pygame.display.set_mode(size)
bg_position = size // 2 - 300, size // 2 - 200
F4_font = pygame.font.Font(None, 20)

while True:
    screen.blit(bg, bg_position)
    for event in pygame.event.get():
      if event.type == QUIT:
            sys.exit()
      if event.type == KEYDOWN:
            if event.key == K_F4:
                fullscreen = not fullscreen
                if fullscreen:
                  bg_position = modes// 2 - 960, modes // 2 - 700
                  bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (1950, 1350))
                  screen = pygame.display.set_mode(modes, FULLSCREEN | HWSURFACE)
                else:
                  bg_position = size // 2 - 300, size // 2 - 200
                  bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (650, 450))
                  screen = pygame.display.set_mode(size)
                  F4_text = F4_font.render("请按下F4变成全屏", True, white)
                  screen.blit(F4_text, (200, 200))
   
    pygame.display.flip()
    clock.tick(100)
    #pygame.time.delay(10)
问为什么文字"请按下F4变成全屏"打不出来?

歌者文明清理员 发表于 2024-2-25 17:40:38

默认字体None不支持中文
字体你改成pygame.font.SysFont("字体", 大小)
pygame.font.get_fonts()也很好用

FishC 发表于 2024-2-25 17:40:56

问题出在代码的位置。你将绘制 "请按下F4变成全屏" 的代码放在了事件监听的内部,当只有按下F4键时才会执行该代码。所以在按下F4键之前,这段代码是不会被执行到的。这就导致了文字没有显示出来的问题。

要解决这个问题,将绘制文字的代码移到事件监听的外部即可。以下是修改后的代码:

import pygame
import sys
from pygame.locals import *

pygame.init()
size = width, height = 600, 400
white = (255, 255, 255)
fullscreen = False
modes = pygame.display.list_modes()
clock = pygame.time.Clock()
bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (650, 450))
pygame.display.set_caption("MyUndertale")
screen = pygame.display.set_mode(size)
bg_position = size // 2 - 300, size // 2 - 200
F4_font = pygame.font.Font(None, 20)
F4_text = F4_font.render("请按下F4变成全屏", True, white)# 将绘制文字的代码移到此处

while True:
    screen.blit(bg, bg_position)
    screen.blit(F4_text, (200, 200))# 绘制文字
    for event in pygame.event.get():
      if event.type == QUIT:
            sys.exit()
      if event.type == KEYDOWN:
            if event.key == K_F4:
                fullscreen = not fullscreen
                if fullscreen:
                  bg_position = modes // 2 - 960, modes // 2 - 700
                  bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (1950, 1350))
                  screen = pygame.display.set_mode(modes, FULLSCREEN | HWSURFACE)
                else:
                  bg_position = size // 2 - 300, size // 2 - 200
                  bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (650, 450))
                  screen = pygame.display.set_mode(size)
   
    pygame.display.flip()
    clock.tick(100)

修改后,无论是否按下F4键,文字都会被显示出来。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: pygame字体问题