您可能还需要你的子弹是Sprites
分组到Batch
为faster execution(以重火力的情况下)。该pyglet
文档show how its done:
batch = pyglet.graphics.Batch()
bullet_image = pyglet.image.load('bullet.png')
bullet_sprites = [] # I've changed the variable names to fitting our issue
for i in range(100):
x, y = i * 10, 50
bullet_sprites.append(pyglet.sprite.Sprite(bullet_image, x, y, batch=batch)
首先,新鲜的Batch
被创建。接下来,100个不同的对象被实例化,每个对象都充当一个子弹(或球,但我们假装我们已经得到子弹)的非常适合的容器。 的Sprite
构造函数的每次调用传递一个可选参考我们的batch
,因此,所有新创建的子弹Sprites
连接到子弹Batch
。
它的妙处是Batch
类的draw()
方法。在调用它时,要注意批次中每个子弹的blitting。你不必调用每一颗子弹Sprite
的draw()
方法自己,因为这只能在一个单一的调用来做到more efficiently:
@window.event
def on_draw():
batch.draw()
你会认识到这个例子,很像一个@Xymostech让步第一个答案,填充一个包含所有项目符号实例的列表。您仍然需要像这样的Collection结构来保留对您的Sprite
的引用,因为Batch
没有函数会返回附加到它的对象的列表。
所以,你会坚持在以前的答案的解决方案,除了当然的图像的blitting。
使用Sprites
的另一个优点是,你不必有自己的位置字段来装备你的bullet
类,因为你可以使用相应的parameters of your Sprite
objects。除了允许更新其(x, y)
坐标,Sprites
也可以rotate和fade out。
实际上,你应该考虑修改你的bullet
类,使其成为Sprite
类的扩展名inheriting。
class Bullet(pyglet.sprite.Sprite): # btw, class names usually start with an Uppercase letter
def __init__(self):
super(pyglet.sprite.Sprite, self).__init__()
self.x = player.pos_x
...
我认为添加到批量精灵后不能移动,我一定会尝试,谢谢! – solusipse 2013-02-22 10:59:35