quox follows petal touches

This commit is contained in:
rhiannon morris 2023-08-17 21:04:00 +02:00
parent 63491e0227
commit ef90ca2e27

View file

@ -35,6 +35,12 @@ class Quox(Application):
LEFT = -90 LEFT = -90
RIGHT = 70 RIGHT = 70
# start at top and go clockwise, 100px from center, 36° apart
# y = 100 cos θ; x = 100 sin θ
CHEST_COORDS = \
[(0, -100), (59, -81), (95, -31), (95, 31), (59, 81),
(0, 100), (-59, 81), (-95, 31), (-95, -31), (-59, -81)]
@staticmethod @staticmethod
def image_path(direction, step): def image_path(direction, step):
dir = os.path.dirname(__file__) dir = os.path.dirname(__file__)
@ -46,29 +52,49 @@ class Quox(Application):
super().__init__(app_ctx) super().__init__(app_ctx)
self.random = Random() self.random = Random()
self.choose_direction() self.boxes = []
self.choose_turn_delay() self.reached_boxes = []
self.x = 0
self.y = 0
self.step = 0 self.step = 0
self.step_delay = Quox.STEP_DELAY self.step_delay = Quox.STEP_DELAY
self.move_delay = Quox.MOVE_DELAY self.move_delay = Quox.MOVE_DELAY
self.x = -10 self.new_direction()
self.y = -10 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): def draw(self, ctx):
self.clear(ctx) self.clear(ctx)
# self.text(ctx) # self.text(ctx)
self.image(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) \
.rectangle(-120, -120, 240, 240) \ .rectangle(-120, -120, 240, 240) \
.fill() .fill()
def image(self, ctx): def quox_sprite(self, ctx):
path = Quox.image_path(self.direction, self.step) path = Quox.image_path(self.direction, self.step)
ctx.image(path, self.x, self.y, -1, -1) ctx.image(path, self.adjusted_x(), self.adjusted_y(), -1, -1)
@staticmethod @staticmethod
def coord(d): def coord(d):
@ -81,15 +107,8 @@ class Quox(Application):
elif d == DOWN: elif d == DOWN:
return (20, 100) 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): def text(self, ctx):
msg = f"{self.x}, {self.y}" msg = f"{self.target()}"
ctx.font_size = 16.0 ctx.font_size = 16.0
ctx.text_align = ctx.CENTER ctx.text_align = ctx.CENTER
ctx.rgb(1, 1, 1).move_to(0, -100).text(msg) ctx.rgb(1, 1, 1).move_to(0, -100).text(msg)
@ -97,20 +116,33 @@ class Quox(Application):
def think(self, state, Δ): def think(self, state, Δ):
super().think(state, Δ) super().think(state, Δ)
self.update_delay(Δ) self.update_delay(Δ)
self.add_boxes(state)
self.pick_direction() self.pick_direction()
self.pick_step() self.pick_step()
self.maybe_move() self.maybe_move()
def add_boxes(self, state):
for i, button in enumerate(state.captouch.petals):
box = Quox.CHEST_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, Δ): def update_delay(self, Δ):
self.turn_delay -= Δ self.turn_delay -= Δ
self.step_delay -= Δ self.step_delay -= Δ
self.move_delay -= Δ self.move_delay -= Δ
def pick_direction(self): def pick_direction(self):
if self.turn_delay <= 0: if self.turn_delay <= 0 or not self.towards_target():
old_delay = self.turn_delay old_delay = min(self.turn_delay, 0)
self.choose_direction() self.new_direction()
self.choose_turn_delay(old_delay) self.new_turn_delay(old_delay)
def pick_step(self): def pick_step(self):
if self.step_delay <= 0: if self.step_delay <= 0:
@ -122,10 +154,13 @@ class Quox(Application):
self.move_delay += Quox.MOVE_DELAY self.move_delay += Quox.MOVE_DELAY
self.move() self.move()
def choose_direction(self): def new_direction(self):
self.direction = self.random.direction() 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 self.turn_delay = self.random.delay() + subtract
def at_edge(self): def at_edge(self):
@ -157,5 +192,28 @@ class Quox(Application):
elif self.direction == RIGHT: elif self.direction == RIGHT:
self.x += 5 self.x += 5
def target(self):
for box in self.boxes:
if box not in self.reached_boxes:
return box
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())
if __name__ == '__main__': if __name__ == '__main__':
run_view(Quox(ApplicationContext())) run_view(Quox(ApplicationContext()))