Brought to you by Sarah Rappaport, Instructor at Create & Learn
Python is a great language for young coders to learn. It is designed to be human friendly, allowing students to develop coding skills without some of the frustrations of more cumbersome languages. Python also has a huge number of use cases: data science, software, art, Artificial Intelligence, and more!
Today, we’re going to walk you through building your first animation in Python - a stick figure!
You need no Python experience to work through this tutorial, but we recommend you have some experience with Scratch, a course I also teach, or another language.
To start this project, you’ll be using a template in Trinket.io. Trinket.io is an interactive development environment, or IDE for short. We use IDEs to code in Python (and other languages), and Trinket.io is especially useful in that we can use it to easily share code online.
If you would like to save your work, you’ll need a Trinket.io account. Once you’ve created one, you can save by clicking remix. All your work will be saved in your Trinket.io library.
Take a look at your IDE. You’ll notice that the lines are numbered on the left side of the screen. This is especially useful when communicating where you are in your code - this tutorial will use line numbers in the reference code we show you. Additionally, notice that the starter code, displayed on the left side of the screen, has lots of different colors of text. This is a nice feature of IDEs that helps to visually show what different pieces of your code do.
Finally, if you make any changes in your code, they won’t show up on the right side of your screen (your output console) until you run your code. You run your code by clicking the “play” button.
Step 1. Choose a Background
Our first step is to choose a background for our project. To help you, we’ve chosen four different options, shown on lines 5, 6, 7, and 8.
You’ll notice that these backgrounds are currently green, and have a # sign in front of them. This is called a comment. Comments are lines of code that the computer will ignore when it runs your code. To choose a background, you need to delete a # before the line with the background you like:
Now, the computer will read it!
Step 2: Choose a Character
When you run your code (press the play sign!) you will now see your background. You will also see a turtle. The turtle is your drawing “pen,” and will be the device we use to draw. We are not stuck with a turtle, however, you can choose a different character, or what you might also know as a “sprite.”
To do this, we’re going to look at Trinket.io’s image library. To find the library, click on the image button.
Find a character you like! Once you’ve chosen, highlight the entire name (including the file extension, .png) and copy it.
Click on main.py to get back to your code window. Then, paste the file name between the two sets of quotation marks on line 12. If you run your code now, you’ll see your chosen character appear!
Step 3. Start Drawing
We’re all ready to start drawing our stick figure! To start, we will draw the body. To draw in Python, we use functions from the Turtle library, which has lots of tools that allow you to be as creative and innovative in your art as you would like.
Two of the most important drawing functions we’ll learn today are pendown() and penup(). Call
- pendown() when you want to put your virtual pen on the canvas and leave a mark, and
- penup() when you want to move around the canvas without drawing.
Drawing in Python involves moving in straight lines and turning. You’ll accomplish these moves using the following commands:
The commands forward(100) and backward(200) move your character forward or backward the number of steps listed in between the parentheses - so in this case, 100 steps forward, 200 steps backward. You can change these numbers to whatever you want.
The commands right(90) and left(180) turn your character the number of degrees listed in between the parentheses - so in this case, 90 degrees to the right, or 180 degrees to the left. You can change these numbers as well.
Draw the Body
We’ll use a combination of all these functions to draw the body. Try typing the following code into your IDE and run your code to see what happens!
Draw the Legs
Let’s give our stick figure some legs. We’ll use the same commands we’ve learned already, but change up the angles and the distances. Notice that after we draw the legs, we’re going to move up to the top of the body. What line makes this happen?
Draw the Arms
We can reuse the code we’ve used for the legs to draw the arms. But let’s switch it up a bit and change the color of our pen. Right now, we’re using the default color, black. To change the color to something else, we’ll use the command color(“green”). Trinket.io has a lot of color options.
Notice that we put “green” in quotation marks.
Draw the Head
To draw a head, we’re going to introduce a new function: circle(50). This function lets us draw a circle of radius 50 - or whatever radius you want. Before you draw your circle, try changing the color to something new.
Draw a Smile
Our final step today is to give our stick figure a smile. We can do this using circle again - all we have to do is add another input. circle() can take two inputs (also called arguments) - the radius, and the extent, or the number of degrees around the circle that you want to move.
Examine line 56 in the code below to see how we add a second argument, 180, which instructs Python to draw 180 degrees of a circle.
Note that on line 57, I call the command hideturtle(). This command hides my character so it doesn’t sit on top of my drawing.
Your final drawing should look something like this:
Step 4: Challenges
Congratulations! You’ve just drawn your first animation in Python! There’s other commands you can use to enhance your drawing toolkit, but you’ve learned many of the key drawing tools today. Some things you can add to your stick figure:
- Add eyes
- Try typing dot(30) and seeing what it does :)
- Add hair
- Add clothing, a hat, or some shoes
What else can you think of to add? What do you want to create?
Want to create new animations, games, quizzes, and more? Keep learning Python with Create and Learn. We offer three levels, and a free intro class. Your child will build more animations, and learn the computer science fundamentals through project-driven classes.
At Create & Learn, every day can be a new opportunity to learn a programming language. Join our free introduction classes in Scratch, Minecraft, Data Science, Python, and more. Or check out all 16 courses we offer, from beginner to mastery, where every child can find topics they love.
Create & Learn is on a mission to power up your child’s future with courses in AI, Robotics, Data Science, and more. Designed for children grades 2-9, classes are delivered virtually with live, expert instructors. Lessons are interactive, foster creativity, and make real life connections across industries so your child can explore the latest technologies and have fun! www.createandlearn.us