因此,我编写了一个函数,当我将鼠标悬停在按钮上时,该函数可以更改光标。我写了它,以便可以将图片用作按钮并将其放置在我想要的任何位置。它适用于一个按钮,但是当我添加多个按钮时,光标会不断地来回切换。我猜要怪“如果”的说法,有什么想法吗?谢谢。
def hover_button(variable_name, scale_percentage, x, y):
mouse_position = pygame.mouse.get_pos()
image_width = pygame.Surface.get_width(variable_name)
image_height = pygame.Surface.get_height(variable_name)
new_width = int(image_width * (scale_percentage/100))
new_height = int(image_height * (scale_percentage/100))
variable_name = pygame.transform.scale(variable_name,(new_width,new_height))
rectang = variable_name.get_rect()
rectangm = rectang.move((x,y))
display.blit(variable_name, rectangm)
if rectangm.collidepoint(mouse_position):
print("mouse is over 'Button'")
_HAND_CURSOR = (
" XX ",
" X..X ",
" X..X ",
" X..X ",
" X..XXXXX ",
" X..X..X.XX ",
" XX X..X..X.X.X ",
"X..XX.........X ",
"X...X.........X ",
" X.....X.X.X..X ",
" X....X.X.X..X ",
" X....X.X.X.X ",
" X...X.X.X.X ",
" X.......X ",
" X....X.X ",
" XXXXX XX ")
_HCURS, _HMASK = pygame.cursors.compile(_HAND_CURSOR, ".", "X")
HAND_CURSOR = ((16, 16), (5, 1), _HCURS, _HMASK)
pygame.mouse.set_cursor(*HAND_CURSOR)
else:
cursor = pygame.cursors.compile(pygame.cursors.textmarker_strings)
pygame.mouse.set_cursor(*pygame.cursors.arrow)
def title_screen():
button1 = pygame.image.load("button1.png")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
hover_button(button1,90,250,250)
pygame.display.update()
clock.tick(15)
title_screen()