Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (85 loc) · 3.07 KB
/
Copy pathmain.py
File metadata and controls
114 lines (85 loc) · 3.07 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Example to show how module cmd is use. Example taken from:
https://docs.python.org/3.4/library/cmd.html
You might need to install the python module:
sudo apt-get install python3-tk
See more on turtle:
https://docs.python.org/3.4/library/turtle.html
"""
# pylint: disable=locally-disabled,unused-argument,too-many-public-methods
import cmd
#import sys
import turtle
class TurtleShell(cmd.Cmd):
"""
Example of class with command actions.
"""
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
prompt = '(turtle) '
file = None
# ----- basic turtle commands -----
def do_forward(self, arg):
'Move the turtle forward by the specified distance: FORWARD 10'
turtle.forward(*parse(arg))
def do_right(self, arg):
'Turn turtle right by given number of degrees: RIGHT 20'
turtle.right(*parse(arg))
def do_left(self, arg):
'Turn turtle left by given number of degrees: LEFT 90'
turtle.left(*parse(arg))
def do_goto(self, arg):
'Move turtle to an absolute position with changing orientation. GOTO 100 200'
turtle.goto(*parse(arg))
def do_home(self, arg):
'Return turtle to the home postion: HOME'
turtle.home()
def do_circle(self, arg):
'Draw circle with given radius an options extent and steps: CIRCLE 50'
turtle.circle(*parse(arg))
def do_position(self, arg):
'Print the current turle position: POSITION'
print('Current position is %d %d\n' % turtle.position())
def do_heading(self, arg):
'Print the current turle heading in degrees: HEADING'
print('Current heading is %d\n' % (turtle.heading(),))
def do_color(self, arg):
'Set the color: COLOR BLUE'
turtle.color(arg.lower())
def do_undo(self, arg):
'Undo (repeatedly) the last turtle action(s): UNDO'
def do_reset(self, arg):
'Clear the screen and return turtle to center: RESET'
turtle.reset()
def do_bye(self, arg):
'Stop recording, close the turtle window, and exit: BYE'
print('Thank you for using Turtle')
self.close()
turtle.bye()
return True
# ----- record and playback -----
def do_record(self, arg):
'Save future commands to filename: RECORD rose.cmd'
self.file = open(arg, 'w')
def do_playback(self, arg):
'Playback commands from a file: PLAYBACK rose.cmd'
self.close()
with open(arg) as f:
self.cmdqueue.extend(f.read().splitlines())
def precmd(self, line):
line = line.lower()
if self.file and 'playback' not in line:
print(line, file=self.file)
return line
def close(self):
'Close the file.'
if self.file:
self.file.close()
self.file = None
def parse(arg):
'Convert a series of zero or more numbers to an argument tuple'
return tuple(map(int, arg.split()))
if __name__ == '__main__':
print(__doc__)
TurtleShell().cmdloop()