Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions explosion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
DRAG = 0.8

# the colour of each particle in R, G, B values
PARTICLE_COLOR = 255, 230, 128
# Now with Original, White, Red, Green and Yellow (you can add more if you want)
COLOURS = []
COLOURS.append((255, 230, 128))
COLOURS.append((255, 255, 255))
COLOURS.append((255, 0, 0))
COLOURS.append((0, 255, 0))
COLOURS.append((0, 0, 255))
COLOURS.append((255, 255, 0))

# the time in seconds for which a particle is displayed
MAX_AGE = 3
Expand All @@ -20,11 +27,15 @@

# This function creates a new explosion at the specified screen co-ordinates

def explode(x, y, speed=300):
def explode(x, y, speed=300, colour = None):

# these are new particles, so set their age to zero
age = 0

# grab a random colour if one isn't specified
if colour is None:
colour = random.choice(COLOURS)

# generate 100 particles per explosion
for _ in range(100):

Expand All @@ -36,8 +47,8 @@ def explode(x, y, speed=300):
vx = speed * radius * math.sin(angle)
vy = speed * radius * math.cos(angle)

# add the particle's position, velocity and age to the array
particles.append((x, y, vx, vy, age))
# add the particle's position, colour, velocity and age to the array
particles.append((x, y, colour, vx, vy, age))


# This function redraws the screen by plotting each particle in the array
Expand All @@ -48,10 +59,10 @@ def draw():
screen.clear()

# loop through all the particles in the array
for x, y, *_ in particles:
for x, y, colour, *_ in particles:

# for each particle in the array, plot its position on the screen
screen.surface.set_at((int(x), int(y)), PARTICLE_COLOR)
screen.surface.set_at((int(x), int(y)), colour)


# This function updates the array of particles
Expand All @@ -62,7 +73,7 @@ def update(dt):
new_particles = []

# loop through the existing particle array
for (x, y, vx, vy, age) in particles:
for (x, y, colour, vx, vy, age) in particles:

# if a particle was created more than a certain time ago, it can be removed
if age + dt > MAX_AGE:
Expand All @@ -80,8 +91,8 @@ def update(dt):
# update the particle's age
age += dt

# add the particle's new position, velocity and age to the new array
new_particles.append((x, y, vx, vy, age))
# add the particle's new positionm colour, velocity and age to the new array
new_particles.append((x, y, colour, vx, vy, age))

# replace the current array with the new one
particles[:] = new_particles
Expand All @@ -90,13 +101,15 @@ def update(dt):
# This function creates an explosion at a random location on the screen

def explode_random():

# select a random position on the screen
x = random.randrange(WIDTH)
y = random.randrange(HEIGHT)

# call the explosion function for that position
explode(x, y)
# call the explosion function for that position (with a little randomness to the speed of the explosion)
explode(x, y, random.randint(200, 500))

# Randomise the random thingy
random.seed()

# call the random explosion function every 1.5 seconds
clock.schedule_interval(explode_random, 1.5)