当鼠标在rects附近存储在列表中,然后鼠标恢复正常时,我正在尝试切换pygame (python模块)中的游标。
我测试了这个循环,它的工作方式很多。但是在打开游标时,只有列表的最后一个元素才能工作(缩进是可以的)。这很奇怪,我测试过很多东西,但都不起作用。
这是我的代码:
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()我还测试了这个:
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()发布于 2020-06-04 23:00:21
问题是,当光标不在下一个框中时,您会不断地将光标设置为箭头。
通过你的算法:
diamond
将光标设置为arrow的
diamond
将光标设置为arrow的
diamond
将光标设置为arrow的
因此,除非您在最后一个框上悬停,否则光标在下一个框测试中重新设置之前,只被设置为菱形。也许你从来没有看到它改变时,在前两个盒子。
一种方法是在鼠标处于任意框内时设置一个标志,然后使用该标志设置鼠标光标。
https://stackoverflow.com/questions/62204231
复制相似问题