-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polymorphism.py
119 lines (90 loc) · 2.69 KB
/
Polymorphism.py
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
115
116
117
118
119
import turtle
class GoToCommand:
def __init__(self, x, y, width=1, color='black'):
self.x = x
self.y = y
self.width = width
self.color = color
def draw(self, turtle):
turtle.width(self.width)
turtle.pencolor(self.color)
turtle.goto(self.x, self.y)
class CircleCommand:
def __init__(self, radius, width=1, color='black'):
self.radius = radius
self.width = width
self.color = color
def draw(self, turtle):
turtle.width(self.width)
turtle.pencolor(self.color)
turtle.circle(self.radius)
class BeginFillCommand:
def __init__(self, color):
self.color = color
def draw(self, turtle):
turtle.fillcolor(self.color)
turtle.begin_fill()
class EndFillCommand:
def __init__(self):
pass
def draw(self, turtle):
turtle.end_fill()
class PenUpCommand:
def __init__(self):
pass
def draw(self, turtle):
turtle.penup()
class PenDownCommand:
def __init__(self):
pass
def draw(self, turtle):
turtle.pendown()
class PyList:
def __init__(self):
self.items = []
def append(self, item):
self.items = self.items + [item]
def __iter__(self):
for c in self.items:
yield c
def main():
filename = ('turtle_file.txt')
t = turtle.Turtle()
screen = t.getscreen()
file = open(filename, 'r')
# Create a PYlist of hold the graphic command
graphicsCommand = PyList()
command = file.readline().strip()
while command != '':
if command == 'goto':
x = float(file.readline())
y = float(file.readline())
width = float(file.readline())
color = file.readline().strip()
cmd = GoToCommand(x, y, width, color)
elif command == 'circle':
radius = float(file.readline())
width = float(file.readline())
color = file.readline().strip()
cmd = CircleCommand(radius, width, color)
elif command == 'beginfill':
color = file.readline().strip()
cmd = BeginFillCommand(color)
elif command == 'endfill':
cmd = EndFillCommand()
elif command == 'penup':
cmd = PenUpCommand()
elif command == 'pendown':
cmd = PenDownCommand()
else:
raise RuntimeError('Unknown Command {}'.format(command))
graphicsCommand.append(cmd)
command = file.readline().strip()
for cmd in graphicsCommand:
cmd.draw(t)
file.close()
t.ht()
screen.exitonclick()
print ('Program executed Successfully')
if __name__ == '__main__':
main()