-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsense_of_direction.py
More file actions
86 lines (68 loc) · 2.18 KB
/
sense_of_direction.py
File metadata and controls
86 lines (68 loc) · 2.18 KB
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
import os
import time
from termcolor import colored
import math
class Canvas:
def __init__(self, width, height):
self._x = width
self._y = height
self._canvas = [[' ' for y in range(self._y)] for x in range(self._x)]
def hitsWall(self, point):
return round(point[0]) < 0 or round(point[0]) >= self._x or round(point[1]) < 0 or round(point[1]) >= self._y
def setPos(self, pos, mark):
self._canvas[round(pos[0])][round(pos[1])] = mark
def clear(self):
os.system('cls' if os.name == 'nt' else 'clear')
def print(self):
self.clear()
for y in range(self._y):
print(' '.join([col[y] for col in self._canvas]))
class TerminalScribe:
def __init__(self, canvas):
self.canvas = canvas
self.trail = '.'
self.mark = '*'
self.framerate = 0.05
self.pos = [0, 0]
self.direction = [0, 1]
#from the math library it takes cos and sin
def setDegrees(self, degrees):
radians = (degrees/180) * math.pi
self.direction = [math.sin(radians), -math.cos(radians)]
def up(self):
self.direction = [0, -1]
self.forward()
def down(self):
self.direction = [0, 1]
self.forward()
def right(self):
self.direction = [1, 0]
self.forward()
def left(self):
self.direction = [-1, 0]
self.forward()
#update the positions
def forward(self):
pos = [self.pos[0] + self.direction[0], self.pos[1] + self.direction[1]]
if not self.canvas.hitsWall(pos):
self.draw(pos)
def drawSquare(self, size):
for i in range(size):
self.right()
for i in range(size):
self.down()
for i in range(size):
self.left()
for i in range(size):
self.up()
def draw(self, pos):
self.canvas.setPos(self.pos, self.trail)
self.pos = pos
self.canvas.setPos(self.pos, colored(self.mark, 'red'))
self.canvas.print()
time.sleep(self.framerate)
canvas = Canvas(30, 30)
scribe = TerminalScribe(canvas)
scribe.setDegrees(135)
for i in range(30):
scribe.forward()