From a6ea1cde81819a4d7baeb14d57d77fb54fad0a19 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 9 Nov 2018 22:39:00 +0000 Subject: [PATCH] Added a little colour Added some random colours to the explosions and made the speed a little variable. --- explosion.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/explosion.py b/explosion.py index d88ac9e..b4ada9a 100644 --- a/explosion.py +++ b/explosion.py @@ -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 @@ -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): @@ -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 @@ -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 @@ -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: @@ -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 @@ -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)