create turtles with python -...
TRANSCRIPT
Create Turtles with PythonBY PATRICIA FOSTER / PROGRAMMING / OCTOBER 2017 ISSUE
Create turtles with Python, the programming language.
Turtles make great pets. They’re small, slow, and clean. Plus, who can
resist their cute, wrinkled faces peeping out of their shells?
Now, you can create your own pet turtle. All you need is a
programming language called Python, and an extension called
Turtle.
Instructions1. Open up your favourite web browser. Navigate to www.repl.it
Create Turtles with Python | beanz | https://www.kidscodecs.com/create-turtles-python/
1 of 7 6/14/18, 12:16 PM
2. Type ‘Python’ in the box. Make sure you select ‘Python (with
Turtle)’. Otherwise, the code won’t work!
3. Type this code in the left-hand editor:
4. Hit the run button.
See the turtle icon that appeared in the right-hand screen? If you
1 from turtle import *23 maurice = Turtle()4 maurice.shape("turtle")5 screen = Screen()6 screen.onscreenclick(maurice.goto)
Create Turtles with Python | beanz | https://www.kidscodecs.com/create-turtles-python/
2 of 7 6/14/18, 12:16 PM
click inside the screen, your turtle comes running over. Have fun
playing with your new buddy!
Explanationfrom turtle import *
Turtle is a Python ‘module’. Picture a module as a shop full of nifty
gadgets. These gadgets aren’t included in a programming language
by default. You have to explicitly tell the Python interpreter that you
want to ‘borrow’ these gadgets and use them in your code. Once you
do, you can do all kinds of cool things!
The purpose of Turtle is to make it easy to draw shapes and lines.
And to create adorable turtles.
maurice = Turtle()
In most programming languages, if a word starts with a capital letters
and ends with brackets then it’s invoking a constructor. A
constructor builds an object. There are predefined objects, like
number generators, and web sockets, and turtles. You can also
Create Turtles with Python | beanz | https://www.kidscodecs.com/create-turtles-python/
3 of 7 6/14/18, 12:16 PM
create your own objects.
Here, we’re creating our pet turtle. I’ve named mine Maurice.
because he’s a sophisticated reptile. If you want to name your turtle
something else (Frankie, Tubs, Slowpoke), replace ‘maurice’ with the
name of your choice. But make sure you keep the same name
through all the code!
maurice.shape(“turtle”)
By default, Maurice is shaped like a little triangle. This code makes
the Python interpreter use a turtle icon instead. Want to check this?
Code can be confusing to read, especially when there’s a lot of it.
That’s why programmers use comments to explain pieces of their
code to other programmers. Comments are ignored by the computer
when running a program.
There’s another use for comments. The easiest way to understand a
single line of code is to ‘comment it out’. In other words, to,
‘transform the line into a comment’. It has the same effect as deleting
a line of code, except that you can still see the text. By ‘deleting’ this
line, you can then watch how the program changes.
To comment out a line, put a ‘#’ symbol in front of it. See how the
text turned green in the editor? Now hit the ‘Run’ button and see
what happens!
Create Turtles with Python | beanz | https://www.kidscodecs.com/create-turtles-python/
4 of 7 6/14/18, 12:16 PM
screen = Screen()
This line creates the screen object. It’s where Maurice is going to
play.
screen.onscreenclick(maurice.goto)
The first part of this line — screen.onscreenclick(…) — registers a
‘screen click’ event handler. The screen now pays attention to mouse
clicks. It remembers the coordinates where the mouse click
happened. They second part — maurice.goto — tells Maurice to go to
a pair of coordinates. Together, the ‘screen click’ event handler grabs
the coordinates of the mouse click, then passes them to Maurice,
who dashes over.
More Fun Things to DoHere’s more code you can add in between lines 4 and 5.
Change the colour of your turtle’s pen:
maurice.color(“blue”)
You can choose any colour you want: green, pink, yellow, orange.
Make sure your colour is written in between quotation marks!
Change your turtle’s speed:
Create Turtles with Python | beanz | https://www.kidscodecs.com/create-turtles-python/
5 of 7 6/14/18, 12:16 PM
maurice.speed(10)
The number inside the brackets is called the speed variable. It can be
any number between 0 and 10. 0 is a slow, plodding turtle and 10 is a
zooming, speedy one. Experiment with a couple different numbers!
Learn More
MORE ABOUT COMMENTShttps://www.kidscodecs.com/comments/
http://www.pythonforbeginners.com/comments/comments-in-
python
ABOUT OBJECTShttps://www.tutorialspoint.com/python/python_classes_objects.htm
ACTIVITIES WITH PYTHON AND TURTLEhttp://interactivepython.org/runestone/static/thinkcspy
/PythonTurtle/OurFirstTurtleProgram.html
http://openbookproject.net/thinkcs/python/english3e
Create Turtles with Python | beanz | https://www.kidscodecs.com/create-turtles-python/
6 of 7 6/14/18, 12:16 PM
/hello_little_turtles.html
https://michael0x2a.com/blog/turtle-examples
Share on Social Media
About the Author
Drag to outliner or Upload
Close
Patricia FosterPatricia Foster is a computer science student at Carleton University. Inaddition to working professionally as a software developer, she spendsher time reading and writing.
Create Turtles with Python | beanz | https://www.kidscodecs.com/create-turtles-python/
7 of 7 6/14/18, 12:16 PM