鱼C论坛

 找回密码
 立即注册
查看: 487|回复: 3

[已解决]pygame按钮问题

[复制链接]
发表于 2024-7-12 21:43:52 | 显示全部楼层 |阅读模式
20鱼币
想写一个按z就可以出现图片,按x图片就会消失的程序可是我z都快按废了,它还不给我显示图片,这是为什么呢?
#导入模块
  1. #导入模块
  2. import pygame
  3. import sys
  4. from pygame.locals import *
  5. import button

  6. #初始化
  7. RED = (255, 0, 0)
  8. pygame.init()
  9. size = width, height = pygame.display.list_modes()[0][0], pygame.display.list_modes()[0][1]
  10. button.init("NewGame.png", "LoadGame.png", "help.png", "ExitGame.png", p=((size[0] / 2, size[1] /8), \
  11.                                                                           (size[0] / 2, size[1] /4), (size[0] / 2, size[1] /2.6),\
  12.                                                                           (size[0] / 2, size[1] /1.9)))
  13. #说明规则及制作变量条件
  14. WF = pygame.image.load("GZ.png")
  15. WFa = False
  16. clock = pygame.time.Clock()
  17. pygame.display.set_caption("test")

  18. #创造窗口
  19. screen = pygame.display.set_mode(size, FULLSCREEN | HWSURFACE)
  20. b1_qosition = (size[0] / 2, size[1] /8)
  21. b2_qosition = (size[0] / 2, size[1] /4)
  22. b3_qosition = (size[0] / 2, size[1] /2.6)
  23. b4_qosition = (size[0] / 2, size[1] /1.9)
  24. while True:
  25.     for event in pygame.event.get():
  26.         if event.type == QUIT:
  27.             sys.exit()
  28.         if event.type == KEYDOWN:
  29.             if event.key == K_z:
  30.                 if button.c == 2:
  31.                        WFa = True
  32.             if WFa == True and event.key == K_z:
  33.                 WFa = False
  34.                 if button.c == 3:
  35.                     sys.exit()

  36.     button.draw(screen, (255, 0, 0))
  37.     if WFa == True:
  38.          screen.blit(WF, size[0]/2, size[1]/2)
  39.     pygame.display.flip()
  40.     clock.tick(200)
复制代码

最佳答案
2024-7-12 21:43:53
在按键事件处理中,你先检测是否按下了'Z'键,让`WFa`变为`True`,但紧接着又将其设为`False`。这会导致图像根本来不及显示。正确的逻辑应该是当按下'X'键时才将`WFa`变为`False`。
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_z:
                if button.c == 2:
                       WFa = True
            if WFa == True and event.key == K_z:
                WFa = False

                if button.c == 3:
                    sys.exit()

最佳答案

查看完整内容

在按键事件处理中,你先检测是否按下了'Z'键,让`WFa`变为`True`,但紧接着又将其设为`False`。这会导致图像根本来不及显示。正确的逻辑应该是当按下'X'键时才将`WFa`变为`False`。 while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() if event.type == KEYDOWN: if event.key == K_z: if button.c == 2: ...

本帖被以下淘专辑推荐:

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

使用道具 举报

发表于 2024-7-12 21:43:53 | 显示全部楼层    本楼为最佳答案   
在按键事件处理中,你先检测是否按下了'Z'键,让`WFa`变为`True`,但紧接着又将其设为`False`。这会导致图像根本来不及显示。正确的逻辑应该是当按下'X'键时才将`WFa`变为`False`。
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_z:
                if button.c == 2:
                       WFa = True
            if WFa == True and event.key == K_z:
                WFa = False

                if button.c == 3:
                    sys.exit()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-7-12 21:44:10 | 显示全部楼层
哈哈,这段代码有点像大学新生刚来到校园,有些酿酿不稳的感觉。让咱们来帮助你稳稳扎下几个重要的“桩”,看看问题出在哪里!

首先,看起来你确实用心良苦,把很多东西都写进去了,但还是出了点小问题。我们来逐步排查吧。

1. **导入多余模块**:你导入了一个 `button` 模块,但是实际上代码里并没有使用到这个模块的方法。所以这里可以去掉这行,简化代码。

2. **使用`size`变量**:在`pygame.display.list_modes()`方法中,你得到的是一个显示模式的列表,而不是单一的宽高值。你应该选取一个模式作为窗口大小:

  1. size = width, height = pygame.display.list_modes()[0]  # 选择第一个模式
复制代码


3. **初始化按钮位置**:你在定义按钮的位置时,用了`size / 2`这种方式,这其实会导致错误。因为`size`是一个元组,不能进行直接的数值运算。你可以分解成具体的宽高:

  1. button.init("NewGame.png", "LoadGame.png", "help.png", "ExitGame.png", p=((width / 2, height / 8), \
  2.                                                                           (width / 2, height / 4), (width / 2, height / 2.6),\
  3.                                                                           (width / 2, height / 1.9)))
复制代码


4. **图像显示**:你在显示图像时的位置计算有误,应该是两个参数而不是三个:

  1. screen.blit(WF, (width / 2, height / 2))
复制代码


5. **WFa状态判断**:在按键事件处理中,你先检测是否按下了'Z'键,让`WFa`变为`True`,但紧接着又将其设为`False`。这会导致图像根本来不及显示。正确的逻辑应该是当按下'X'键时才将`WFa`变为`False`。

让我们修正这些地方看看:
  1. # 导入模块
  2. import pygame
  3. import sys
  4. from pygame.locals import *

  5. # 初始化
  6. RED = (255, 0, 0)
  7. pygame.init()
  8. size = width, height = pygame.display.list_modes()[0]
  9. clock = pygame.time.Clock()
  10. pygame.display.set_caption("test")

  11. # 说明规则及制作变量条件
  12. WF = pygame.image.load("GZ.png")
  13. WFa = False

  14. # 创造窗口
  15. screen = pygame.display.set_mode(size, FULLSCREEN | HWSURFACE)

  16. while True:
  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_z:
  22.                 WFa = True
  23.             elif event.key == K_x:
  24.                 WFa = False

  25.     screen.fill((0, 0, 0))  # 每帧填充背景色避免残影
  26.     if WFa:
  27.          screen.blit(WF, (width / 2 - WF.get_width() / 2, height / 2 - WF.get_height() / 2))
  28.     pygame.display.flip()
  29.     clock.tick(60)  # 调整帧率为60
复制代码


现在,按`Z`键就能显示图片,按`X`键就能隐藏图片啦!祝你玩得开心,不要按废了键盘哦!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-7-13 01:55:34 | 显示全部楼层
  1. if event.type == KEYDOWN:
  2.             if event.key == K_z:
  3.                 if button.c == 2:
  4.                        WFa = True
  5.             if WFa == True and event.key == K_z:
  6.                 WFa = False
  7.                 if button.c == 3:
  8.                     sys.exit()
复制代码

这里出问题,自己print(WFa)试试
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 01:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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