简体   繁体   English

不能在pyglet中绘制()精灵

[英]Can't draw() sprites in pyglet

For some reason, I can't get pyglet to draw sprites. 出于某种原因,我无法得到pyglet来绘制精灵。 Here's my code: 这是我的代码:

import pyglet

game = pyglet.window.Window(640, 480, "I'm a window")

batch = pyglet.graphics.Batch()

pyglet.resource.path = ["."]
pyglet.resource.reindex()

image = pyglet.resource.image("hextile.png")
pyglet.sprite.Sprite(image, x=200, y=300, batch=batch)
pyglet.text.Label('DING', font_name='Arial', font_size=24, x=100, y=100, batch=batch)

@game.event
def on_draw():

    game.clear()
    batch.draw()
    #image.blit(0, 0)

pyglet.app.run()

Now, when I draw the batch, the text label is shown correctly. 现在,当我绘制批处理时,文本标签会正确显示。 I see "DING" on the window. 我在窗户上看到“DING”。 However, the image "hextile.png" is not shown. 但是,未显示图像“hextile.png”。 I tried drawing the sprite independently, but that didn't work either. 我尝试独立绘制精灵,但这也不起作用。 Blitting the image (as shown in the commented line), however, seems to work just fine, but obviously that's not quite the functionality I'm after here. 然而,模糊图像(如注释行中所示)似乎工作得很好,但显然这不是我在此之后的功能。 I can't figure this one out. 我无法想出这个。 What am I missing? 我错过了什么?

Assuming you and your friends have ATI graphics cards: 假设您和您的朋友有ATI显卡:

Sprite.draw() uses the v2i format and VertexDomain.draw() internally. Sprite.draw()在内部使用v2i格式和VertexDomain.draw()。 For some reason this combination doesn't work on Windows Vista/7 Catalyst drivers 11.9 and above, and consequently Sprite drawing fails as well. 由于某种原因,此组合不适用于Windows Vista / 7 Catalyst驱动程序11.9及更高版本,因此Sprite绘图也会失败。 See also: pyglet vertex list not rendered (AMD driver?) 另请参见: pyglet顶点列表未呈现(AMD驱动程序?)

There is a pyglet issue you might want to follow: http://code.google.com/p/pyglet/issues/detail?id=544 您可能需要遵循一个pyglet问题: http//code.google.com/p/pyglet/issues/detail? id = 544

Your options for the moment seem to be either to patch pyglet.sprite.Sprite as mentioned in the third comment on that issue or downgrade your video driver. 您当前的选择似乎是修补pyglet.sprite.Sprite,如第三条评论中提到那个问题或降级您的视频驱动程序。

Update: No need to patch Sprite or downgrade your video driver. 更新:无需修补Sprite或降级视频驱动程序。 This problem seems to be fixed with Catalyst 12.4 (video driver 8.961.0.0). Catalyst 12.4(视频驱动程序8.961.0.0)似乎解决了这个问题。

The sprite is getting garbage collected because you don't hold a reference to it. 精灵正在收集垃圾,因为你没有对它进行引用。 Do this: 做这个:

sprite = pyglet.sprite.Sprite(image, x=200, y=300, batch=batch)

For what it's worth, I prefer using a subclass of Window, like this: (this code works for me too) 对于它的价值,我更喜欢使用Window的子类,如下所示:(此代码也适用于我)

import pyglet

class Window(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        self.batch = pyglet.graphics.Batch()
        image = pyglet.resource.image('hextile.png')
        self.sprite = pyglet.sprite.Sprite(image, batch=self.batch)
    def on_draw(self):
        self.clear()
        self.batch.draw()

def main():
    window = Window(width=640, height=480, caption='Pyglet')
    pyglet.app.run()

if __name__ == '__main__':
    main()

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

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