(Pygame) 鼠标悬停检测问题
(Pygame) Problems with Mouseover Detection
我都看过 these posts,但鼠标悬停检测仍未正常工作。我正在为我正在为朋友开发的游戏开发一个简单的开始菜单;有两段文字,悬停在上面时应该会变成蓝色。
但是,当我将鼠标悬停在左上角时,它们只会变成蓝色:我假设我的代码正在检测左上角的原始(未位图和未定位)表面,并将 that 成一个矩形,然后检查 .collidepoint(pygame.mouse.get_pos()).
如何让它检测到已经 blit 和定位的文本?
这是我的代码(或者至少是造成问题的部分):
font = pygame.font.Font(os.path.join('.', 'bin', 'NOVEMBER.TTF'), 26)
playText = font.render("Play", True, lightGray)
settingsText = font.render("Options", True, lightGray)
setDisplay.fill(darkGray)
playText_rect = playText.get_rect()
settingsText_rect = settingsText.get_rect()
然后,在我的主循环中:
if settingsText_rect.collidepoint(pygame.mouse.get_pos()):
settingsText = font.render("Options", True, grayBlue)
setDisplay.blit(settingsText, (rightBorder / 2 - settingsText.get_width() / 2 + 200, bottomBorder / 2 - settingsText.get_height() / 2 + 120))
elif playText_rect.collidepoint(pygame.mouse.get_pos()):
playText = font.render("Play", True, grayBlue)
setDisplay.blit(playText, (rightBorder / 2 - playText.get_width() / 2 - 200, bottomBorder / 2 - playText.get_height() / 2 + 120))
else:
playText = font.render("Play", True, lightGray)
settingsText = font.render("Options", True, lightGray)
哦,如果它有所作为,我会加入 Ubuntu。
当您简单地在 Surface
上调用 .get_rect()
时,结果 Rect
确实将其 x
和 y
位置设置为 0
.
解决这个问题的一个简单方法是使用 playText_rect
和 settingsText_rect
进行 blitting,而不是在主循环中计算位置。
# calculate the position once and but the rect at that position
playText_rect = playText.get_rect(topleft=(rightBorder / 2 - playText.get_width() / 2 - 200, bottomBorder / 2 - playText.get_height() / 2 + 120))
settingsText_rect = settingsText.get_rect(topleft=(rightBorder / 2 - settingsText.get_width() / 2 + 200, bottomBorder / 2 - settingsText.get_height() / 2 + 120))
...
# use the rect as position argument for blit
setDisplay.blit(settingsText, settingsText_rect)
...
setDisplay.blit(playText, playText_rect)
我都看过 these posts,但鼠标悬停检测仍未正常工作。我正在为我正在为朋友开发的游戏开发一个简单的开始菜单;有两段文字,悬停在上面时应该会变成蓝色。
但是,当我将鼠标悬停在左上角时,它们只会变成蓝色:我假设我的代码正在检测左上角的原始(未位图和未定位)表面,并将 that 成一个矩形,然后检查 .collidepoint(pygame.mouse.get_pos()).
如何让它检测到已经 blit 和定位的文本?
这是我的代码(或者至少是造成问题的部分):
font = pygame.font.Font(os.path.join('.', 'bin', 'NOVEMBER.TTF'), 26)
playText = font.render("Play", True, lightGray)
settingsText = font.render("Options", True, lightGray)
setDisplay.fill(darkGray)
playText_rect = playText.get_rect()
settingsText_rect = settingsText.get_rect()
然后,在我的主循环中:
if settingsText_rect.collidepoint(pygame.mouse.get_pos()):
settingsText = font.render("Options", True, grayBlue)
setDisplay.blit(settingsText, (rightBorder / 2 - settingsText.get_width() / 2 + 200, bottomBorder / 2 - settingsText.get_height() / 2 + 120))
elif playText_rect.collidepoint(pygame.mouse.get_pos()):
playText = font.render("Play", True, grayBlue)
setDisplay.blit(playText, (rightBorder / 2 - playText.get_width() / 2 - 200, bottomBorder / 2 - playText.get_height() / 2 + 120))
else:
playText = font.render("Play", True, lightGray)
settingsText = font.render("Options", True, lightGray)
哦,如果它有所作为,我会加入 Ubuntu。
当您简单地在 Surface
上调用 .get_rect()
时,结果 Rect
确实将其 x
和 y
位置设置为 0
.
解决这个问题的一个简单方法是使用 playText_rect
和 settingsText_rect
进行 blitting,而不是在主循环中计算位置。
# calculate the position once and but the rect at that position
playText_rect = playText.get_rect(topleft=(rightBorder / 2 - playText.get_width() / 2 - 200, bottomBorder / 2 - playText.get_height() / 2 + 120))
settingsText_rect = settingsText.get_rect(topleft=(rightBorder / 2 - settingsText.get_width() / 2 + 200, bottomBorder / 2 - settingsText.get_height() / 2 + 120))
...
# use the rect as position argument for blit
setDisplay.blit(settingsText, settingsText_rect)
...
setDisplay.blit(playText, playText_rect)