this is amidar movement

This commit is contained in:
rhiannon morris 2023-08-16 20:27:13 +02:00
parent 5925a4c857
commit 2a44d4e25b

49
main.py
View file

@ -39,8 +39,13 @@ class Random:
# screen coords range ±120 each side
class Box(Responder):
STEP_DELAY = 250
STEP_DELAY = 250
TURN_DELAY_BASE = 1400
MOVE_DELAY = 100
TOP = -90
BOTTOM = 70 # so its always a bit visible
LEFT = -90
RIGHT = 70
def __init__(self):
self.random = Random()
@ -48,6 +53,9 @@ class Box(Responder):
self.choose_turn_delay()
self.step = 0
self.step_delay = Box.STEP_DELAY
self.move_delay = Box.MOVE_DELAY
self.x = -10
self.y = -10
def draw(self, ctx):
self.clear(ctx)
@ -61,7 +69,7 @@ class Box(Responder):
def image(self, ctx):
path = image_path(self.direction, self.step)
ctx.image(path, -10, -10, -1, -1)
ctx.image(path, self.x, self.y, -1, -1)
@staticmethod
def coord(d):
@ -82,7 +90,7 @@ class Box(Responder):
ctx.rectangle(0, 0, *Box.coord(self.direction)).fill()
def text(self, ctx):
msg = f"{self.turn_delay}; {self.step_delay}; {self.direction}; {self.step}"
msg = f"{self.x}, {self.y}"
ctx.font_size = 16.0
ctx.text_align = ctx.CENTER
ctx.rgb(1, 1, 1).move_to(0, -100).text(msg)
@ -93,6 +101,7 @@ class Box(Responder):
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()
@ -100,11 +109,43 @@ class Box(Responder):
if self.step_delay <= 0:
self.step_delay = Box.STEP_DELAY
self.step = (self.step + 1) & 3
if self.move_delay <= 0:
self.move_delay += Box.MOVE_DELAY
self.move()
def choose_direction(self):
self.direction = self.random.direction()
def choose_turn_delay(self, subtract=0):
self.turn_delay = self.random.delay() - subtract
self.turn_delay = self.random.delay() + subtract
def at_edge(self):
return ((self.direction == UP and self.y <= Box.TOP) or
(self.direction == DOWN and self.y >= Box.BOTTOM) or
(self.direction == LEFT and self.x <= Box.LEFT) or
(self.direction == RIGHT and self.x >= Box.RIGHT))
def flip_direction(self):
if self.direction == UP:
self.direction = DOWN
elif self.direction == LEFT:
self.direction = RIGHT
elif self.direction == DOWN:
self.direction = UP
elif self.direction == RIGHT:
self.direction = LEFT
def move(self):
if self.at_edge():
self.flip_direction()
if self.direction == UP:
self.y -= 5
elif self.direction == LEFT:
self.x -= 5
elif self.direction == DOWN:
self.y += 5
elif self.direction == RIGHT:
self.x += 5
run_responder(Box())