如果可以的话,我希望包装是我已经拥有的代码的附加组件,因为我知道这段代码可以无缝地从一侧平滑地运行到另一侧。基本上,我的代码允许从底部到顶部和从右侧到左侧包装,但不是从左侧到右侧和顶部到底部。我试过几件事情的我似乎已经和所有代码我已经影响或不工作。下面是代码:
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