Create a graphic animation of fireworks in Python (with Turtle) with me to celebrate the 4th of July! Use loops and random numbers to draw colorful fireworks in the night sky.
Who is this for?
- Language: Python (with Turtle)
- Juni Level: Python Level 1
- Ages: 11+
- Coding experience: Beginner, or familiar with basic concepts
- Difficulty Level: Easy, but every student is different!
|Core concepts used||Things to know beforehand|
|Random numbers||How to move/turn the Turtle|
|How to affect the screen|
Click run to see the Fireworks project in action below. You can also view my project solution code if you get stuck.
What to look out for:
- The background is black, like the night sky.
- There's lots of fireworks, but they all have the same shape.
- Each one has a random color, random size, and random location.
- If you slow down the speed, you can see that each firework is made by making 36 lines.
- To make each line, the turtle goes forward, then goes backward the same distance, and then turns a little to get ready for the next line.
General Steps to Build the Project
- Make just one firework, without any of the randomness.
- Make that firework appear in a random place.
- Make that firework a random size.
- Make that firework a random color.
- Make the background black.
- Use a loop to make your code draw lots of fireworks.
Step 1: Make just one firework, without any of the randomness
- Then, create a variable to store your turtle.
- Then, make the first line of your firework.
- Make sure to go forward and then backward go that you end up back where you started!
- Then, turn the turtle so that he’s ready for the next line.
Hint: How much should we turn the turtle? There’s 360 degrees in a circle, and in the example each firework had 36 lines, so we can divide those numbers to figure out how many degrees we should turn!
- Now, use a loop to make the turtle draw 36 lines!
Hint: Remember, you can use
for i in range():to repeat a piece of code; you just put the number of repetitions you want inside of range’s parentheses!
Step 2: Make that firework appear in a random place
- Then, before our firework code, create a variable and store our random x position.
Hint 1: Remember, you can use
random.randint()to generate a random number! Just decide on the range you want, and put those numbers in
randint’s parentheses! For example, if I want a random number from 1 to 10, I would write
Hint 2: Your Python with Turtle screen’s x and y coordinates normally range from about -250 to 250, but it varies from screen to screen!
- Then, create a variable and store our random y position.
Hint: It’s going to be really similar to what you just did for the x position!
goto()to make your turtle go to the random position before it draws its firework.
Step 3: Make that firework a random size
- Before your firework code, create a variable and store a random number for the length of the firework’s lines.
- Replace the number you’re currently using to tell the turtle how far to go with your new variable.
Step 4: Make that firework a random color
- Before our firework code, create a random number between 0 and 255, and store it in a variable. This will represent how much red is in our color!
- Right after that, create a random number between 0 and 255, and store it in a variable. This will represent how much green is in our color!
- Right after that, create a random number between 0 and 255, and store it in a variable. This will represent how much blue is in our color!
color()and the three variables you just made to make your turtle turn a random color.
Step 5: Make the background black
- At the top of your code, right under your import statements, make a new variable and set it equal to
turtle.Screen(). This will represent our screen!
- Use the function
bgcolor()on your new screen variable to change the background color of the screen! Put the name of the color you want inside of its parentheses to change the screen to that color.
Step 6: Use a loop to make your code draw lots of fireworks
Take all of the code we made that affects how our firework looks, and put it in a loop! Decide how many fireworks you want, and use that number when you’re creating your loop!
If you want, use what we learned to create a bunch of stars in the sky before you start drawing your fireworks! Check out this example of how I made both fireworks and stars in my sky.
Stars are like fireworks, but they only have 5 lines instead of 36. They’re also a lot smaller! I made mine all white, but you can make yours different colors if you want.
Great job! Want to keep learning?
We hope you enjoyed building Maya's project! To keep practicing or learning, check out more of our coding tutorials on our blog.
Looking up your coding questions is one of the best ways to learn!
Another great way to learn is from an experienced coder or instructor. Juni Learning CS Instructors like Maya work closely with students ages 8-18, and are specially trained to adapt to each child's unique learning style, pace, and interests.
Read more about how Juni teaches coding for kids, or if you have questions, speak with a Juni Advisor by calling (650) 263-4306 or emailing firstname.lastname@example.org.