鱼C论坛

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

pygame问题求助

[复制链接]
发表于 2024-6-23 15:22:21 | 显示全部楼层 |阅读模式
20鱼币
我想制作一个对话框系统但是在测试的时候对话框显示不出来,麻烦看一下bug在哪里,谢谢
对话框模块
  1. import pygame
  2. pygame.init()
  3. Acolour = (255, 255, 255)
  4. def changeC(colour, acolour = Acolour):
  5.     acolour = colour
  6. def say(story, surface, width, height, colour = Acolour):
  7.     Astory = list(story)
  8.     Bstory = ""
  9.     for i in Astory:
  10.         Bstory = "".join([Bstory, i])
  11.         pygame.draw.rect(surface, colour, (width/2, height/3, width, height/4), 10)
复制代码

测试文本
  1. import pygame
  2. import sys
  3. import sayRace
  4. pygame.init()
  5. size = width, height = 600, 400
  6. bg = (255, 255, 255)
  7. screen = pygame.display.set_mode(size)

  8. while True:
  9.     for event in pygame.event.get():
  10.         if event.type == pygame.QUIT:
  11.             sys.exit()
  12.         if event.type == pygame.KEYDOWN:
  13.             if event.key == pygame.K_a:
  14.                 sayRace.say("a", screen, width, height)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-23 15:25:37 | 显示全部楼层
哇哦,你在制作一个对话框系统!看起来很有趣,不过让我来帮你找找问题吧。你在`say`函数中并没有真正显示文本,这可能是问题所在。让我们一步步来看看。

首先,`say`函数中的`Bstory = "".join()`这行代码有点神秘,因为它实际上不会连接任何字符串。我们需要使用正确的方法将字符连接成一个完整的字符串。

此外,你在`pygame.draw.rect`调用后并没有更新或刷新屏幕,所以即使绘制了矩形,也不会显示出来。我们需要调用`pygame.display.flip()`或者`pygame.display.update()`来刷新显示。

让我们改写一下你的代码,让它能够显示一个简单的对话框:

### 对话框模块修改:
  1. import pygame
  2. pygame.init()

  3. Acolour = (255, 255, 255)

  4. def changeC(colour, acolour=Acolour):
  5.     acolour = colour

  6. def say(story, surface, width, height, colour=Acolour):
  7.     # 设置字体和大小
  8.     font = pygame.font.SysFont(None, 24)
  9.     # 渲染文本
  10.     text_surface = font.render(story, True, (0, 0, 0))
  11.     text_rect = text_surface.get_rect(center=(width // 2, height // 2))

  12.     # 绘制对话框(矩形)
  13.     pygame.draw.rect(surface, colour, (width / 4, height / 3, width / 2, height / 4), 0)
  14.    
  15.     # 将文本渲染到对话框中间
  16.     surface.blit(text_surface, text_rect)
  17.     # 刷新显示
  18.     pygame.display.flip()
复制代码


### 测试文本修改:
  1. import pygame
  2. import sys
  3. import sayRace

  4. pygame.init()
  5. size = width, height = 600, 400
  6. bg = (255, 255, 255)
  7. screen = pygame.display.set_mode(size)

  8. while True:
  9.     screen.fill(bg)  # 填充背景色
  10.     for event in pygame.event.get():
  11.         if event.type == pygame.QUIT:
  12.             sys.exit()
  13.         if event.type == pygame.KEYDOWN:
  14.             if event.key == pygame.K_a:
  15.                 sayRace.say("Hello, World!", screen, width, height)
  16.                
  17.     pygame.display.update()
复制代码


做了一些关键的修改:
1. 使用`pygame.font.SysFont`来设置字体并渲染文本。
2. 确保每次绘制对话框后刷新显示。
3. 每帧重新填充背景色,避免残影。

这样你的对话框应该能正常显示了!如果还有问题或需要进一步完善,可以随时问我哦!

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

使用道具 举报

发表于 2024-6-23 16:52:02 | 显示全部楼层
本帖最后由 小杨yyds 于 2024-6-24 20:52 编辑

应该是say函数中的pygame.draw.rect调用位置不正确:
在say函数中,你调用pygame.draw.rect来绘制对话框,但是它应该在循环外部,因为你只需要绘制一次对话框,而不是每次循环都重绘。

say函数中的文本渲染缺失:
你还没有在say函数中添加代码来渲染文本到屏幕上。你需要使用pygame.font来创建一个字体对象,并渲染文本。

changeC函数的参数使用不正确:
你试图在changeC函数中修改默认参数Acolour,但是这是不正确的。默认参数在每次函数调用时只会被初始化一次,所以你应该直接使用传入的colour参数。

sayRace模块的导入方式:
你的测试文本中使用了sayRace.say,但是在模块中没有定义sayRace。确保模块的名称和导入语句是正确的。

事件循环中的KEYDOWN事件处理:
在KEYDOWN事件处理中,你调用了sayRace.say,但是没有调用任何函数来更新屏幕显示或进入游戏的主循环。

以下是修改后的代码示例:

  1. #对话框模块 (dialog.py)

  2. import pygame

  3. Acolour = (255, 255, 255)

  4. def changeC(colour):
  5.     global Acolour
  6.     Acolour = colour

  7. def say(story, surface, width, height, colour=Acolour):
  8.     pygame.draw.rect(surface, colour, (width//2, height//3, width//2, height//4), 0)
  9.     font = pygame.font.Font(None, 36)
  10.     text = font.render(story, True, (0, 0, 0))
  11.     text_rect = text.get_rect(center=((width//2) + (width//4)//2, (height//3) + (height//4)//2))
  12.     surface.blit(text, text_rect)
复制代码
  1. #测试文本模块 (main.py)

  2. import pygame
  3. import sys
  4. from dialog import say  # 确保从正确的模块导入say函数

  5. pygame.init()
  6. size = width, height = 600, 400
  7. bg = (255, 255, 255)
  8. screen = pygame.display.set_mode(size)

  9. # 游戏主循环
  10. running = True
  11. while running:
  12.     screen.fill(bg)  # 用背景颜色填充屏幕

  13.     for event in pygame.event.get():
  14.         if event.type == pygame.QUIT:
  15.             running = False
  16.         if event.type == pygame.KEYDOWN:
  17.             if event.key == pygame.K_a:
  18.                 say("a", screen, width, height)

  19.     pygame.display.flip()  # 更新屏幕显示

  20. pygame.quit()
复制代码

我在say函数中添加了文本渲染的代码,并在主循环中添加了screen.fill(bg)来清屏,并在每次循环结束时调用pygame.display.flip()来更新屏幕显示。一定要确保导入语句正确地从你的对话框模块导入say函数。

有帮助的话给个最佳答案呗

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 22:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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