From 61d86a36d38ea97eb4322bbaefa7f8bea405da99 Mon Sep 17 00:00:00 2001 From: rhiannon morris Date: Thu, 17 Aug 2023 21:29:43 +0200 Subject: [PATCH] leds instead of on screen squares --- __init__.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/__init__.py b/__init__.py index 61c3ab3..6d19211 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,6 @@ from st3m.application import Application, ApplicationContext from st3m.run import run_view +import leds import os.path UP = 0 @@ -37,7 +38,7 @@ class Quox(Application): # start at top and go clockwise, 100px from center, 36° apart # 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)] @@ -53,7 +54,6 @@ class Quox(Application): self.random = Random() self.boxes = [] - self.reached_boxes = [] self.x = 0 self.y = 0 @@ -64,6 +64,10 @@ class Quox(Application): self.new_direction() self.new_turn_delay() + leds.set_gamma(2, 2, 2) + leds.set_auto_update(False) + leds.set_slew_rate(255) + def adjusted_x(self): if self.direction == UP or self.direction == DOWN: return self.x - 32 @@ -80,12 +84,6 @@ class Quox(Application): self.clear(ctx) # self.text(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): ctx.rgb(0, 0, 0) \ @@ -120,18 +118,17 @@ class Quox(Application): self.pick_direction() self.pick_step() self.maybe_move() + self.update_leds() def add_boxes(self, state): for i, button in enumerate(state.captouch.petals): - box = Quox.CHEST_COORDS[i] + box = Quox.BOX_COORDS[i] if button.pressed: if box not in self.boxes: self.boxes.append(box) else: if box in self.boxes: self.boxes.remove(box) - if box in self.reached_boxes: - self.reached_boxes.remove(box) def update_delay(self, Δ): self.turn_delay -= Δ @@ -193,10 +190,10 @@ class Quox(Application): self.x += 5 def target(self): - for box in self.boxes: - if box not in self.reached_boxes: - return box - return None + if self.boxes: + return self.boxes[0] + else: + return None def towards(self, direction, point): if not point: @@ -215,5 +212,16 @@ class Quox(Application): def towards_target(self): 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__': run_view(Quox(ApplicationContext()))