简体   繁体   English

如何在 pygame/pygame 零中添加包装

[英]How to add wrapping in pygame/pygame zero

If you can I would like the wrapping to be an add-on to the code I have already, as this code I know works seamlessly and smoothing going from one side to the other.如果可以,我希望包装成为我已经拥有的代码的附加组件,因为我知道这段代码可以无缝地工作并且从一侧到另一侧平滑。 Basically the code I have allows wrapping from the bottom side to the top and from the right side to the left but NOT the left side to the right and the top side to the bottom.基本上,我拥有的代码允许从底部到顶部和从右侧到左侧包装,但不允许从左侧到右侧和从顶部到底部包装。 I have tried several things based off of what I have already and all seem to either effect the code I have already or just not work.我已经根据我已经做过的事情尝试了几件事情,而且所有事情似乎要么影响我已经拥有的代码,要么就是不工作。 Here's the code:这是代码:

    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

All help is appreciated thankyou!感谢所有帮助谢谢!

you need to add conditions for these cases:您需要为这些情况添加条件:

    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

Use the % (modulo) operator.使用% (模)运算符。 The % operator computes the remainder of a division: %运算符计算除法的余数:

player.centerx %= WIDTH 
player.centery %= HEIGHT 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM