如何在 pygame/pygame 零中添加换行
How to add wrapping in pygame/pygame zero
如果可以的话,我希望包装成为我已有代码的附加组件,因为我知道这段代码可以无缝工作并且从一侧到另一侧平滑。基本上我的代码允许从底部到顶部和从右侧到左侧包装,但不是左侧到右侧和顶部到底部。我已经根据我已经尝试过的一些东西进行了尝试,所有这些似乎都影响了我已经拥有的代码或者只是不起作用。这是代码:
if player.left > WIDTH: #if player hits right side of screen it takes the player to the left
player.right = 0
elif player.top > HEIGHT: #if player hits bottom it goes to top
player.bottom = 0
感谢所有帮助,谢谢!
您需要为这些情况添加条件:
if player.left > WIDTH:
player.right = 0
elif player.right < 0: # if the player leaves to the left
player.left = WIDTH
elif player.top > HEIGHT:
player.bottom = 0
elif player.bottom < 0: # if the player leaves to the right
player.top = HEIGHT
使用 %
(取模)运算符。 %
运算符计算除法的余数:
player.centerx %= WIDTH
player.centery %= HEIGHT
如果可以的话,我希望包装成为我已有代码的附加组件,因为我知道这段代码可以无缝工作并且从一侧到另一侧平滑。基本上我的代码允许从底部到顶部和从右侧到左侧包装,但不是左侧到右侧和顶部到底部。我已经根据我已经尝试过的一些东西进行了尝试,所有这些似乎都影响了我已经拥有的代码或者只是不起作用。这是代码:
if player.left > WIDTH: #if player hits right side of screen it takes the player to the left
player.right = 0
elif player.top > HEIGHT: #if player hits bottom it goes to top
player.bottom = 0
感谢所有帮助,谢谢!
您需要为这些情况添加条件:
if player.left > WIDTH:
player.right = 0
elif player.right < 0: # if the player leaves to the left
player.left = WIDTH
elif player.top > HEIGHT:
player.bottom = 0
elif player.bottom < 0: # if the player leaves to the right
player.top = HEIGHT
使用 %
(取模)运算符。 %
运算符计算除法的余数:
player.centerx %= WIDTH
player.centery %= HEIGHT