Skip to content

Commit 8697180

Browse files
committed
Update 02_07_challenge.py
Add comments to challenge file
1 parent e35b1dd commit 8697180

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

exercise_files/02_07_challenge.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@
22
import time
33
from termcolor import colored
44

5+
# This is the Canvas class. It defines some height and width, and a
6+
# matrix of characters to keep track of where the TerminalScribes are moving
57
class Canvas:
68
def __init__(self, width, height):
79
self._x = width
810
self._y = height
11+
# This is a grid that contains data about where the
12+
# TerminalScribes have visited
913
self._canvas = [[' ' for y in range(self._y)] for x in range(self._x)]
1014

15+
# Returns True if the given point is outside the boundaries of the Canvas
1116
def hitsWall(self, point):
1217
return point[0] < 0 or point[0] >= self._x or point[1] < 0 or point[1] >= self._y
1318

19+
# Set the given position to the provided character on the canvas
1420
def setPos(self, pos, mark):
1521
self._canvas[pos[0]][pos[1]] = mark
1622

23+
# Clear the terminal (used to create animation)
1724
def clear(self):
1825
os.system('cls' if os.name == 'nt' else 'clear')
1926

27+
# Clear the terminal and then print each line in the canvas
2028
def print(self):
2129
self.clear()
2230
for y in range(self._y):
@@ -51,15 +59,24 @@ def left(self):
5159
self.draw(pos)
5260

5361
def draw(self, pos):
62+
# Set the old position to the "trail" symbol
5463
self.canvas.setPos(self.pos, self.trail)
64+
# Update position
5565
self.pos = pos
66+
# Set the new position to the "mark" symbol
5667
self.canvas.setPos(self.pos, colored(self.mark, 'red'))
68+
# Print everything to the screen
5769
self.canvas.print()
70+
# Sleep for a little bit to create the animation
5871
time.sleep(self.framerate)
5972

73+
# Create a new Canvas instance that is 30 units wide by 30 units tall
6074
canvas = Canvas(30, 30)
75+
76+
# Create a new scribe and give it the Canvas object
6177
scribe = TerminalScribe(canvas)
6278

79+
# Draw a small square
6380
scribe.right()
6481
scribe.right()
6582
scribe.right()

0 commit comments

Comments
 (0)