Boing: An Extremely Fun Python Library!

Want to make a colorful bouncing ball pop up in the terminal with just 3 lines of code? Boing can do that. It combines “animation”, “sound effects”, and “keyboard” into a toy-level API, when you’re tired of writing crawlers, use it to create an Easter egg, or warm up for an interview in the last 5 minutes. It takes about 4 minutes to read, brew some coffee, and let’s bounce together.

1. Bouncing Prelude: Installation Takes Only Seven Seconds

After running pip install boing, the terminal instantly turns into an amusement park. First, sneak a peek at the official website, confirm version 0.4.3, then roll up your sleeves and get started.

2. Bouncing Ball in Place

Make a small red ball bounce up and down in place, learning the core concepts of Boing: Sprite and Loop.

from boing import Sprite, run

ball = Sprite('🔴', y=5)
def bounce(): ball.y += 1 if ball.y < 5 else -5
run(loop=bounce, fps=10)

In less than ten lines, the terminal starts going “da-da-da”.

3. Keyboard Hijacking, the Ball Follows You

Bouncing in place is too dull, let’s let WASD take control of the inertia.

from boing import Sprite, run, key

hero = Sprite('🚀', x=10, y=5)
def move():
    if key.w: hero.y -= 1
    if key.s: hero.y += 1
    if key.a: hero.x -= 2
    if key.d: hero.x += 2
run(loop=move, fps=20)

With a flick of your finger, the rocket dashes out with a trail.

4. Particle Fireworks, Lighting Up the Screen

One ball isn’t enough? Generate 50 Mars balls in bulk, with gravity + random initial velocity, boom!

import random
from boing import Sprite, run

stars = [Sprite('✨', x=20, y=5,
               vx=random.uniform(-3, 3),
               vy=random.uniform(-4, 0))
          for _ in range(50)]

def gravity():
    for s in stars:
        s.x += s.vx
        s.vy += 0.2
        s.y += s.vy
run(loop=gravity, fps=30)

Half the screen is filled with meteors, and the performance remains smooth.

5. Synchronized Sound Effects, Ears Also Bounce

The visuals are exciting, don’t let your ears be idle. Load a beep with boing.play, call it at the moment of collision, and it goes “ding” just right.

from boing import Sprite, run, play

beep = play.load('beep.wav')
ball = Sprite('⚽', x=1, vx=2)
def wall():
    ball.x += ball.vx
    if ball.x <= 0 or ball.x >= 40:
        ball.vx *= -1
        beep()
run(loop=wall, fps=40)

Sound and pixels are synchronized, immersion +50%.

6. Two-Player Battle, Done in 30 Lines

Split the screen in half, left side 🐍 right side 🦖, whoever reaches 30 points first wins, let’s have a game during break.

from boing import Sprite, run, key, clear

p1 = Sprite('🐍', x=5, y=3, score=0)
p2 = Sprite('🦖', x=35, y=3, score=0)
ball = Sprite('⚽', x=20, y=10, vx=1, vy=1)

def pong():
    clear()
    if key.a: p1.y -= 2
    if key.z: p1.y += 2
    if key.k: p2.y -= 2
    if key.m: p2.y += 2
    ball.x += ball.vx
    ball.y += ball.vy
    if ball.collide(p1) or ball.collide(p2):
        ball.vx *= -1.1
    if ball.y <= 0 or ball.y >= 20:
        ball.vy *= -1
    if ball.x < 0: p2.score += 1; ball.x = 20
    if ball.x > 40: p1.score += 1; ball.x = 20
    Sprite(f'{p1.score}-{p2.score}', x=18).show()
run(loop=pong, fps=25)

In less than 30 lines, the dorm turns into an arena.

7. Performance Easter Egg: A Million Particles?

In the official benchmark, Boing uses a NumPy backend, allowing particles to reach the level of 1e6, as long as you don’t insist on printing every frame, the fan hardly turns on.

8. Advantages Flash

Lighter than Pygame, faster than Tkinter, install with pip, zero dependencies. But don’t use it to write AAA games, UI controls are almost non-existent, for complex scenes, please turn to Godot.

9. Final Bouncing

Today we turned the terminal into a disco hall, have you mastered the magic of Boing? Leave a message to tell me, who do you want to make bounce?

Recommended Reading:

  • cdpred, an extremely nice Python library!
  • • lpsim, a clearly visible Python library!
  • lookfor, a super flexible Python library!
  • lpsim, a clearly visible Python library!

Leave a Comment