A simple drawing program that lets you use your keyboard to draw figures on screen, using the turtle graphics module built into Python.
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.
More details and sample output here:
http://jugad2.blogspot.in/2016/01/simple-drawing-program-with-python.html
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
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 = 10 angle = 45 def forward(): '''Move forward step positions.'''
def back(): '''Move back step positions.'''
def left(): '''Turn left by angle degrees.'''
def right(): '''Turn right by angle degrees.'''
def home(): '''Go to turtle home.'''
def clear(): '''Clear drawing.'''
def quit():
def penup():
def 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()
https://docs.google.com/a/cbsc.co.uk/document/d/1MOYeqaprp_9Fsnxg2lvyEsU7u7vZNu5Mmih4dvxUZD4/edit?usp=sharing