擅长:python、mysql、java
<p>我知道这个问题已经得到了回答,但最好的方法是像凯尔温克建议的那样。我会详细说明,这样更容易理解。</p>
<pre><code># First, create you group
gems = pygame.sprite.Group()
class Jewel (pygame.sprite.Sprite): # Inherit from the Sprite
def __init__ (self, *args): # Call the constructor with whatever arguments...
# This next part is key. You call the super constructor, and pass in the
# group you've created and it is automatically added to the group every
# time you create an instance of this class
pygame.sprite.Sprite.__init__(self, gems)
# rest of class stuff after this.
>>> ruby = Jewel()
>>> diamond = Jewel()
>>> coal = Jewel()
# All three are now in the group gems.
>>> gems.sprites()
[<Jewel sprite(in 1 groups)>, <Jewel sprite(in 1 groups)>, <Jewel sprite(in 1 groups)>]
</code></pre>
<p>您还可以使用<code>gems.add(some_sprite)
</code>添加更多内容,同样也可以使用<code>gems.remove(some_sprite)</code>删除它们。</p>