Skip to content

Commit

Permalink
Update 02_07_challenge.py
Browse files Browse the repository at this point in the history
Add comments to challenge file
  • Loading branch information
REMitchell committed Jul 21, 2022
1 parent e35b1dd commit 8697180
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions exercise_files/02_07_challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@
import time
from termcolor import colored

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

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

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

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

# Clear the terminal and then print each line in the canvas
def print(self):
self.clear()
for y in range(self._y):
Expand Down Expand Up @@ -51,15 +59,24 @@ def left(self):
self.draw(pos)

def draw(self, pos):
# Set the old position to the "trail" symbol
self.canvas.setPos(self.pos, self.trail)
# Update position
self.pos = pos
# Set the new position to the "mark" symbol
self.canvas.setPos(self.pos, colored(self.mark, 'red'))
# Print everything to the screen
self.canvas.print()
# Sleep for a little bit to create the animation
time.sleep(self.framerate)

# Create a new Canvas instance that is 30 units wide by 30 units tall
canvas = Canvas(30, 30)

# Create a new scribe and give it the Canvas object
scribe = TerminalScribe(canvas)

# Draw a small square
scribe.right()
scribe.right()
scribe.right()
Expand Down

0 comments on commit 8697180

Please sign in to comment.