如何解决按钮无效请求
所以我正在为我正在制作的独立游戏制作菜单屏幕,我对 pygame 不太了解,觉得制作游戏并同时学习会很有趣。但出于某种原因,如果您将鼠标悬停在退出游戏按钮的左侧,它会亮起,我希望将其设置为与按钮大小完全一致。
pygame.init()
white = (255,255,255)
black = (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,40])
window.blit(quit_button,(width / 2 - 75,height / 2 - 8))
pygame.display.update()
解决方法
在检查鼠标是否在按钮上方的代码中,它说 width / 2 - 50
但在下面的代码中,它说 width / 2 - 75
尝试使它们相同,如果没有'工作告诉我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。