Skip to content

Commit b3c3d17

Browse files
committed
Read and draw from separated classes
1 parent c0f00e0 commit b3c3d17

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import turtle
2+
from turtlecommands import (
3+
GoToCommand,
4+
CircleCommand,
5+
BeginFillCommand,
6+
EndFillCommand,
7+
PenUpCommand,
8+
PenDownCommand,
9+
)
10+
11+
12+
class PyList:
13+
def __init__(self):
14+
self.items = []
15+
16+
def append(self, item):
17+
self.items = self.items + [item]
18+
19+
def __iter__(self):
20+
for item in self.items:
21+
yield item
22+
23+
24+
def main():
25+
filename = "./src/turtle_commands_multiplelines.txt"
26+
t = turtle.Turtle()
27+
screen = t.getscreen()
28+
graphic_commands = PyList()
29+
30+
with open(file=filename, mode="r") as file:
31+
command = file.readline().strip()
32+
33+
while command != "":
34+
if command == "goto":
35+
x, y = float(file.readline()), float(file.readline())
36+
width = float(file.readline())
37+
color = file.readline().strip()
38+
cmd = GoToCommand(x, y, width, color)
39+
40+
elif command == "circle":
41+
radius, width = float(file.readline()), float(file.readline())
42+
color = file.readline().strip()
43+
cmd = CircleCommand(radius, width, color)
44+
45+
elif command == "beginfill":
46+
color = file.readline().strip()
47+
cmd = BeginFillCommand(color)
48+
49+
elif command == "endfill":
50+
cmd = EndFillCommand()
51+
52+
elif command == "penup":
53+
cmd = PenUpCommand()
54+
55+
elif command == "pendown":
56+
cmd = PenDownCommand()
57+
58+
else:
59+
raise RuntimeError(f"Unknown command: {command}")
60+
61+
graphic_commands.append(cmd)
62+
command = file.readline().strip()
63+
64+
for cmd in graphic_commands:
65+
cmd.draw(t)
66+
67+
t.hideturtle()
68+
screen.exitonclick()
69+
70+
print("Program execution completed.")
71+
72+
73+
if __name__ == "__main__":
74+
main()

chap01_python101/turtlecommands.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class GoToCommand:
2+
def __init__(self, x: int, y: int, width=1, color="black"):
3+
self.x = x
4+
self.y = y
5+
self.width = width
6+
self.color = color
7+
8+
def draw(self, turtle):
9+
turtle.width(self.width)
10+
turtle.pencolor(self.color)
11+
turtle.goto(self.x, self.y)
12+
13+
14+
class CircleCommand:
15+
def __init__(self, radius, width=1, color="black"):
16+
self.radius = radius
17+
self.width = width
18+
self.color = color
19+
20+
def draw(self, turtle):
21+
turtle.width(self.width)
22+
turtle.pencolor(self.color)
23+
turtle.circle(self.radius)
24+
25+
26+
class BeginFillCommand:
27+
def __init__(self, color):
28+
self.color = color
29+
30+
def draw(self, turtle):
31+
turtle.fillcolor(self.color)
32+
turtle.begin_fill()
33+
34+
35+
class EndFillCommand:
36+
def __init__(self):
37+
pass
38+
39+
def draw(self, turtle):
40+
turtle.end_fill()
41+
42+
43+
class PenUpCommand:
44+
def __init__(self):
45+
pass
46+
47+
def draw(self, turtle):
48+
turtle.penup()
49+
50+
51+
class PenDownCommand:
52+
def __init__(self):
53+
pass
54+
55+
def draw(self, turtle):
56+
turtle.pendown()

0 commit comments

Comments
 (0)