【问题标题】:Cannot figure out why my code for a random location button isn't working (python, pygame) [duplicate]无法弄清楚为什么我的随机位置按钮代码不起作用(python,pygame)[重复]
【发布时间】:2021-04-29 23:04:47
【问题描述】:

一切都在我的脑海中有意义。我不明白为什么当我单击按钮时它不起作用。我相信问题出在这些文本行中

        mouse = pygame.mouse.get_pos()
        if mouse[0] >= rand_x <= player_size and mouse[1] >= rand_y <= player_size:
            sys.exit()"""

Python Code Image

【问题讨论】:

  • 如果您认为这是您的问题所在,您是否打印出 mouse 的值?你得到了什么?
  • 欢迎来到 SO。请澄清“不起作用”的含义。

标签: python python-3.x pygame


【解决方案1】:

在此声明中:

mouse[0] >= rand_x <= player_size

你真正想说的是:

mouse[0]>= rand_X and rand_x <= player_size

因此,如果 rand_x 不小于或等于 player_size (50),它不会评估为 True。

documentation 说:

比较可以任意链接,例如,x

也许你的意思是:

    rand_x <= mouse[0] <= rand_x+player_size

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    相关资源
    最近更新 更多