| 1 | import pyglet |
| 2 | import random |
| 3 | |
| 4 | |
| 5 | WIDTH = 1280 |
| 6 | HEIGHT = 720 |
| 7 | TARGET_FPS = 60 |
| 8 | |
| 9 | window = pyglet.window.Window(WIDTH, HEIGHT, caption="Benchmark", resizable=True, vsync=False) |
| 10 | label = pyglet.text.Label(" ", x=5, y=5, color=(223, 223, 223, 255), dpi=80) |
| 11 | batch = pyglet.graphics.Batch() |
| 12 | |
| 13 | |
| 14 | grey_img = pyglet.image.SolidColorImagePattern((50, 50, 50, 150)).create_image(5, 5) |
| 15 | blocks = [] |
| 16 | |
| 17 | |
| 18 | @window.event |
| 19 | def on_draw(): |
| 20 | window.clear() |
| 21 | batch.draw() |
| 22 | label.draw() |
| 23 | |
| 24 | |
| 25 | @window.event |
| 26 | def on_resize(width, height): |
| 27 | global WIDTH, HEIGHT |
| 28 | WIDTH, HEIGHT = width, height |
| 29 | |
| 30 | |
| 31 | def update(dt): |
| 32 | |
| 33 | fps = 1 / dt |
| 34 | count = int(fps - TARGET_FPS) |
| 35 | |
| 36 | if count > 0: |
| 37 | for i in range(count//2): |
| 38 | x = random.uniform(0, WIDTH/2) |
| 39 | y = random.uniform(0, HEIGHT) |
| 40 | blocks.append(pyglet.sprite.Sprite(img=grey_img, x=x, y=y, batch=batch, subpixel=True)) |
| 41 | |
| 42 | else: |
| 43 | if blocks: |
| 44 | blocks.pop(0) |
| 45 | |
| 46 | for block in blocks: |
| 47 | block.x += dt |
| 48 | |
| 49 | if blocks: |
| 50 | label.text = f"FPS: {round(fps)}, sprites:{len(blocks)}" |
| 51 | |
| 52 | |
| 53 | pyglet.clock.schedule_interval(update, 1/120) |
| 54 | pyglet.app.run(1/120) |