| 1 | import pyglet |
| 2 | |
| 3 | from pyglet.window import key |
| 4 | |
| 5 | window = pyglet.window.Window(vsync=False) |
| 6 | fps = pyglet.window.FPSDisplay(window=window) |
| 7 | batch = pyglet.graphics.Batch() |
| 8 | label = pyglet.text.Label("Press a number from 1-0 to change the FPS. (number * 10)", x=10, y=50, batch=batch) |
| 9 | |
| 10 | circle = pyglet.shapes.Circle(200, 200, 25, color=(200, 60, 130), batch=batch) |
| 11 | MOVEMENT_SPEED = 600 |
| 12 | |
| 13 | |
| 14 | @window.event |
| 15 | def on_key_press(keycode, mod): |
| 16 | rates = {key._1: 1/10, key._2: 1/20, key._3: 1/30, key._4: 1/40, key._5: 1/50, |
| 17 | key._6: 1/60, key._7: 1/70, key._8: 1/80, key._9: 1/90, key._0: 1/100} |
| 18 | |
| 19 | if redraw_rate := rates.get(keycode): |
| 20 | pyglet.clock.unschedule(window.draw) |
| 21 | pyglet.clock.schedule_interval(window.draw, redraw_rate) |
| 22 | global fps |
| 23 | fps = pyglet.window.FPSDisplay(window=window) |
| 24 | |
| 25 | |
| 26 | @window.event |
| 27 | def on_refresh(dt): |
| 28 | global MOVEMENT_SPEED |
| 29 | |
| 30 | circle.x += MOVEMENT_SPEED * dt |
| 31 | |
| 32 | if circle.x > window.width or circle.x < 0: |
| 33 | MOVEMENT_SPEED = -MOVEMENT_SPEED |
| 34 | |
| 35 | window.clear() |
| 36 | batch.draw() |
| 37 | fps.draw() |
| 38 | |
| 39 | |
| 40 | pyglet.clock.schedule_interval(window.draw, 1/60) |
| 41 | pyglet.app.run(None) |