Search code examples
pythonspritedrawpygletblit

Can't draw() sprites in pyglet


For some reason, I can't get pyglet to draw sprites. 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. However, the image "hextile.png" is not shown. 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?


Solution

  • Assuming you and your friends have ATI graphics cards:

    Sprite.draw() uses the v2i format and VertexDomain.draw() internally. 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. See also: pyglet vertex list not rendered (AMD driver?)

    There is a pyglet issue you might want to follow: 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.

    Update: No need to patch Sprite or downgrade your video driver. This problem seems to be fixed with Catalyst 12.4 (video driver 8.961.0.0).