鱼C论坛

 找回密码
 立即注册
查看: 290|回复: 2

[已解决]pygame字体问题

[复制链接]
发表于 2024-2-25 17:40:37 | 显示全部楼层 |阅读模式
20鱼币
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. pygame.init()
  5. size = width, height = 600, 400
  6. white = (255, 255, 255)
  7. fullscreen = False
  8. modes = pygame.display.list_modes()
  9. clock = pygame.time.Clock()
  10. bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (650, 450))
  11. pygame.display.set_caption("MyUndertale")
  12. screen = pygame.display.set_mode(size)
  13. bg_position = size[0] // 2 - 300, size[1] // 2 - 200
  14. F4_font = pygame.font.Font(None, 20)

  15. while True:
  16.     screen.blit(bg, bg_position)
  17.     for event in pygame.event.get():
  18.         if event.type == QUIT:
  19.             sys.exit()
  20.         if event.type == KEYDOWN:
  21.             if event.key == K_F4:
  22.                 fullscreen = not fullscreen
  23.                 if fullscreen:
  24.                     bg_position = modes[0][0]  // 2 - 960, modes[0][1] // 2 - 700
  25.                     bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (1950, 1350))
  26.                     screen = pygame.display.set_mode(modes[0], FULLSCREEN | HWSURFACE)
  27.                 else:
  28.                     bg_position = size[0] // 2 - 300, size[1] // 2 - 200
  29.                     bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (650, 450))
  30.                     screen = pygame.display.set_mode(size)
  31.                     F4_text = F4_font.render("请按下F4变成全屏", True, white)
  32.                     screen.blit(F4_text, (200, 200))
  33.    
  34.     pygame.display.flip()
  35.     clock.tick(100)
  36.     #pygame.time.delay(10)
复制代码

问为什么文字"请按下F4变成全屏"打不出来?
最佳答案
2024-2-25 17:40:38
默认字体None不支持中文
字体你改成pygame.font.SysFont("字体", 大小)
pygame.font.get_fonts()也很好用

最佳答案

查看完整内容

默认字体None不支持中文 字体你改成pygame.font.SysFont("字体", 大小) pygame.font.get_fonts()也很好用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-2-25 17:40:38 | 显示全部楼层    本楼为最佳答案   
默认字体None不支持中文
字体你改成pygame.font.SysFont("字体", 大小)
pygame.font.get_fonts()也很好用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

  16. while True:
  17.     screen.blit(bg, bg_position)
  18.     screen.blit(F4_text, (200, 200))  # 绘制文字
  19.     for event in pygame.event.get():
  20.         if event.type == QUIT:
  21.             sys.exit()
  22.         if event.type == KEYDOWN:
  23.             if event.key == K_F4:
  24.                 fullscreen = not fullscreen
  25.                 if fullscreen:
  26.                     bg_position = modes // 2 - 960, modes // 2 - 700
  27.                     bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (1950, 1350))
  28.                     screen = pygame.display.set_mode(modes, FULLSCREEN | HWSURFACE)
  29.                 else:
  30.                     bg_position = size // 2 - 300, size // 2 - 200
  31.                     bg = pygame.transform.smoothscale(pygame.image.load("waterfall.jpg"), (650, 450))
  32.                     screen = pygame.display.set_mode(size)
  33.    
  34.     pygame.display.flip()
  35.     clock.tick(100)
复制代码


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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 06:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表