Reputation: 927
I'm testing pyglet lib. First thing I did is to draw a background image and a simple written, here the code:
game_window = pyglet.window.Window(720, 1280, fullscreen=True)
game_window.set_exclusive_mouse()
background_day = pyglet.sprite.Sprite(img=resources.background_day_image, x=0, y=0 )
fps_label = pyglet.text.Label(text="fps: 0", x=100, y=100)
@game_window.event
def on_draw():
game_window.clear()
background_day.draw()
fps_label.draw()
if __name__ == '__main__':
pyglet.app.run()
This works perfectly no problem at all. Then I decide to implement batch draw so I change my code:
main_batch = pyglet.graphics.Batch()
background_day = pyglet.sprite.Sprite(img=resources.background_day_image, x=0, y=0 ,batch=main_batch)
fps_label = pyglet.text.Label(text="fps: 0", x=100, y=100,batch=main_batch)
@game_window.event
def on_draw():
game_window.clear()
main_batch.draw()
It happens that I see the background but I cannot see the written and I don't get why or where I mistake.
Thanks
UPDATE
From the manual:When sprites are collected into a batch, no guarantee is made about the order in which they will be drawn. If you need to ensure some sprites are drawn before others, use two or more OrderedGroup objects to specify the draw order. Every good programmer should read the manual first!
Upvotes: 1
Views: 3373
Reputation: 927
From the manual:When sprites are collected into a batch, no guarantee is made about the order in which they will be drawn. If you need to ensure some sprites are drawn before others, use two or more OrderedGroup objects to specify the draw order.
So basically let's say you have background and foreground sprites, you create first a batch draw, then two group one for the background and one for the foreground:
main_batch = pyglet.graphics.Batch()
background = pyglet.graphics.OrderedGroup(0)
foreground = pyglet.graphics.OrderedGroup(1)
Then you add your sprite specifying both the batch name and the group name:
background = pyglet.sprite.Sprite(img=resources.background_day_image, x=0, y=0, batch=main_batch,group=background)
sprite1 = pyglet.sprite.Sprite(img=resources.background_day_image, x=0, y=0, batch=main_batch,group=foreground)
Every good programmer should read the manual first!
Upvotes: 4