Welcome, guest | Sign In | My Account | Store | Cart

A simple drawing program that lets you use your keyboard to draw figures on screen, using the turtle graphics module built into Python.

Python, 68 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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()

You can experiment with both using this recipe to draw shapes, as well as enhance it to support more drawing operations.

3 comments

Luke 7 years, 9 months ago  # | flag

Edited code so you can put the pen up with u and down with d

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

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 = 10 angle = 45 def forward(): '''Move forward step positions.'''

t.forward(step)

def back(): '''Move back step positions.'''

t.back(step)

def left(): '''Turn left by angle degrees.'''

t.left(angle)

def right(): '''Turn right by angle degrees.'''

t.right(angle)

def home(): '''Go to turtle home.'''

t.home()

def clear(): '''Clear drawing.'''

t.clear()

def quit():

t.bye()

def penup():

t.penup()

def pendown():

t.pendown()

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.onkey(penup, "u") t.onkey(penup, "U") t.onkey(pendown, "d") t.onkey(pendown, "D") t.listen() t.mainloop()