Fireworks 🎉
Here's a program that spawns particles to form fireworks every second at a random position on the screen.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "batFramework"
# ]
# ///
import batFramework as bf
import pygame
from random import randint
class MainScene(bf.Scene):
def __init__(self):
super().__init__("main")
def do_when_added(self):
self.set_clear_color((10, 10, 20))
self.pg = bf.ParticleGenerator()
self.add("world", self.pg)
bf.Timer(duration=1.0, end_callback=self.generate, loop=-1).start()
def generate(self):
position = bf.utils.random_point_on_screen(margin=40)
for _ in range(200):
velocity = pygame.math.Vector2(randint(40, 280), 0)
velocity.rotate_ip(randint(0, 360))
self.pg.add_particle(
bf.BasicParticle(
position,
velocity,
duration=1.0,
color=bf.utils.random_color(40, 255),
size=(3, 3),
)
)
bf.init(
resolution=(640, 480),
flags=pygame.SCALED,
window_caption="Fireworks",
fps_limit=60,
)
bf.Manager(MainScene()).run_async()
You can see the result here.
Web build
The live demo is built with pygbag against the PyPI release of batFramework (scene.add("world", …) / add_particle). Source lives in examples/fireworks/ in this docs repo.
SCALED flag
You can pass the pygame.SCALED flag to bf.init() to scale the window when the resolution is small.