DEMO: A lot of code is needed to create a firework! This demo walks you through the algorithm (steps) to make a firework, and why certain pieces of information are needed.

Notice that after line 26, all code is indented (has 4 spaces before the code) inside the click() event. That's because all that code only runs once you click on the stage!

  • Click Run to see the demo and follow the instructions on the stage.

To navigate the page using the TAB key, first press ESC to exit the code editor.

#################################################################### # PROVIDED FUNCTION # #################################################################### ### This is the math for step 2! ### def return_trajectory(speed, angle): radians = math.radians(angle) x_velocity = speed * math.cos(radians) y_velocity = speed * math.sin(radians) return (x_velocity, y_velocity) #################################################################### ### Step 1! This is the information for how many lines in each ### firework, the colors, and how fast each particle should move! stage.set_background_color("black") amount = 10 speed = 5 color_list = ["purple", "yellow", "red"] stage.disable_all_walls() ### EVERYTHING ELSE takes place inside this click event def click(): x = stage.click_x() y = stage.click_y() ### Step 2: Make the firework! ### particle_list = [] for color in color_list: for counter in range(amount): # sprite = codesters.Circle(x, y, diameter, "color") sprite = codesters.Circle(x, y, 10, color) particle_list.append(sprite) ### Step 3: Make the firework burst outward! angle = 0 for count, p in enumerate(particle_list): count += 1 speed_values = return_trajectory(speed, angle) p.set_x_speed(speed_values[0]) p.set_y_speed(speed_values[1]) angle += 360/amount if count % amount == 0: stage.wait(.1) stage.event_click(click)
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)