Image attribution: Claudio Giovenzana www.longwalk.it
Some time back I had written a post with a simple turtle graphics program in Python:
(The history of turtle graphics is of interest, including the early years, when they used actual robots for the turtles, which were controlled by programs). The Wikipedia page in the link above has the details.)
Here is the post I wrote earlier:
Python, meet Turtle
Python has support for turtle graphics in the standard library.
Today I wrote a different kind of turtle graphics program: a simple drawing program that allows you to interactively draw figures using a few keys on the keyboard.
The program is a first version, so is not polished. But it works, as far as I tested it.
You can use the following keys to control the turtle and draw:
- up arrow to move forward
- down arrow to move backward
- left arrow to turn left 45 degrees
- right arrow to turn right 45 degrees
- h or H key to move the turtle to its home position (center of screen, heading east)
- c or C key to clear the screen
- q or Q to quit the program
Note: If you do Clear and then Home, and if your turtle had been away from the home position when you did the Clear, the screen will get cleared, but then the Home action will draw a line from where the turtle just was to its (new) home position. If that happens, just press C again to clear the screen of that line.
The better way is to do Home first and then Clear.
Here is the code for the program:
# Program to do drawing using Python turtle graphics. # turtle_drawing.py v0.1 # Author: Vasudev Ram # http://jugad2.blogspot.in/p/about-vasudev-ram.html # Copyright (C) 2016 Vasudev Ram. import turtle # Create and set up screen and turtle. t = turtle # May need to tweak dimensions below for your screen. t.setup(600, 600) t.Screen() t.title("Turtle Drawing Program - by Vasudev Ram") t.showturtle() # Set movement step and turning angle. step = 160 angle = 45 def forward(): '''Move forward step positions.''' print "forward", step t.forward(step) def back(): '''Move back step positions.''' print "back", step t.back(step) def left(): '''Turn left by angle degrees.''' print "left", angle t.left(angle) def right(): '''Turn right by angle degrees.''' print "right", angle t.right(angle) def home(): '''Go to turtle home.''' print "home" t.home() def clear(): '''Clear drawing.''' print "clear" t.clear() def quit(): print "quit" t.bye() t.onkey(forward, "Up") t.onkey(left, "Left") t.onkey(right, "Right") t.onkey(back, "Down") t.onkey(home, "h") t.onkey(home, "H") t.onkey(clear, "c") t.onkey(clear, "C") t.onkey(quit, "q") t.onkey(quit, "Q") t.listen() t.mainloop()Support for more turtle actions (see the Python turtle module's docs linked above) can be added, for many of them on the same pattern as the existing functions, without changing the overall structure of the program. Some others may require changes to the design, which is very simple as of now. (I applied the principle Do The Simplest Thing That Could Possibly Work.)
Also, a point to note: many of the capabilities of the turtle module are available in both procedural and object-oriented versions. Here, to keep it simple, I've used the procedural style.
Here are a few screenshots of drawings I created by running and using this program:
A diamond (just a square tilted):
A figure made out of squares:
The image at the top of the post is of Olive ridley sea turtles.
- Vasudev Ram - Online Python training and programming Signup to hear about new products and services I create. Posts about Python Posts about xtopdf My ActiveState recipes