-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (99 loc) · 3.01 KB
/
main.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import pyb
SWIDTH = 127
SHEIGHT = 31
TILE_WIDTH = 4
CTRL_SENS = 3
BEGIN_COORDS = [15, 15]
BEGIN_DIR = [1, 0]
lcd_screen = pyb.LCD("X")
# lcd_screen.light(True)
lcd_screen.fill(0)
lcd_screen.show()
class Tile(object):
def draw(self, x, y, color=1):
for i in range(TILE_WIDTH):
for j in range(TILE_WIDTH):
lcd_screen.pixel(x + i, y + j, color)
def render(self):
return [self.draw(*args) for args in self.points]
class Snake(Tile):
def __init__(self, controller):
self.head = BEGIN_COORDS
self.direction = BEGIN_DIR
self.points = [self.head]
self.ctrl = controller
def _read_dir(self):
if self.ctrl.x() > CTRL_SENS:
self.direction = [-1, 0]
elif self.ctrl.x() < -CTRL_SENS:
self.direction = [1, 0]
elif self.ctrl.y() > -CTRL_SENS:
self.direction = [0, 1]
elif self.ctrl.y() < CTRL_SENS:
self.direction = [0, -1]
x, y = self.direction
x *= TILE_WIDTH
y *= TILE_WIDTH
return x, y
def move(self):
dx, dy = self._read_dir()
hx = self.head[0] + dx
hy = self.head[1] + dy
self.head = [hx, hy]
self.points.insert(0, self.head)
self.points.pop()
def eat(self):
if len(self.points) == 1:
newlast = self.head[:]
newlast[0] -= (self.direction[0] * TILE_WIDTH)
newlast[1] -= (self.direction[1] * TILE_WIDTH)
else:
plast, last = self.points[-2:]
newlast = last[:]
if plast[0] == last[0]:
if plast[1] > last[1]:
newlast[1] -= TILE_WIDTH
else:
newlast[1] += TILE_WIDTH
else:
if plast[0] > last[0]:
newlast[0] -= TILE_WIDTH
else:
newlast[0] += TILE_WIDTH
self.points.append(newlast)
self.render()
def _bite(self):
return self.head in self.points[1:]
def _fall_out(self):
x, y = self.head
out_width = x > SWIDTH + TILE_WIDTH or x < -TILE_WIDTH
out_height = y > SHEIGHT + TILE_WIDTH or y < -TILE_WIDTH
return out_width or out_height
def isdead(self):
return self._fall_out() or self._bite()
class Apple(Tile):
def __init__(self):
self.fall()
def fall(self):
x = pyb.rng() % SWIDTH // TILE_WIDTH * TILE_WIDTH - 1
y = pyb.rng() % SHEIGHT // TILE_WIDTH * TILE_WIDTH - 1
self.points = [[x, y]]
self.render()
def main():
snake = Snake(pyb.Accel())
apple = Apple()
while not snake.isdead():
lcd_screen.fill(0)
apple.render()
snake.render()
lcd_screen.show()
pyb.delay(200)
if snake.head == apple.points[0]:
snake.eat()
apple.fall()
snake.render()
lcd_screen.show()
snake.move()
lcd_screen.write("death...\n")
pyb.delay(100)
main()