[英]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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.