Whether you’re writing games, doing simulations, or creating visualizations, Python’s native turtle becomes frustratingly slow as soon as the window opens.
Today’s star, Pylet, is like a cup of iced Americano—lightweight, smooth, and zero configuration. With just three lines of code, you can make a sprite fly at 60 frames per second.
Students and geeks, polish your monitors and let’s squeeze out its performance.
1. Window and Sprite: Hello, Sprite!
Pylet encapsulates the verbosity of Pygame into a single sentence: create a window, load resources, and start the event loop, all done behind the scenes.
The following 12 lines will place a 64×64 little dinosaur in the center of the screen, laying the groundwork for subsequent operations.
import pylet, pathlib, random
win = pylet.Window(480, 320, 'DinoPark')
dino = pylet.Sprite(pathlib.Path('dino.png'))
dino.center() # Automatically center
@win.event
def on_draw():
win.clear()
dino.draw()
pylet.run(0) # 0 means vertical sync, frame rate stable at 60
2. Frame Animation: Let the Dinosaur Run in Place
Static images are too stiff? Drop 8 running sequence images into <span>run_??.png</span>, and Pylet will automatically slice them in order by filename.
The logic below is: change the image every 6 frames, loop playback; speed is adjustable, suitable for pixel art.
frames = [pylet.load(f'run_{i:02d}.png') for i in range(8)]
idx, counter = 0, 0
@win.event
def on_update(dt):
global idx, counter
counter += 1
if counter % 6 == 0:
idx = (idx + 1) % 8
dino.image = frames[idx]
3. Keyboard Control: A/D for Left and Right Movement
Pylet’s event decorators are as intuitive as Flask routes.
Attach <span>dx</span> to the sprite to avoid global variable pollution; <span>dt</span> is the actual frame interval, ensuring smooth movement on both 30 FPS and 144Hz screens.
dino.dx = 0
@win.event
def on_key_press(key):
if key == pylet.keys.A: dino.dx = -220
if key == pylet.keys.D: dino.dx = 220
@win.event
def on_update(dt):
dino.x += dino.dx * dt
dino.x = max(0, min(win.width - dino.width, dino.x))
4. Particle Effects: Meteor Strikes Earth
Finally, let’s add some cinematic flair.
Using <span>Batch</span> to push 50 meteors to the GPU at once is 8 times faster than drawing them one by one;
Gravity acceleration <span>ay</span> makes the meteors fall more realistically, suitable for explosions and fireworks scenes.
batch = pylet.Batch()
particles = []
for _ in range(50):
p = pylet.Circle(random.uniform(2, 5), color=(255, 150, 0))
p.x, p.y = win.width // 2, win.height // 2
p.vx = random.uniform(-180, 180)
p.vy = random.uniform(60, 200)
p.ay = -400
particles.append(p)
@win.event
def on_update(dt):
batch.clear()
for p in particles:
p.vy += p.ay * dt
p.x += p.vx * dt; p.y += p.vy * dt
if p.y > 0: batch.add(p)
5. Advantages Comparison
Compared to Pygame, Pylet eliminates redundant initialization, resulting in a package size reduction of 60%, making it quick to get started;
However, its ecological niche is narrow—there’s no advanced audio mixing or 3D support.
If you’re making lightweight 2D prototypes or teaching demos, choose it;
for commercial-grade games, Pygame or Arcade is still more stable.
6. Conclusion
Pylet breaks down “writing games” into building blocks: window, sprite, batch rendering, all achieved in three steps.
If you’re itching to try it out, post your creative demo screenshots in the comments, and let’s see who can achieve the highest frame rate!
Recommended Reading:
- • Emojis, a fast and cute Python library!
- • Pagr, a lightweight Python library!
- • Dref, an accurate and understated Python library!
- • Pohoda, a pleasant Python library!