| 1 |
"""This is a simple example that visually confirms """ |
| 2 |
|
| 3 |
import pyglet |
| 4 |
from pyglet.gl import ( |
| 5 |
glClearColor, |
| 6 |
GL_TRIANGLES, |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
window = pyglet.window.Window(width=540, height=540, resizable=True) |
| 13 |
batch = pyglet.graphics.Batch() |
| 14 |
print(f"OpenGL Context: {window.context.get_info().version}") |
| 15 |
|
| 16 |
program = pyglet.graphics.get_default_shader() |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
label = pyglet.text.Label("Scroll the mouse wheel", x=0, y=180, dpi=200, color=(255, 25, 255, 150), batch=batch) |
| 22 |
|
| 23 |
vertex_list = program.vertex_list(3, GL_TRIANGLES, |
| 24 |
position=('f', (100, 300, 0, 200, 250, 0, 200, 350, 0)), |
| 25 |
colors=('f', (1, 0, 0, 1, 0, 1, 0, 1, 0.3, 0.3, 1, 1))) |
| 26 |
|
| 27 |
|
| 28 |
def create_quad_vertex_list(x, y, z, width, height): |
| 29 |
return x, y, z, x + width, y, z, x + width, y + height, z, x, y + height, z |
| 30 |
|
| 31 |
|
| 32 |
program.vertex_list_indexed(4, GL_TRIANGLES, [0, 1, 2, 0, 2, 3], batch, None, |
| 33 |
position=('f', create_quad_vertex_list(480, 270, -11, 50, 50)), |
| 34 |
colors=('f', (1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1))) |
| 35 |
|
| 36 |
program.vertex_list_indexed(4, GL_TRIANGLES, [0, 1, 2, 0, 2, 3], batch, None, |
| 37 |
position=('f', (400, 400, 0, 400+50, 400, 0, 400+50, 400+50, 0, 400, 400+50, 0)), |
| 38 |
colors=('f', (1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1, 1, 0.5, 0.2, 1))) |
| 39 |
|
| 40 |
|
| 41 |
img = pyglet.image.load("pyglet.png") |
| 42 |
img.anchor_x = img.width // 2 |
| 43 |
img.anchor_y = img.height // 2 |
| 44 |
red = pyglet.image.SolidColorImagePattern((255, 0, 0, 255)).create_image(50, 50) |
| 45 |
green = pyglet.image.SolidColorImagePattern((0, 255, 0, 255)).create_image(50, 50) |
| 46 |
blue = pyglet.image.SolidColorImagePattern((0, 0, 255, 255)).create_image(50, 50) |
| 47 |
white = pyglet.image.SolidColorImagePattern((255, 255, 255, 255)).create_image(50, 50) |
| 48 |
|
| 49 |
sprites = [pyglet.sprite.Sprite(img=img, x=60, y=80, batch=batch), |
| 50 |
pyglet.sprite.Sprite(img=img, x=110, y=90, batch=batch), |
| 51 |
pyglet.sprite.Sprite(img=img, x=160, y=100, batch=batch), |
| 52 |
pyglet.sprite.Sprite(img=img, x=210, y=110, batch=batch)] |
| 53 |
for sprite in sprites: |
| 54 |
sprite.opacity = 220 |
| 55 |
|
| 56 |
sprite2 = pyglet.sprite.Sprite(img=red, x=200, y=400, batch=batch) |
| 57 |
sprite3 = pyglet.sprite.Sprite(img=green, x=300, y=300, batch=batch) |
| 58 |
sprite4 = pyglet.sprite.Sprite(img=blue, x=400, y=200, batch=batch) |
| 59 |
sprite5 = pyglet.sprite.Sprite(img=white, x=500, y=100, batch=batch) |
| 60 |
|
| 61 |
standalone_sprite = pyglet.sprite.Sprite(img=white, x=600, y=0) |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@window.event |
| 69 |
def on_mouse_scroll(x, y, mouse, direction): |
| 70 |
for spr in sprites: |
| 71 |
spr.scale += direction / 10 |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@window.event |
| 78 |
def on_draw(): |
| 79 |
window.clear() |
| 80 |
vertex_list.draw(GL_TRIANGLES) |
| 81 |
batch.draw() |
| 82 |
standalone_sprite.draw() |
| 83 |
|
| 84 |
|
| 85 |
def update(dt): |
| 86 |
for spr in sprites: |
| 87 |
spr.rotation += 100 * dt % 360 |
| 88 |
|
| 89 |
|
| 90 |
if __name__ == "__main__": |
| 91 |
glClearColor(0.2, 0.3, 0.3, 1) |
| 92 |
pyglet.clock.schedule_interval(update, 1/60) |
| 93 |
pyglet.app.run() |