Python Turtle Lessons 1-3 PDF

Summary

This document is a series of lessons on creating shapes and graphics using the Python Turtle module. It explains how to use the turtle graphics functions and provides tasks for practice.

Full Transcript

Python ​Turtle Lesson 1 Turtle Drawing We can create cool images and graphics in Python using a module called turtle - a set of ready-made functions designed for drawing shapes and lines. A turtle is like a cursor...

Python ​Turtle Lesson 1 Turtle Drawing We can create cool images and graphics in Python using a module called turtle - a set of ready-made functions designed for drawing shapes and lines. A turtle is like a cursor that moves around your screen, leaving a line behind it. Turtles can draw all kinds of shapes and pictures - you just need to give them the right commands. [​the​ academy_of_code] Python ​Turtle Drawing a Circle Firstly, we are going to draw a simple shape (a circle) using the turtle module. Import the turtle module. import​ turtle Make a turtle and load it into a variable. tina = turtle.Turtle() We have named the turtle Tina, however, you can give the turtle any name you want. Set the colour that the turtle will use to draw the shape. tina.color(​‘blue’​) Set the style of the turtle with a function called shape. tina.shape(​‘turtle’​) Set the turtle’s speed, choosing a number between 1 and 100 (100 is the fastest). tina.speed(10) Set the thickness of the line your turtle will draw. tina.pensize(4) Now, tell your turtle to draw a circle. tina.circle(60) ​# draws circle with radius of 60 pixels [​the​ academy_of_code] Python ​Turtle Tasks: 1. Run the code above. 2. Fill the inside of the circle with a colour (same colour as the outline). tina.fillcolor(​'blue'​) tina.begin_fill() tina.circle(60) tina.end_fill() 3. Fill the inside of the circle with a different colour (different colour as the outline). [​the​ academy_of_code] Python ​Turtle Drawing Location Sometimes we may wish to move the turtle before we start drawing something so we can draw shapes in different locations on the screen. Set up Tina the Turtle. Take the pen up off the page so Tina does not draw lines while moving to a different location. tina.penup() Tell Tina to go to a particular point on the screen by telling her the x-coordinate and the y-coordinate. tina.goto(30,-150) ​# x, y Note: ​The screen has coordinates that go from -150 to 150 in the x (horizontal) and y (vertical) directions. [​the​ academy_of_code] Python ​Turtle Put the pen back down on the screen so Tina can start drawing. tina.pendown() Start drawing your shape! tina.circle(50) [​the​ academy_of_code] Python ​Turtle Olympic Flag Project The Olympic Council of Ireland have asked you to draw the Olympic flag so they can use it on their new website. Use what you have learned in this lesson to write code to make Tina the Turtle draw the Olympic flag. You will find an image of this below, to help you. [​the​ academy_of_code] Python ​Turtle Lesson 2 Drawing a Square We can also draw shapes other than circles. Although, they are a little bit more complicated to draw. In order to draw other shapes, Tina must draw straight lines and then turn a certain number of degrees and then repeat. We are now going to draw a square using Tina the Turtle. Set up Tina like we did in the last lesson (import module, make turtle, set color, set speed etc.). Now, tell Tina where to draw. tina.forward(50) ​# go right 50 pixels tina.right(90) ​# turn right 90 degrees tina.forward(50) ​# go down 50 pixels tina.right(90) ​# turn right 90 degrees tina.forward(50) ​# go left 50 pixels tina.right(90) ​# turn right 90 degrees tina.forward(50) ​# go up 50 pixels Tasks: 1. Run the code above. 2. Fill the square with a colour. [​the​ academy_of_code] Python ​Turtle Drawing Other Shapes In order to draw any shape, we must know ​three ​things: How many lines there are. How long each line will be. The angle that Tina must turn after each line has been drawn. The only one from that list that may be difficult to know is the last one. So here’s a guide to help you calculate the angle that Tina must turn to draw the shape that you want! To calculate the amount of degrees you must turn to draw a certain shape, use the following formula: Number of degrees = 360 ÷ Number of sides in shape Let’s take a triangle for example. A triangle is a three-sided shape. So let’s fill that into our formula. Number of degrees = 360 ÷ 3 = 120 Hint​: use a calculator to do the maths if you can’t do it in your head! So the code to draw a triangle would look something like this: tina.forward(50) tina.right(120) tina.forward(50) tina.right(120) tina.forward(50) Tasks: 1. Write code to make Tina draw a pentagon (5 sides) 2. Write code to make Tina draw an octagon (8 sides) [​the​ academy_of_code] Python ​Turtle Fat Tony’s Pizzeria Project Fat Tony, a sweaty, overweight, middle-aged pizzeria owner wants you to come up with a new pizza recipe to lure in new customers. Use what you have learned in this lesson to write code to make Tina the Turtle draw a delicious new pizza design with your own favourite toppings. Hint: ​Once you have drawn the outer crust and base of the pizza, draw different shapes with different colours to draw your different toppings. [​the​ academy_of_code] Python ​Turtle Lesson 3 Writing with Turtles Other than drawing lines and shapes, Tina the Turtle can also write words and sentences onto the canvas. We are now going to write something onto the canvas using Tina the Turtle. Set up Tina the Turtle (import module, make turtle, set color, set speed etc.). Tell Tina what to write on the canvas. tina.write(​'Hello World!'​) You can change the font and font size of what Tina is writing. tina.write(​'Hello World!'​, font=(​'Arial'​, 12, '​ normal'​)) Tasks: 1. Run the code above. 2. Change the color of the font. 3. Change the font size. [​the​ academy_of_code] Python ​Turtle The Academy Of Code Poster Project The silly folks at The Academy Of Code headquarters are having trouble coming up with a new poster design for a campaign they are running to get more kids into coding (especially Python!). They want you to use everything you have learned up to know (drawing shapes, colouring shapes, writing words and sentences etc.) to design the poster that will be used in their new campaign. The best poster in the class will be featured on The Academy Of Code website! [​the​ academy_of_code]

Use Quizgecko on...
Browser
Browser