鱼C论坛

 找回密码
 立即注册
查看: 275|回复: 1

[已解决]pygame导入系统字体后无法显示文字

[复制链接]
发表于 2024-12-28 09:55:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import pygame
  2. import random
  3. pygame.init()
  4. screen = pygame.display.set_mode([800, 600])
  5. pygame.display.set_caption("test1")
  6. font = pygame.font.SysFont("微软雅黑", 24)
  7. WHITE = (255, 255, 255)
  8. BLACK = (0, 0, 0)
  9. RED = (255, 0, 0)
  10. posx = 50
  11. posy = 50
  12. xvel = 3
  13. yvel = 3
  14. keep_going = True
  15. times = 0
  16. clock = pygame.time.Clock()
  17. while keep_going:
  18.     for event in pygame.event.get():
  19.         if event.type == pygame.QUIT:
  20.             keep_going = False
  21.     print_text = f"the hit times -- {times}"
  22.     text = font.render(print_text, True, WHITE)
  23.     text_rect = text.get_rect()
  24.     text_rect.centerx = screen.get_rect().centerx
  25.     text_rect.y = 50
  26.     screen.blit(text, text_rect)
  27.     posx += xvel
  28.     posy += yvel
  29.     if posx <= 50 or posx >= 750:
  30.         xvel = -xvel
  31.         times += 1
  32.     if posy <= 50 or posy >= 550:
  33.         yvel = -yvel
  34.         times += 1
  35.     screen.fill(BLACK)
  36.     pygame.draw.circle(screen, RED, (posx, posy), 50)
  37.     pygame.display.update()
  38.     clock.tick(60)
  39. pygame.quit()
复制代码


运行后屏幕上方没有显示文字,请求帮助
最佳答案
2024-12-28 09:55:13
以下是你代码中可能导致文字无法显示的问题以及解决方案:

1. **更新屏幕顺序错误:**
   你在更新屏幕内容(`pygame.display.update()`)之前调用了`screen.fill(BLACK)`,这会覆盖之前绘制的文字。因此,你需要先清屏,再绘制文字和其他元素,然后更新显示。

2. **`if`条件错误:**
   条件判断中的`=`应该是`==`。使用单个`=`进行赋值会导致语法错误。应当用`==`来进行条件判断。

修改后的代码如下:

  1. import pygame
  2. import random
  3. pygame.init()
  4. screen = pygame.display.set_mode((800, 600))  # 指定窗口大小
  5. pygame.display.set_caption("test1")
  6. font = pygame.font.SysFont("微软雅黑", 24)
  7. WHITE = (255, 255, 255)
  8. BLACK = (0, 0, 0)
  9. RED = (255, 0, 0)
  10. posx = 50
  11. posy = 50
  12. xvel = 3
  13. yvel = 3
  14. keep_going = True
  15. times = 0
  16. clock = pygame.time.Clock()
  17. while keep_going:
  18.     for event in pygame.event.get():
  19.         if event.type == pygame.QUIT:
  20.             keep_going = False

  21.     # 先填充背景色
  22.     screen.fill(BLACK)

  23.     # 更新文字
  24.     print_text = f"the hit times -- {times}"
  25.     text = font.render(print_text, True, WHITE)
  26.     text_rect = text.get_rect()
  27.     text_rect.centerx = screen.get_rect().centerx
  28.     text_rect.y = 50
  29.     screen.blit(text, text_rect)

  30.     # 更新圆的信息
  31.     posx += xvel
  32.     posy += yvel
  33.     if posx == 750 or posx == 0:  # 使用==进行比较
  34.         xvel = -xvel
  35.         times += 1
  36.     if posy == 550 or posy == 0:  # 使用==进行比较
  37.         yvel = -yvel
  38.         times += 1

  39.     # 绘制圆
  40.     pygame.draw.circle(screen, RED, (posx, posy), 50)
  41.    
  42.     # 更新显示内容
  43.     pygame.display.update()
  44.     clock.tick(60)
  45. pygame.quit()
复制代码


希望对你有所帮助!如果还有其他问题,请随时问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-12-28 09:55:13 | 显示全部楼层    本楼为最佳答案   
以下是你代码中可能导致文字无法显示的问题以及解决方案:

1. **更新屏幕顺序错误:**
   你在更新屏幕内容(`pygame.display.update()`)之前调用了`screen.fill(BLACK)`,这会覆盖之前绘制的文字。因此,你需要先清屏,再绘制文字和其他元素,然后更新显示。

2. **`if`条件错误:**
   条件判断中的`=`应该是`==`。使用单个`=`进行赋值会导致语法错误。应当用`==`来进行条件判断。

修改后的代码如下:

  1. import pygame
  2. import random
  3. pygame.init()
  4. screen = pygame.display.set_mode((800, 600))  # 指定窗口大小
  5. pygame.display.set_caption("test1")
  6. font = pygame.font.SysFont("微软雅黑", 24)
  7. WHITE = (255, 255, 255)
  8. BLACK = (0, 0, 0)
  9. RED = (255, 0, 0)
  10. posx = 50
  11. posy = 50
  12. xvel = 3
  13. yvel = 3
  14. keep_going = True
  15. times = 0
  16. clock = pygame.time.Clock()
  17. while keep_going:
  18.     for event in pygame.event.get():
  19.         if event.type == pygame.QUIT:
  20.             keep_going = False

  21.     # 先填充背景色
  22.     screen.fill(BLACK)

  23.     # 更新文字
  24.     print_text = f"the hit times -- {times}"
  25.     text = font.render(print_text, True, WHITE)
  26.     text_rect = text.get_rect()
  27.     text_rect.centerx = screen.get_rect().centerx
  28.     text_rect.y = 50
  29.     screen.blit(text, text_rect)

  30.     # 更新圆的信息
  31.     posx += xvel
  32.     posy += yvel
  33.     if posx == 750 or posx == 0:  # 使用==进行比较
  34.         xvel = -xvel
  35.         times += 1
  36.     if posy == 550 or posy == 0:  # 使用==进行比较
  37.         yvel = -yvel
  38.         times += 1

  39.     # 绘制圆
  40.     pygame.draw.circle(screen, RED, (posx, posy), 50)
  41.    
  42.     # 更新显示内容
  43.     pygame.display.update()
  44.     clock.tick(60)
  45. pygame.quit()
复制代码


希望对你有所帮助!如果还有其他问题,请随时问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-6 06:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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