Getting to know you
Grey overlay
Pink
Green
Blue
Cream
Liliac
Purple
Yellow
Objectives
Developing the Programming and Development and Algorithm learning strands, specifically:
- developing basic understanding of Python's turtle module
- developing skills in using basic functions in turtle module to create simple geometrical shapes
Introduction
Python Turtle
- Python is a highly popular programming language, used by many large organisations.
- Facebook, NASA, IBM, Google and Yahoo among others all use it extensively.
- It is considered by many to be one of the easiest languages to pick up.
- One of the big attractions of Python is that many 'libraries' are available to provide it with extra functionality.
- A library is a set of extra functions that help perform different types of task. There are libraries for advanced maths, graph drawing, graphics, game writing, random numbers and so forth.
- We're going to use a fun library called 'Turtle', which allows for the convenient drawing of lines and shapes.
Let's get started
- Load up the Python Development, called IDLE.
- You'll be presented with the Python Shell window, which looks like this…
- With the mouse in the shell window, type the following at the >>> Prompt.
- Hit enter after each line.
- (Note: Make sure you write Screen with a capital letter!
import turtle wn=turtle.Screen() wn.bgcolor("lightgreen") wn.title("Turtle Power")
- A graphics window (currently empty) will appear that we can draw into.
- We've called our screen object, "wn". Every time we want Python to control somthing to do with this window, we'll need to put wn. in front of the command.
- We also changed the background colour, and put a title on the window.
- Let's add a "turtle" to the screen. We can move this arounnd the screen to draw shapes and patterns. We'll call our turtle, "Tess".
tess=turtle.Turtle() tess.color("hotpink") tess.pensize(5) tess.shape("turtle")
- When we created a tess object, we then went on to set some of her properties - colour, thickness of the lines she'll draw and how she appears on the screen.
- Incidentally, a full colour list can be found here.
- We could add other turtle objects too, if we wanted, and have several drawing shapes on the screen. We might have a look at that later on.
- Next, lets start to draw. Type…
tess.forward(100) tess.left(90) tess.forward(100) tess.left(90) tess.forward(100) tess.left(90) tess.forward(100) tess.left(90)
- Your turtle should have traced a square shape for you.
Your turn
Try it
- Tip: You can move the turtle back to the centre, and clear the screen at any time by using the commands…
tess.home() tess.clear()
- Experiment with drawing lines until you're confident, then try the badge task.
Badge it
- Silver: Draw a red triangle, (screenshot the result, and upload the image file to BourneToLearn)
- Gold: Draw a pentagon, where each side is a different colour,
- Platinum: Create second turtle on the screen, and call it tom. Have this draw a black square, and the other turtle draw a green square, so that they look like this…
Write interpreter commands to draw a grid two squares tall and two squares wide (like the Windows logo). Each square should be a different colour.