鱼C论坛

 找回密码
 立即注册
查看: 1096|回复: 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()

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-2 08:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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