| 1 |
""" |
| 2 |
Simple example showing some animated shapes |
| 3 |
""" |
| 4 |
import math |
| 5 |
import pyglet |
| 6 |
from pyglet import shapes |
| 7 |
|
| 8 |
|
| 9 |
class ShapesDemo(pyglet.window.Window): |
| 10 |
|
| 11 |
def __init__(self, width, height): |
| 12 |
super().__init__(width, height, "Shapes") |
| 13 |
self.time = 0 |
| 14 |
self.batch = pyglet.graphics.Batch() |
| 15 |
|
| 16 |
self.circle = shapes.Circle(360, 240, 75, color=(255, 225, 255, 127), batch=self.batch) |
| 17 |
|
| 18 |
|
| 19 |
self.square = shapes.BorderedRectangle(360, 240, 100, 100, border=5, color=(55, 55, 255), |
| 20 |
border_color=(25, 25, 25), batch=self.batch) |
| 21 |
self.square.anchor_position = 50, 50 |
| 22 |
|
| 23 |
|
| 24 |
self.rectangle = shapes.Rectangle(100, 190, 500, 100, color=(255, 22, 20, 64), batch=self.batch) |
| 25 |
|
| 26 |
self.line = shapes.Line(0, 0, 0, 480, thickness=4, color=(200, 20, 20), batch=self.batch) |
| 27 |
|
| 28 |
self.triangle = shapes.Triangle(10, 10, 190, 10, 100, 150, color=(55, 255, 255, 175), batch=self.batch) |
| 29 |
|
| 30 |
septagon_step = math.pi * 2 / 7 |
| 31 |
self.fading_septagon = shapes.Polygon( |
| 32 |
*[[50 + 40 * math.sin(i * septagon_step), 200 + 40 * math.cos(i * septagon_step)] for i in range(7)], |
| 33 |
batch=self.batch, |
| 34 |
) |
| 35 |
|
| 36 |
self.arc = shapes.Arc(50, 300, radius=40, segments=25, angle=270.0, color=(255, 255, 255), batch=self.batch) |
| 37 |
|
| 38 |
self.star = shapes.Star(600, 375, 50, 30, 5, color=(255, 255, 0), batch=self.batch) |
| 39 |
|
| 40 |
self.ellipse = shapes.Ellipse(650, 150, a=50, b=30, color=(55, 255, 55), batch=self.batch) |
| 41 |
|
| 42 |
self.sector = shapes.Sector(125, 400, 60, angle=0.45 * 360, color=(55, 255, 55), batch=self.batch) |
| 43 |
|
| 44 |
self.polygon = shapes.Polygon([400, 100], [500, 10], [600, 100], [550, 175], [450, 150], batch=self.batch) |
| 45 |
|
| 46 |
self.box = shapes.Box(60, 40, 200, 100, thickness=2, color=(244, 55, 55), batch=self.batch) |
| 47 |
|
| 48 |
coordinates = [[450, 400], [475, 450], [525, 450], [550, 400]] |
| 49 |
self.multiLine = shapes.MultiLine(*coordinates, closed=True, batch=self.batch) |
| 50 |
|
| 51 |
def on_draw(self): |
| 52 |
"""Clear the screen and draw shapes""" |
| 53 |
self.clear() |
| 54 |
self.batch.draw() |
| 55 |
|
| 56 |
def update(self, delta_time): |
| 57 |
"""Animate the shapes""" |
| 58 |
self.time += delta_time |
| 59 |
self.square.rotation = self.time * 15 |
| 60 |
self.rectangle.y = 200 + math.sin(self.time) * 190 |
| 61 |
self.circle.radius = 75 + math.sin(self.time * 1.17) * 25 |
| 62 |
self.triangle.rotation = self.time * 15 |
| 63 |
|
| 64 |
self.line.x = 360 + math.sin(self.time * 0.81) * 360 |
| 65 |
self.line.x2 = 360 + math.sin(self.time * 1.34) * 360 |
| 66 |
|
| 67 |
self.arc.rotation = self.time * 30 |
| 68 |
|
| 69 |
self.fading_septagon.opacity = int(255 * (0.5 + (0.5 * math.cos(self.time)))) |
| 70 |
|
| 71 |
self.star.rotation = self.time * 50 |
| 72 |
self.polygon.rotation = self.time * 45 |
| 73 |
|
| 74 |
self.ellipse.b = abs(math.sin(self.time) * 100) |
| 75 |
self.sector.angle = (self.time * 30) % 360.0 |
| 76 |
|
| 77 |
self.multiLine.rotation = self.time * -15 |
| 78 |
|
| 79 |
self.multiLine.rotation = self.time * -15 |
| 80 |
|
| 81 |
|
| 82 |
if __name__ == "__main__": |
| 83 |
demo = ShapesDemo(720, 480) |
| 84 |
pyglet.clock.schedule_interval(demo.update, 1/30) |
| 85 |
pyglet.app.run() |