Compare commits
5 Commits
0db170c269
...
abeccd8c5e
Author | SHA1 | Date |
---|---|---|
rhiannon morris | abeccd8c5e | |
rhiannon morris | ef90ca2e27 | |
rhiannon morris | 63491e0227 | |
rhiannon morris | 1a635a3140 | |
rhiannon morris | 0b245a6f86 |
142
__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
|
||||
|
@ -7,20 +8,6 @@ LEFT = 1
|
|||
DOWN = 2
|
||||
RIGHT = 3
|
||||
|
||||
def dir_str(dir):
|
||||
if dir == UP:
|
||||
return 'back'
|
||||
elif dir == LEFT:
|
||||
return 'left'
|
||||
elif dir == DOWN:
|
||||
return 'front'
|
||||
elif dir == RIGHT:
|
||||
return 'right'
|
||||
|
||||
def image_path(direction, step):
|
||||
dir = os.path.dirname(__file__)
|
||||
return f"{dir}/sprites/{dir_str(direction)}{str(step + 1)}.png"
|
||||
|
||||
|
||||
class Random:
|
||||
def __init__(self):
|
||||
|
@ -49,31 +36,60 @@ class Quox(Application):
|
|||
LEFT = -90
|
||||
RIGHT = 70
|
||||
|
||||
# start at top and go clockwise, 100px from center, 36° apart
|
||||
# y = 100 cos θ; x = 100 sin θ
|
||||
BOX_COORDS = \
|
||||
[(0, -100), (59, -81), (95, -31), (95, 31), (59, 81),
|
||||
(0, 100), (-59, 81), (-95, 31), (-95, -31), (-59, -81)]
|
||||
|
||||
@staticmethod
|
||||
def image_path(direction, step):
|
||||
dir = os.path.dirname(__file__)
|
||||
bases = {UP: 'back', LEFT: 'left', DOWN: 'front', RIGHT: 'right'}
|
||||
base = bases[direction]
|
||||
return f"{dir}/sprites/{base}{step}.png"
|
||||
|
||||
def __init__(self, app_ctx):
|
||||
super().__init__(app_ctx)
|
||||
|
||||
self.random = Random()
|
||||
self.choose_direction()
|
||||
self.choose_turn_delay()
|
||||
|
||||
self.boxes = []
|
||||
self.x = 0
|
||||
self.y = 0
|
||||
|
||||
self.step = 0
|
||||
self.step_delay = Quox.STEP_DELAY
|
||||
self.move_delay = Quox.MOVE_DELAY
|
||||
self.x = -10
|
||||
self.y = -10
|
||||
|
||||
self.new_direction()
|
||||
self.new_turn_delay()
|
||||
|
||||
def adjusted_x(self):
|
||||
if self.direction == UP or self.direction == DOWN:
|
||||
return self.x - 32
|
||||
else:
|
||||
return self.x - 44
|
||||
|
||||
def adjusted_y(self):
|
||||
if self.direction == UP or self.direction == DOWN:
|
||||
return self.y - 30
|
||||
else:
|
||||
return self.y - 28
|
||||
|
||||
def draw(self, ctx):
|
||||
self.clear(ctx)
|
||||
# self.text(ctx)
|
||||
self.image(ctx)
|
||||
self.quox_sprite(ctx)
|
||||
self.update_leds()
|
||||
|
||||
def clear(self, ctx):
|
||||
ctx.rgb(0, 0, 0) \
|
||||
.rectangle(-120, -120, 240, 240) \
|
||||
.fill()
|
||||
|
||||
def image(self, ctx):
|
||||
path = image_path(self.direction, self.step)
|
||||
ctx.image(path, self.x, self.y, -1, -1)
|
||||
def quox_sprite(self, ctx):
|
||||
path = Quox.image_path(self.direction, self.step)
|
||||
ctx.image(path, self.adjusted_x(), self.adjusted_y(), -1, -1)
|
||||
|
||||
@staticmethod
|
||||
def coord(d):
|
||||
|
@ -86,15 +102,8 @@ class Quox(Application):
|
|||
elif d == DOWN:
|
||||
return (20, 100)
|
||||
|
||||
def rect(self, ctx):
|
||||
if self.step:
|
||||
ctx = ctx.rgb(.1, .6, .3)
|
||||
else:
|
||||
ctx = ctx.rgb(.1, .3, .6)
|
||||
ctx.rectangle(0, 0, *Quox.coord(self.direction)).fill()
|
||||
|
||||
def text(self, ctx):
|
||||
msg = f"{self.x}, {self.y}"
|
||||
msg = f"{self.target()}"
|
||||
ctx.font_size = 16.0
|
||||
ctx.text_align = ctx.CENTER
|
||||
ctx.rgb(1, 1, 1).move_to(0, -100).text(msg)
|
||||
|
@ -102,26 +111,49 @@ class Quox(Application):
|
|||
def think(self, state, Δ):
|
||||
super().think(state, Δ)
|
||||
self.update_delay(Δ)
|
||||
self.add_boxes(state)
|
||||
self.pick_direction()
|
||||
self.pick_step()
|
||||
self.maybe_move()
|
||||
|
||||
def add_boxes(self, state):
|
||||
for i, button in enumerate(state.captouch.petals):
|
||||
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)
|
||||
|
||||
def update_delay(self, Δ):
|
||||
self.turn_delay -= Δ
|
||||
self.step_delay -= Δ
|
||||
self.move_delay -= Δ
|
||||
if self.turn_delay <= 0:
|
||||
old_delay = self.turn_delay
|
||||
self.choose_direction()
|
||||
self.choose_turn_delay(old_delay)
|
||||
|
||||
def pick_direction(self):
|
||||
if self.turn_delay <= 0 or not self.towards_target():
|
||||
old_delay = min(self.turn_delay, 0)
|
||||
self.new_direction()
|
||||
self.new_turn_delay(old_delay)
|
||||
|
||||
def pick_step(self):
|
||||
if self.step_delay <= 0:
|
||||
self.step_delay = Quox.STEP_DELAY
|
||||
self.step = (self.step + 1) & 3
|
||||
|
||||
def maybe_move(self):
|
||||
if self.move_delay <= 0:
|
||||
self.move_delay += Quox.MOVE_DELAY
|
||||
self.move()
|
||||
|
||||
def choose_direction(self):
|
||||
self.direction = self.random.direction()
|
||||
def new_direction(self):
|
||||
d = self.random.direction()
|
||||
while not self.towards(d, self.target()):
|
||||
d = self.random.direction()
|
||||
self.direction = d
|
||||
|
||||
def choose_turn_delay(self, subtract=0):
|
||||
def new_turn_delay(self, subtract=0):
|
||||
self.turn_delay = self.random.delay() + subtract
|
||||
|
||||
def at_edge(self):
|
||||
|
@ -153,5 +185,39 @@ class Quox(Application):
|
|||
elif self.direction == RIGHT:
|
||||
self.x += 5
|
||||
|
||||
def target(self):
|
||||
if self.boxes:
|
||||
return self.boxes[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
def towards(self, direction, point):
|
||||
if not point:
|
||||
return True
|
||||
else:
|
||||
x, y = point
|
||||
if direction == UP:
|
||||
return self.y > y
|
||||
elif direction == LEFT:
|
||||
return self.x > x
|
||||
elif direction == RIGHT:
|
||||
return self.x < x
|
||||
elif direction == DOWN:
|
||||
return self.y < y
|
||||
|
||||
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 * 36, 1, 1)
|
||||
leds.update()
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_view(Quox(ApplicationContext()))
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[app]
|
||||
name = "stompy quox"
|
||||
menu = "Apps"
|
||||
menu = "Badge"
|
||||
|
||||
[entry]
|
||||
class = "Quox"
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
[Dolphin]
|
||||
Timestamp=2023,8,16,15,52,48
|
||||
Version=4
|
After Width: | Height: | Size: 833 B |
Before Width: | Height: | Size: 833 B After Width: | Height: | Size: 823 B |
Before Width: | Height: | Size: 823 B After Width: | Height: | Size: 804 B |
Before Width: | Height: | Size: 804 B After Width: | Height: | Size: 803 B |
Before Width: | Height: | Size: 803 B |
After Width: | Height: | Size: 809 B |
Before Width: | Height: | Size: 809 B After Width: | Height: | Size: 790 B |
Before Width: | Height: | Size: 790 B After Width: | Height: | Size: 790 B |
Before Width: | Height: | Size: 790 B After Width: | Height: | Size: 814 B |
Before Width: | Height: | Size: 814 B |
After Width: | Height: | Size: 873 B |
Before Width: | Height: | Size: 873 B After Width: | Height: | Size: 868 B |
Before Width: | Height: | Size: 868 B After Width: | Height: | Size: 873 B |
Before Width: | Height: | Size: 873 B After Width: | Height: | Size: 858 B |
Before Width: | Height: | Size: 858 B |
After Width: | Height: | Size: 952 B |
Before Width: | Height: | Size: 952 B After Width: | Height: | Size: 952 B |
Before Width: | Height: | Size: 952 B After Width: | Height: | Size: 952 B |
Before Width: | Height: | Size: 952 B After Width: | Height: | Size: 937 B |
Before Width: | Height: | Size: 937 B |