![](/img/trans.png)
[英]How do i make a text box pop up when i hover over a button in pygame
[英]How do I detect if the mouse is hovering over a button? PyGame button class is not displaying the text or changing colour on hover
我在 pygame 中创建了一个按钮类,虽然按钮本身正在显示,但我的文本没有显示出来。 当鼠标悬停在按钮上时,我还有一些条件可以改变颜色。 我在主函数中使用硬编码值实现了预期的结果,但是我想使用一个类来处理我的各种按钮,因为我可能有很多按钮。
字体
class Fonts:
def __init__(self, font, antialias, text, color, x, y):
self.font = font
self.antialias = antialias
self.text = text
self.color = color
self.x = x
self.y = y
def draw(self, win):
win.blit(self.font.render(self.text, self.antialias, (self.color)), (self.x, self.y))
按钮
class Button:
def __init__(self, color, x, y, width, height, text, fontsize):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.fontsize = fontsize
self.courier_button_font = pygame.font.SysFont("Courier", self.fontsize)
def draw(self, pos):
x, y = pos
text_width, text_height = self.courier_button_font.size(self.text)
if self.x < x < self.width and self.y < y < self.height:
pygame.draw.rect(screen, (211, 211, 211), (self.x, self.y, self.width, self.height))
buttontext = Fonts(courier_font, True, f"{self.text}", BLACK,
int(self.width / 2 - text_width / 2), int(self.height / 2 - text_height / 2))
buttontext.draw(screen)
else:
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
buttontext = Fonts(courier_font, True, f"{self.text}", BLACK,
(self.width / 2 - text_width / 2), (self.height / 2 - text_height / 2))
buttontext.draw(screen)
def isOver(self, pos):
x, y = pos
if self.x < x < self.width and self.y < y < self.height:
return True
else:
pass
FireTowerButton = Button(BLACK, 810, 200, 80, 30, "Fire Tower", 20)
def ShowTowers(mouse):
mousex, mousey = mouse
screen.fill((147, 207, 14))
FireTowerButton.draw(mouse)
def main():
run = True
screen.fill(BLACK)
# screen.blit(enemy1, (90, 90))
while run:
mouse = pygame.mouse.get_pos()
ShowTowers(mouse)
screen.blit(money_image, (790, 0))
screen.blit(background_image, (0, 0))
enemy1_spawn(wave_number)
pygame.draw.rect(screen, BLACK, (770, 75, 30, 90))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
clock.tick(FPS)
pygame.display.update()
条件
if self.x < x < self.width and self.y < y < self.height:
是错的。 你必须评估是否x < self.x + self.width
和y < self.y + self.height
:
if self.x < x < self.x + self.width and self.y < y < self.y + self.height:
# [...]
无论如何,我建议使用pygame.Rect
/ collidepoint()
进行碰撞测试:
button_rect = pygame.Rect(self.x, self.y, self.width, self.height)
if button_rect.collidepoint((x, y):
# [...]
button_rect
可以进一步用于绘制矩形:
pygame.draw.rect(screen, (211, 211, 211), button_rect)
通过使用属性rect
而不是单独的属性x
、 y
、 width
、 height
,可以大大简化代码:
class Button:
def __init__(self, color, x, y, width, height, text, fontsize):
self.color = color
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.fontsize = fontsize
self.courier_button_font = pygame.font.SysFont("Courier", self.fontsize)
def draw(self, pos):
button_color = (211, 211, 211) if self.rect.collidepoint(pos) else self.color
pygame.draw.rect(screen, button_color, self.rect)
text_width, text_height = self.courier_button_font.size(self.text)
textpos = (self.rect.centerx - text_width // 2, self.rect.centery - text_height // 2)
buttontext = Fonts(courier_font, True, self.text, BLACK, textpos)
buttontext.draw(screen)
def isOver(self, pos):
return self.rect.collidepoint(pos)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.