首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >游戏游标在for循环中只运行在最后一个

游戏游标在for循环中只运行在最后一个
EN

Stack Overflow用户
提问于 2020-06-04 21:13:21
回答 1查看 51关注 0票数 1

当鼠标在rects附近存储在列表中,然后鼠标恢复正常时,我正在尝试切换pygame (python模块)中的游标。

我测试了这个循环,它的工作方式很多。但是在打开游标时,只有列表的最后一个元素才能工作(缩进是可以的)。这很奇怪,我测试过很多东西,但都不起作用。

这是我的代码:

代码语言:javascript
运行
复制
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()

我还测试了这个:

代码语言:javascript
运行
复制
 def mouse_state(cursor_state):
    if cursor_state:
        pygame.mouse.set_cursor(*pygame.cursors.arrow)
    else:
        pygame.mouse.set_cursor(*pygame.cursors.diamond)

..。

代码语言:javascript
运行
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-04 23:00:21

问题是,当光标不在下一个框中时,您会不断地将光标设置为箭头。

通过你的算法:

diamond

  • if游标在box1
    • 中将游标设置为

将光标设置为arrow的

diamond

  • if游标在box2
    • 中将游标设置为

将光标设置为arrow的

diamond

  • if游标在box3
    • 中将游标设置为

将光标设置为arrow的

因此,除非您在最后一个框上悬停,否则光标在下一个框测试中重新设置之前,只被设置为菱形。也许你从来没有看到它改变时,在前两个盒子。

一种方法是在鼠标处于任意框内时设置一个标志,然后使用该标志设置鼠标光标。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62204231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档