leds instead of on screen squares
This commit is contained in:
parent
ef90ca2e27
commit
61d86a36d3
1 changed files with 23 additions and 15 deletions
38
__init__.py
38
__init__.py
|
@ -1,5 +1,6 @@
|
||||||
from st3m.application import Application, ApplicationContext
|
from st3m.application import Application, ApplicationContext
|
||||||
from st3m.run import run_view
|
from st3m.run import run_view
|
||||||
|
import leds
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
UP = 0
|
UP = 0
|
||||||
|
@ -37,7 +38,7 @@ class Quox(Application):
|
||||||
|
|
||||||
# start at top and go clockwise, 100px from center, 36° apart
|
# start at top and go clockwise, 100px from center, 36° apart
|
||||||
# y = 100 cos θ; x = 100 sin θ
|
# y = 100 cos θ; x = 100 sin θ
|
||||||
CHEST_COORDS = \
|
BOX_COORDS = \
|
||||||
[(0, -100), (59, -81), (95, -31), (95, 31), (59, 81),
|
[(0, -100), (59, -81), (95, -31), (95, 31), (59, 81),
|
||||||
(0, 100), (-59, 81), (-95, 31), (-95, -31), (-59, -81)]
|
(0, 100), (-59, 81), (-95, 31), (-95, -31), (-59, -81)]
|
||||||
|
|
||||||
|
@ -53,7 +54,6 @@ class Quox(Application):
|
||||||
self.random = Random()
|
self.random = Random()
|
||||||
|
|
||||||
self.boxes = []
|
self.boxes = []
|
||||||
self.reached_boxes = []
|
|
||||||
self.x = 0
|
self.x = 0
|
||||||
self.y = 0
|
self.y = 0
|
||||||
|
|
||||||
|
@ -64,6 +64,10 @@ class Quox(Application):
|
||||||
self.new_direction()
|
self.new_direction()
|
||||||
self.new_turn_delay()
|
self.new_turn_delay()
|
||||||
|
|
||||||
|
leds.set_gamma(2, 2, 2)
|
||||||
|
leds.set_auto_update(False)
|
||||||
|
leds.set_slew_rate(255)
|
||||||
|
|
||||||
def adjusted_x(self):
|
def adjusted_x(self):
|
||||||
if self.direction == UP or self.direction == DOWN:
|
if self.direction == UP or self.direction == DOWN:
|
||||||
return self.x - 32
|
return self.x - 32
|
||||||
|
@ -80,12 +84,6 @@ class Quox(Application):
|
||||||
self.clear(ctx)
|
self.clear(ctx)
|
||||||
# self.text(ctx)
|
# self.text(ctx)
|
||||||
self.quox_sprite(ctx)
|
self.quox_sprite(ctx)
|
||||||
self.draw_boxes(ctx)
|
|
||||||
|
|
||||||
def draw_boxes(self, ctx):
|
|
||||||
for i, j in Quox.CHEST_COORDS:
|
|
||||||
if (i, j) in self.boxes:
|
|
||||||
ctx.rgb(.3, 1, .85).rectangle(i-4, j-4, 8, 8).fill()
|
|
||||||
|
|
||||||
def clear(self, ctx):
|
def clear(self, ctx):
|
||||||
ctx.rgb(0, 0, 0) \
|
ctx.rgb(0, 0, 0) \
|
||||||
|
@ -120,18 +118,17 @@ class Quox(Application):
|
||||||
self.pick_direction()
|
self.pick_direction()
|
||||||
self.pick_step()
|
self.pick_step()
|
||||||
self.maybe_move()
|
self.maybe_move()
|
||||||
|
self.update_leds()
|
||||||
|
|
||||||
def add_boxes(self, state):
|
def add_boxes(self, state):
|
||||||
for i, button in enumerate(state.captouch.petals):
|
for i, button in enumerate(state.captouch.petals):
|
||||||
box = Quox.CHEST_COORDS[i]
|
box = Quox.BOX_COORDS[i]
|
||||||
if button.pressed:
|
if button.pressed:
|
||||||
if box not in self.boxes:
|
if box not in self.boxes:
|
||||||
self.boxes.append(box)
|
self.boxes.append(box)
|
||||||
else:
|
else:
|
||||||
if box in self.boxes:
|
if box in self.boxes:
|
||||||
self.boxes.remove(box)
|
self.boxes.remove(box)
|
||||||
if box in self.reached_boxes:
|
|
||||||
self.reached_boxes.remove(box)
|
|
||||||
|
|
||||||
def update_delay(self, Δ):
|
def update_delay(self, Δ):
|
||||||
self.turn_delay -= Δ
|
self.turn_delay -= Δ
|
||||||
|
@ -193,10 +190,10 @@ class Quox(Application):
|
||||||
self.x += 5
|
self.x += 5
|
||||||
|
|
||||||
def target(self):
|
def target(self):
|
||||||
for box in self.boxes:
|
if self.boxes:
|
||||||
if box not in self.reached_boxes:
|
return self.boxes[0]
|
||||||
return box
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def towards(self, direction, point):
|
def towards(self, direction, point):
|
||||||
if not point:
|
if not point:
|
||||||
|
@ -215,5 +212,16 @@ class Quox(Application):
|
||||||
def towards_target(self):
|
def towards_target(self):
|
||||||
return self.towards(self.direction, self.target())
|
return self.towards(self.direction, self.target())
|
||||||
|
|
||||||
|
def update_leds(self):
|
||||||
|
leds.set_all_rgb(0, 0, 0)
|
||||||
|
for i, box in enumerate(Quox.BOX_COORDS):
|
||||||
|
if box in self.boxes:
|
||||||
|
center = i * 4
|
||||||
|
left = (center - 1) % 40
|
||||||
|
right = (center + 1) % 40
|
||||||
|
for led in [left, right, center]:
|
||||||
|
leds.set_hsv(led, ((i + 4) * 36) % 360, 1, 1)
|
||||||
|
leds.update()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run_view(Quox(ApplicationContext()))
|
run_view(Quox(ApplicationContext()))
|
||||||
|
|
Loading…
Reference in a new issue