Particle Effects Example
This example demonstrates how to create a simple confetti particle effect using akesi lili. Particle effects can add dynamic and visually appealing elements to your game, such as explosions, smoke, or in this case, confetti.

Explanation
The code initializes a game state with an empty list of particles. When the mouse button is pressed, it generates new particles at the mouse position. Each particle has randomized properties such as position, velocity, and color. The particles are updated and drawn each frame, creating a confetti effect.
Code
# list of particles
par=[]
# random number between min, max
rand=|min,max|
random.number()*(max-min)+min
# creates a new particle
new_par=|x,y|
x:rand(-2.0,2.0)+x
y:rand(-2.0,2.0)+y
vx:rand(-15.0,15.0)/50.0
vy:rand(-50.0,10.0)/50.0
ay:0.05
ttl:rand(10.0,70.0).round()
col:rand(1.0,3.0).round()
# update the particle
up:||
self.vy+=self.ay
self.x+=self.vx
self.y+=self.vy
self.ttl-=1
# draw the particle
dr:||
pix self.x,self.y,self.col
export update=||
# create new particle
if btn 8
m=mouse()
for i in 0..10
par.push(new_par(m.x,m.y))
i=0
# update remove particles
while i<size par
if par[i].ttl==0
par.remove i
else
par[i].up()
i+=1
export draw=||
clear 3
title='Press mouse or touch to throw confetti'.to_uppercase()
txt title,3,3,1
for p in par
p.dr()
How It Works
This example demonstrates a particle system for creating dynamic visual effects:
-
Particle Storage: The
pararray stores all active particles in the system. -
Random Number Generation: The
rand()helper function generates random numbers between min and max values, used to add variation to particle properties. -
Particle Creation: The
new_par()function creates a particle object with:x,y: Initial position (randomized slightly around spawn point for spread)vx,vy: Velocity in x and y directions (horizontal spread and upward/downward motion)ay: Acceleration in y-direction (0.05 for gravity effect)ttl: Time-to-live in frames (random between 10-70 frames)col: Color index (randomly chosen between 1-3)up(): Method to update particle physics each framedr(): Method to draw the particle as a single pixel
-
Particle Spawning: When the mouse button (button 8) is pressed:
- Gets current mouse position using
mouse() - Spawns 10 new particles at that location
- Each particle gets randomized initial velocities and properties
- Gets current mouse position using
-
Physics Update: In the
update()function, for each particle:- Applies gravity:
vyincreases byay(acceleration) - Updates position: Adds velocity to position (
x += vx,y += vy) - Decreases time-to-live counter
- Removes particles when
ttlreaches 0
- Applies gravity:
-
Rendering: In the
draw()function:- Clears screen with color 3
- Displays instruction text at the top
- Draws each active particle as a single pixel at its current position with its assigned color
The result is a dynamic confetti effect where particles shoot out from the mouse click position, arc downward due to gravity, and eventually disappear after their lifetime expires. The randomization creates natural-looking variation in the particle motion and appearance.