【发布时间】: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