|
2 | 2 | import time |
3 | 3 | from termcolor import colored |
4 | 4 |
|
| 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 |
5 | 7 | class Canvas: |
6 | 8 | def __init__(self, width, height): |
7 | 9 | self._x = width |
8 | 10 | self._y = height |
| 11 | + # This is a grid that contains data about where the |
| 12 | + # TerminalScribes have visited |
9 | 13 | self._canvas = [[' ' for y in range(self._y)] for x in range(self._x)] |
10 | 14 |
|
| 15 | + # Returns True if the given point is outside the boundaries of the Canvas |
11 | 16 | def hitsWall(self, point): |
12 | 17 | return point[0] < 0 or point[0] >= self._x or point[1] < 0 or point[1] >= self._y |
13 | 18 |
|
| 19 | + # Set the given position to the provided character on the canvas |
14 | 20 | def setPos(self, pos, mark): |
15 | 21 | self._canvas[pos[0]][pos[1]] = mark |
16 | 22 |
|
| 23 | + # Clear the terminal (used to create animation) |
17 | 24 | def clear(self): |
18 | 25 | os.system('cls' if os.name == 'nt' else 'clear') |
19 | 26 |
|
| 27 | + # Clear the terminal and then print each line in the canvas |
20 | 28 | def print(self): |
21 | 29 | self.clear() |
22 | 30 | for y in range(self._y): |
@@ -51,15 +59,24 @@ def left(self): |
51 | 59 | self.draw(pos) |
52 | 60 |
|
53 | 61 | def draw(self, pos): |
| 62 | + # Set the old position to the "trail" symbol |
54 | 63 | self.canvas.setPos(self.pos, self.trail) |
| 64 | + # Update position |
55 | 65 | self.pos = pos |
| 66 | + # Set the new position to the "mark" symbol |
56 | 67 | self.canvas.setPos(self.pos, colored(self.mark, 'red')) |
| 68 | + # Print everything to the screen |
57 | 69 | self.canvas.print() |
| 70 | + # Sleep for a little bit to create the animation |
58 | 71 | time.sleep(self.framerate) |
59 | 72 |
|
| 73 | +# Create a new Canvas instance that is 30 units wide by 30 units tall |
60 | 74 | canvas = Canvas(30, 30) |
| 75 | + |
| 76 | +# Create a new scribe and give it the Canvas object |
61 | 77 | scribe = TerminalScribe(canvas) |
62 | 78 |
|
| 79 | +# Draw a small square |
63 | 80 | scribe.right() |
64 | 81 | scribe.right() |
65 | 82 | scribe.right() |
|
0 commit comments