【问题标题】:BUTTON NOT FUCTIONING REQUESTING [duplicate]按钮不起作用请求[重复]
【发布时间】:2021-06-25 22:09:50
【问题描述】:

所以我正在为我正在制作的独立游戏制作菜单屏幕。我对 pygame 了解不多,但觉得同时制作游戏和学习会很有趣。但由于某种原因,如果您将鼠标悬停在退出游戏按钮的左侧,它会亮起,我希望将其精确设置为按钮大小。


pygame.init()

white = (255, 255, 255)
black = (0, 0, 0)
button_light = (170, 170, 170)
button_dark = (100, 100, 100)

background = black
window = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Zero Dark Thirty")
window.fill(background)
pygame.display.flip()

width = window.get_width()
height = window.get_height()

small_font = pygame.font.SysFont('Corbel', 35)
quit_button = small_font.render('Exit Game', True, white)


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if width / 2 - 50 <= mouse[0] <= width / 2 + 140 and height / 2 - 8 <= mouse[1] <= height / 2 + 40:
                pygame.quit()
    mouse = pygame.mouse.get_pos()

    if width / 2 - 75 <= mouse[0] <= width / 2 + 140 and height / 2 - 8 <= mouse[1] <= height / 2 + 40:
        pygame.draw.rect(window, button_light, [width / 2 - 80, height / 2 - 10, 150, 40])

    else:
        pygame.draw.rect(window, button_dark, [width / 2 - 80, height / 2 - 10, 150, 40])

    window.blit(quit_button, (width / 2 - 75, height / 2 - 8))
    pygame.display.update()

【问题讨论】:

    标签: python button pygame helper


    【解决方案1】:

    在检查鼠标是否在按钮上的代码中,它显示width / 2 - 50,但在下面的代码中,它显示width / 2 - 75 尝试使它们相同,如果不起作用,请告诉我.

    【讨论】:

    • 当我这样做按钮不会点击时不起作用
    猜你喜欢
    • 1970-01-01
    • 2015-10-17
    • 2017-10-13
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2012-05-13
    相关资源
    最近更新 更多