pygame移动广场走出了bounderies

时间:2017-12-18 20:24:06

标签: python python-3.x pygame

我目前正在编制一个移动广场。我想用我的钥匙来控制它,并且它不会超出界限。但是,我无法让它移出边界(窗口的一侧)。这是我的代码:

active

有人知道如何解决这个问题吗?我刚刚学习了pygame(昨晚!)

1 个答案:

答案 0 :(得分:1)

你有一个名为xmfl的变量(小写L)而不是xmf1(带有1)

回答你的问题:

我会更改此代码:

if xmb1:
    x1 -= squares
if xmf1:
    x1 += squares
if ymb1:
    y1 -= squares
if ymf1:
    y1 += squares

到这个

if xmb1 and not (x1 <= 0):
    x1 -= squares
if xmf1 and not (x1 + squarew >= width):
    x1 += squares
if ymb1 and not (y1 <= 0):
    y1 -= squares
if ymf1 and not (y1 + squareh >= height):
    y1 += squares

我可能会向后退y方向...因为我使用了pygame已经有一段时间了。

代码的作用是检查我们是否已经处于董事会的边缘。如果我们处于边缘,不要允许进一步的移动。如果我们不在边缘,请继续。

请告诉我这是否有帮助和/或我是否可以为您回答任何问题。