| 1 |
import pyglet |
| 2 |
import random |
| 3 |
|
| 4 |
from pyglet.gl import GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_TRIANGLES |
| 5 |
from pyglet.graphics.instance import InstanceSourceMixin |
| 6 |
|
| 7 |
window = pyglet.window.Window(1920, 1080, vsync=False) |
| 8 |
|
| 9 |
|
| 10 |
pyglet.resource.path = ['../resources'] |
| 11 |
pyglet.resource.reindex() |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
image = pyglet.resource.image("pyglet.png") |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
image.anchor_x = image.width // 2 |
| 21 |
image.anchor_y = image.height // 2 |
| 22 |
|
| 23 |
|
| 24 |
batch = pyglet.graphics.Batch() |
| 25 |
|
| 26 |
scales = [1.0, 0.75, 0.5, 0.25] |
| 27 |
|
| 28 |
|
| 29 |
class SpriteInstanceSource(pyglet.sprite.Sprite, InstanceSourceMixin): |
| 30 |
"""Subclass example.""" |
| 31 |
|
| 32 |
def __init__(self, img, attributes, x=0, y=0, z=0, blend_src=GL_SRC_ALPHA, blend_dest=GL_ONE_MINUS_SRC_ALPHA, batch=None, |
| 33 |
group=None, subpixel=False): |
| 34 |
self.instance_attributes = attributes |
| 35 |
InstanceSourceMixin.__init__(self, attributes) |
| 36 |
pyglet.sprite.Sprite.__init__(self, img, x, y, z, blend_src, blend_dest, batch, group, subpixel) |
| 37 |
|
| 38 |
|
| 39 |
def _create_vertex_list(self): |
| 40 |
self._vertex_list = self.program.vertex_list_instanced_indexed( |
| 41 |
4, GL_TRIANGLES, [0, 1, 2, 0, 2, 3], self.instance_attributes, |
| 42 |
self._batch, self._group, |
| 43 |
position=('f', self._get_vertices()), |
| 44 |
colors=('Bn', (*self._rgb, int(self._opacity)) * (1 if 'colors' in self.instance_attributes else 4)), |
| 45 |
translate=('f', (self._x, self._y, self._z) * (1 if 'translate' in self.instance_attributes else 4)), |
| 46 |
scale=('f', (self._scale*self._scale_x, self._scale*self._scale_y) * (1 if 'scale' in self.instance_attributes else 4)), |
| 47 |
rotation=('f', (self._rotation,) * (1 if 'rotation' in self.instance_attributes else 4)), |
| 48 |
tex_coords=('f', self._texture.tex_coords)) |
| 49 |
|
| 50 |
|
| 51 |
sprites = [] |
| 52 |
|
| 53 |
sprite = SpriteInstanceSource(image, ('translate', 'colors'), |
| 54 |
x=random.randint(0, window.width), |
| 55 |
y=random.randint(0, window.height), |
| 56 |
batch=batch) |
| 57 |
|
| 58 |
instance = sprite.create_instance(translate=(100, 50, 0), colors=(255, 255, 255, 255)) |
| 59 |
|
| 60 |
for i in range(10): |
| 61 |
sprite_instance = sprite.create_instance( |
| 62 |
translate=(random.randint(0, window.width), random.randint(0, window.height), 0), |
| 63 |
colors=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255), |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
sprites.append(sprite_instance) |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@window.event |
| 78 |
def on_key_press(symbol, modifiers): |
| 79 |
if symbol == pyglet.window.key.SPACE: |
| 80 |
instance.translate = (random.randint(0, window.width), random.randint(0, window.height), 0) |
| 81 |
|
| 82 |
elif symbol == pyglet.window.key.DELETE: |
| 83 |
del_instance = instance_sprite.pop() |
| 84 |
sprites.remove(del_instance) |
| 85 |
elif symbol == pyglet.window.key.INSERT: |
| 86 |
sprite_instance = instance_sprite.create( |
| 87 |
translate=(random.randint(0, window.width), random.randint(0, window.height), 0), |
| 88 |
colors=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255), |
| 89 |
) |
| 90 |
sprites.append(sprite_instance) |
| 91 |
|
| 92 |
@window.event |
| 93 |
def on_draw(): |
| 94 |
window.clear() |
| 95 |
batch.draw() |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
pyglet.app.run(0) |
| 100 |
|