Who is this Project For?
This is an intermediate-level project for those who are new to Python. At this point, you should already know the basics of using Turtle, such as drawing shapes, controlling the pen with fill and colour change commands, and controlling the pen’s location in the window. Basic Python experience is needed as well, such as knowing booleans, creating classes, and defining functions. These will be the core concepts used in this project. Note that this project is also quite long, but although the project as a whole may take some time to complete the steps and concepts are quite simple and easy to follow.
What Will We Learn?
This project focuses on using pen commands and functions to draw the final image. By the end of the project, you will be able to confidently use these commands to create images in the Turtle window.
Features to Consider:
- The project should be able to create a clear image
- The drawing should use multiple colours
- he drawing should be done in a reasonable amount of time
This project can be broken down into 6 main steps:
- Setup Turtle and create Initial Functions
- Define facial features
- Draw the body
- Draw head and upper body
- Draw left hand and leg
- Draw right hand and leg
- Draw the tail
- Set the locations of the facial features
- Define the main function
Step 1: Setup Turtle and Create Initial Functions:
First, we’ll start by importing the turtle library. We do this by typing
Next, we will need to define our position function. To do this we will set up a function with our X and Y coordinates and use these to define the turtle’s position. We’ll type:
Now that we have our getPosition function defined, we can go ahead and begin coding the other main functions for the drawing. But first, we’ll need to create a class for our Pikachu. In this case, we’ll be creating a class called “Pikachu” that will contain all of the functions we’ll use to draw the image.
Underneath, we’ll define two of our most important functions: _init_ (self) and noTrace_goto. The code for these functions will look like this:
Notice that the self function starts by defining the turtle, setting the pen size to 3 and the speed to 9, and also gets the turtle’s position. They are the basic setup lines of code that will allow the image to be drawn as efficiently as possible and sets the line size to the proper width. The noTrace_goto function is a function that will pick up the pen, move it to the desired location, then set the pendown to draw again.
This function will be used all throughout the project.
Step 2: Define facial features
Now that the initial functions have been created it’s time to move on to define the facial features. This will include the code for the eyes, mouth, nose, cheeks, and ears. We’ll start by creating the code for the left eye. This will be made up of two circles inside of one another and then a smaller white one to act as are flection in the eye. We’ll start by creating the function itself and setting up the turtle. To do this we will type:
Next, we’ll set the turtle’s heading to 0 by using the turtle.seth() function.
Hint: This will set the turtle’s orientation to a specific angle, in this case, we’re using 0.
Now we’ll begin the circles. Start by choosing a dark grey colour for the eye, then begin filling in a circle with a radius of 22 pixels. Once this is done we will end the fill to complete the circle. When complete, the code will look like this:
Now we’ll make the other two smaller circles. For the first, we’ll make the turtle move 10 pixels up, before setting the colour to 0 and drawing a circle with a radius of10 pixels. This will use the same basic
code as the first circle. Once this is done, we’ll make the third circle. The code is the same, but we will move the turtle to (x+6, y+22) and the colour of the circle will be white to act as a highlight.
Hint: You can change the colour hex codes used in the code if you want to customize the colours of your Pikachu!
After these steps are complete the code will be:
Now it’s time to define the right eye. The code for the first two circles will be the exact same as the left eye. The third circle will also be very similar, except when setting the x and y-coordinates they will be (x-6, y+22). Once this function has been created it should look like this:
Note: We have written a line of code saying that t = self.t. This line simply defines the turtle and will allow us to draw with it.
Now we’ll write the code for our Pikachu’s mouth. We’ll start by creating a mouth function and setting the turtle’s location and colour. In this case, we’re setting the colour to a dark red colour. After this is complete the code will look like this and we’re ready to begin!
First, we’ll start by creating two lists. In this case, we’ve called them l1 and l2. Then, we’ll set the turtle’s heading to 190 degrees and create a variable with a value of 0.7. This variable will be used to control how far the turtle moves. Then, we’ll create a for loop to move the turtle right 3 degrees and forward by the number of pixels in the variable we just created. It will then add the position to the “l1” list using the append function, and the loop will repeat 28 times. The code will look like this:
Then we’ll set the heading to 10 degrees again and repeat the loop, this time appending the values to the “l2” list instead. Once this is done the inner mouth code will look like this:
Now it’s time to move on and create the code for the upper lip, tongue, and nose. For the upper lip, we will start by setting the turtle’s heading to 10 degrees, then will draw two half-circles, turning the turtle left by 180 degrees after the first circle. One will be (-50, 15) and the other will be (-50, 40). These will create one of the bumps for the upper lip. Then we’ll set the turtle’s heading to 233degrees and make two more half-circles, one with (-50, 55) and the other being(50, 12.1).
Hint: When using the circle method, we can type turtle.circle(100, 180). In this example, the circle will have a radius of 100 pixels and will only draw 180 degrees of the circle (a half-circle).
Then we will end the fill using the end_fill method. Once the upper lip code is complete it should look like this:
For the tongue ,we will start by making the turtle go to (15, 54) and set the fill colour to light pink. Then we’ll begin filling and set the pen’s heading to 145 degrees before making a half-circle with measurements of (40, 86). Then we will lift the turtle’s pen and use two for loops to decide where the pen should go. For reversed values in the l1 list, the turtle will go to the values of position 1and position 2 + 1.5. The second for loop will be the same, except the values are taken from the l2 list instead. Then, the turtle will place the pen down and end the fill. Once the tongue code is complete it will look like this:
The nose has a very simple code. All we will do is go to (-17, 94), set the pen’s heading to 8degrees, then move forward 4 pixels and backward 8. The code for this step will be:
Now that the mouth and eyes are complete it’s time to add the code for the cheeks. We’ll start by creating the leftCheek function and defining the turtle and its location with the goto(x, y) function. Then, we’ll set the turtle’s heading to300, the colour to dark pink, and begin filling. We will use for loops and if statements to draw the cheeks. We’ll start by creating a variable (in this case we’ve titled it “a”), and setting its value to 2.3. Then, we create a loop to run 120 times. Inside, we’ll check to see if the number of times the loop has run is between 0 and 30 or 60 and 90. If so, “a” is equal to itself minus 0.5.The turtle will then turn left 3 degrees and move forward by “a” pixels. Otherwise, “a” is equal to itself plus 0.5 and will turn left 3 degrees and move forward by a value of “a”. After this is done, we will end the fill and the code will look like this:
The right cheek will use the same code, but at the beginning, the heading will be set to 60instead, so the code will be
Finally, we’re onto making the coloured tips of Pikachu’s ears. These are the last facial features to code for this step. As usual, we will start by defining the function and turtle, and set the turtle coordinates to x and y. Then, the pen colour will be set to black and we will begin filling. The code for this section will look like this:
Notice how we've set the headings to 330, 219, and 110 in between making the circles, and we've made four partial circles with different radii. The code for the right ear is very similar and follows the same steps but with different coordinates and values for the circle radius. The code for the right ear is:
And now we’re done with the facial features and are ready to move on to creating the body!
Step 3: Draw the body:
This step consists of multiple components:
- Draw head and upper body
- Draw left hand and leg
- Draw right hand and leg
Let’s start by creating the head and upper body. We’ll start by creating a body function, then we’ll define the turtle, set the pen colour to a bright yellow, and begin filling. The code for the right side of the face and right ear will look like this:
Notice that we have used a variety of partial circles to create the ear shape and the side of the head, and the turtle’s heading is adjusted every time. Next, we’ll create the rest of the upper body. This is done by simply setting the turtle’s heading to 150 and making a 70-degree circle with a radius of 150. The code for this step will be:
Now it’s time to code the left side of the head and the left ear. Like the right ear, this is made up of many semicircles and we need to make sure to adjust the turtle’s pen angle in between each circle. The code for the right side of the face is:
Now that the face and head are complete it’s time to move on to the left side of the body. We’ll start with the left arm and leg. Like the rest of the body, this step consists mainly of making many semicircles to act as curves for the body. The left arm will have circles with large radii to make the main arm, then the fingers will be much smaller with sharper angles. We will be using thet.seth() function to adjust the pen angle after every circle. Once the left side of the body is complete, the code will look like this:
Now that the left side of the body is complete we’ll add the horizontal line to transition from the left to the right side of the body. This will be done by simply setting the heading to 0 and making a circle with the measurements (-700, 10), like so:
Finally, it’s time to code the right side of the body, including the arm and leg. As with the left side, the right side is primarily made up of semicircles and angle changes, with some forward movement for straight lines. You may notice that some of the code is very similar between the two sides, with the angles changed slightly or measurements adjusted. This simplifies the coding process because once you understand the basics the rest becomes much quicker to code. Follow this code below to draw the right side of Pikachu’s body:
Now that the body is complete, it’s time to move on to the final details– the fingers. To make the fingers we’ll start by using our noTrace function to move the turtle to(130, 125). Then, we’ll set the angle to -20 degrees, move forward 5 pixels, and draw a small semicircle with the measurements (-5, 160) before removing 5more pixels forward. This will draw the thumb. Next, we’ll add two more fingers by going to (166, 130). We will set the turtle’s heading to -90 degrees, move forward 3 pixels, and make another small circle with the measurements (-4,180). We will repeat this code once more to make the next finger, and once this step is complete the code should look like this:
Step 4: Draw the tail
Now that Pikachu’s body and facial features have been completed, it’s time to draw the tail. Because it is shaped like a zig-zag, this section of code will have many more forward movements than the previous sections. We’ll start by moving the turtle to (168, 134). Then, we’ll select the same bright yellow colour that we used on the body for the tail and begin the pen’s filling. Afterward, we will adjust the pen’s angle and move forward. These steps will repeat many times until they reach Pikachu’s arm. Then, the tail will curve around the arm before continuing the zigzag shape. For the measurements and angle changes needed to draw the tail correctly, refer to this code:
And now that the tail is complete, it’s time to add the coloured pattern at the bottom. To do this, we’ll start by setting the pen colour to a dark brown colour. Then we’ll set the heading to 30, move forward 40, set the heading to 100, and move forward 40 again. This will make one of the pattern’s spikes. Then, we will repeat this pattern two more times for a total of three spikes. After this is done, the pen will follow the pre-drawn tail to close up the shape before filling it in. After the tail pattern is complete the code will look like this:
Step 5: Set the locations of the facial features
Now that the body is complete it’s time to set locations for all of the facial features. You may have noticed that throughout the code we have not referred to specific coordinates often. In this step we will be defining the pen’s starting point for each step. The mouth will start at (-5, 25), the cheeks will be at (-126, 32) and (107, 63), the ears will be at (-250, 100) and (140, 270), and the eyes will be positioned at (-85, 90) and (50,110). Finally, we will hide the turtle so that it is not visible once the image is complete. After this step is complete the code will be:
Step 6: Define the main function
Finally, all that’s left is to define our start and main functions. The start function is created by simply setting t to self.body() like so:
Now all that’s left is to make and run the main function. To do this, create a new function, and underneath set the turtle screen size to 800x600 pixels. We will title the project “Pikachu”. Then, we will start the Pikachu function we created earlier and set up the turtle main loop as well. Finally, we will call the main function to run the entire program. After this step is complete the project is finished and the code should look like this:
And now we’re done! Feel free to test your code and see how it works. If you’re stuck or have any issues with your code, try reviewing it to see if you’ve made any mistakes, or check out the code mentioned above as a reference.
Keep Learning: Python for Kids
Geekedu offers Python coding for kids that will give kids and teenagers a head start in Python coding. Outside of Python, we also teach coding courses for kids 8-18 in a variety of languages like Java, C++, Animation and more.
Check out our online courses for kids, or contact our admissions team to learn which course is best for your student!