Python 如何检测鼠标是否悬停在按钮上?PyGame按钮类未在悬停时显示文本或更改颜色
我在pygame中创建了一个button类,虽然按钮本身正在显示,但我的文本没有显示。我也有一些条件改变颜色时,鼠标是在按钮上。在我的主函数中使用硬编码值已经达到了预期的效果,但是我想使用一个类来处理我的各种按钮,因为我可能有很多按钮Python 如何检测鼠标是否悬停在按钮上?PyGame按钮类未在悬停时显示文本或更改颜色,python,python-3.x,class,pygame,Python,Python 3.x,Class,Pygame,我在pygame中创建了一个button类,虽然按钮本身正在显示,但我的文本没有显示。我也有一些条件改变颜色时,鼠标是在按钮上。在我的主函数中使用硬编码值已经达到了预期的效果,但是我想使用一个类来处理我的各种按钮,因为我可能有很多按钮 下面是我正在使用的一些类 字体 class Fonts: def __init__(self, font, antialias, text, color, x, y): self.font = font self.ant
下面是我正在使用的一些类
字体
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
状况
如果self.x
这是错误的。您必须评估x
和y
:
如果self.x
无论如何,对于碰撞测试,我建议:
button_rect=pygame.rect(self.x、self.y、self.width、self.height)
如果按钮直接碰撞点((x,y):
# [...]
按钮\u rect
可进一步用于绘制矩形:
pygame.draw.rect(屏幕,(211211211211)按钮)
通过使用属性rect
而不是单独的属性x
、y
、宽度
、高度
,代码可以简化很多:
class按钮:
def u uu init_uuuuu(自身、颜色、x、y、宽度、高度、文本、字体大小):
self.color=颜色
self.rect=pygame.rect(x,y,宽度,高度)
self.text=文本
self.fontsize=fontsize
self.courier\u按钮\u font=pygame.font.SysFont(“courier”,self.fontsize)
def抽取(自身,位置):
按钮颜色=(211211211211)如果self.rect.collidepoint(pos)else self.color
pygame.draw.rect(屏幕、按钮颜色、self.rect)
文本宽度,文本高度=self.courier\u按钮\u字体大小(self.text)
textpos=(self.rect.centerx-文本宽度//2,self.rect.centery-文本高度//2)
buttontext=字体(courier\u字体、True、self.text、黑色、textpos)
buttontext.draw(屏幕)
def isOver(自身,位置):
返回自校正碰撞点(位置)
谢谢,请确保删除代码最后一行的冒号。
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()