Reputation: 1
Python, pyglet
I want to add sprites to my window after it was drawn.
i try to use batch, becouse i want to to have lot of sprites.
my simple testing code is:
import pyglet
import random
from pyglet.window import key, mouse
from pyglet import image
from PIL import Image, ImageDraw, ImageFont
#function return pyglet image 10x10px triangle, random color. Instead of loading a file, work fine
def pyimg():
background = Image.new('RGBA', (10,10), (255,255,255,0) )
img = Image.new('RGBA', (10,10), (0,0,0,0) )
draw = ImageDraw.Draw(img)
draw.polygon([(0,0), (0,10), (10,0)], fill=(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
im= Image.alpha_composite(background, img)
return pyglet.image.ImageData(im.width, im.height, 'RGBA', im.tobytes(), pitch=-im.width * 4)
screenWidth=1280
window = pyglet.window.Window(screenWidth, 720, resizable=True)
batch=pyglet.graphics.Batch()
sprites=[]
sprites.append(pyglet.sprite.Sprite(pyimg(), x=50, y=50, batch=batch))
sprites.append(pyglet.sprite.Sprite(pyimg(), x=150, y=50, batch=batch))
i=30
@window.event
def on_draw():
window.clear()
batch.draw()
@window.event
def on_key_press(symbol, modifiers):
global i
if symbol == key.RETURN:
print("Return key pressed")
sprites.append(pyglet.sprite.Sprite(pyimg(), x=150+i, y=50))
i+=15
pyglet.app.run()
App displays two sprites, and should add one more after enter key pressed. How to update display?
Upvotes: 0
Views: 227
Reputation: 791
This line: sprites.append(pyglet.sprite.Sprite(pyimg(), x=150+i, y=50))
you aren't specifying the batch to add sprites into.
It needs to be sprites.append(pyglet.sprite.Sprite(pyimg(), x=150+i, y=50, batch=batch))
Upvotes: 1