I'm trying to switch the cursor in pygame (python module) when the mouse is around rects stored in list and then the mouse get back to normal.
I tested the loop, its working in many ways. But with a switch on cursor, only on the last element of the list is working(the indentation is ok). It's quite weird, i have tested many things but it doesn't work.
Here is my code :
pygame.init()
[...]
button = pygame.draw.rect(screen, white, (585, 424, 50, 50))
button2 = pygame.draw.rect(screen, white, (235, 274, 30, 30))
button3 = pygame.draw.rect(screen, white, (440, 311, 40, 40))
all_buttons = [button2, button3, button]
running = True
while running:
screen.blit(background, (0, 0))
for x in all_buttons:
if x.collidepoint(pygame.mouse.get_pos()):
pygame.mouse.set_cursor(*pygame.cursors.diamond)
else:
pygame.mouse.set_cursor(*pygame.cursors.arrow)
pygame.display.flip()
I also tested this :
def mouse_state(cursor_state):
if cursor_state:
pygame.mouse.set_cursor(*pygame.cursors.arrow)
else:
pygame.mouse.set_cursor(*pygame.cursors.diamond)
[...]
cursor_state = True
while running:
for x in all_buttons:
if x.collidepoint(pygame.mouse.get_pos()):
cursor_state = False
else:
cursor_state = True
mouse_state(cursor_state)
pygame.display.flip()
The issue is that you're constantly setting the cursor back to arrow when the cursor is not in the next box.
Walk through your algorithm:
So unless you're hovering over the last box, the cursor is only set to diamond for a split-second, before being re-set in the next box-test. Probably you never see it change when over the first 2 boxes.
One way around is to set a flag when the mouse is inside any box, then use that flag to set the mouse-cursor.