简体   繁体   English

在 pygame 中,重置每个精灵位置的最有效方法是什么?

[英]In pygame, what is the most efficient way of resetting every sprite's position?

I'm trying to make a typical platformer using pygame but I'm not exactly sure how I should go about doing a death sequence.我正在尝试使用 pygame制作一个典型的平台游戏,但我不确定我应该如何进行死亡序列。 I want it so after time the player dies every sprite's position is reset to its original position if they moved at all.我希望它在玩家死亡后每个精灵的位置都重置为其原始位置,如果他们移动了。

I tried experimenting with the pygame.sprite.Group().copy() but I don't know how to use it or if it even applies to my situation.我尝试尝试使用pygame.sprite.Group().copy()但我不知道如何使用它,或者它是否适用于我的情况。

I would store a copy of the original position in the sprite, and then as @furas suggests re-position and re-stat the sprite via a reset() function.我会在精灵中存储原始位置的副本,然后正如@furas 建议的那样,通过reset()函数重新定位和重新统计精灵。

For example:例如:

class ResettableSprite( pygame.sprite.Sprite ):
    MAX_HEALTH = 100
    def __init__( self, image, x, y ):
        pygame.sprite.Sprite.__init__( self )
        self.image   = image
        self.rect    = self.image.get_rect()
        self.start_x = x
        self.start_y = y
        self.reset()

    def reset( self ):
        self.rect.x = self.start_x
        self.rect.y = self.start_y
        self.health = ResettableSprite.MAX_HEALTH
        # ... etc.

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

相关问题 在 pygame 中处理多个玩家键输入的最有效方法是什么? - What is the most efficient way to handle multiple players key inputs in pygame? 什么是最有效的Python睡眠方式? - What's the most efficient way to sleep in Python? 比较 2 个 numpy 矩阵的每个值的最有效方法是什么? - What is the most efficient way to compare every value of 2 numpy matrices? 解析块实体最有效,最简单的方法是什么? - What's the most efficient and easiest way to parse block entities? 在Python中加载字典的最有效方法是什么? - What's the most efficient way to load a dictionary in Python? 筛选QuerySet以提供数据表的最有效方法是什么? - What's the most efficient way to filter QuerySet to feed datatables? 导出和使用 TensorFlow model 的最有效和/或最简单的方法是什么 - What's the most efficient and/or easy way of exporting and using a TensorFlow model 在 python 循环期间暂停的最有效方法是什么? - What's the most efficient way to pause during a loop in python? 在python中生成集合组合的最有效内存方法是什么? - What's the most memory efficient way to generate the combinations of a set in python? 在Python中匹配字符串的最有效方法是什么? - What's the most efficient way to match strings in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM