qflow3r/__init__.py

153 lines
4.1 KiB
Python
Raw Normal View History

2023-08-16 14:52:56 -04:00
from st3m.application import Application, ApplicationContext
from st3m.run import run_view
import os.path
2023-08-16 09:45:20 -04:00
UP = 0
LEFT = 1
DOWN = 2
RIGHT = 3
class Random:
def __init__(self):
self._state = 0x9ec5_9ec5 # (two gecs)
# idk its fine its just for choosing delay and direction
def next(self):
self._state *= 37
self._state &= 0xffff_ffff
return self._state >> 24
def direction(self):
return self.next() & 3
def delay(self):
2023-08-16 14:52:56 -04:00
return Quox.TURN_DELAY_BASE + self.next()
2023-08-16 09:45:20 -04:00
# screen coords range ±120 each side
2023-08-16 14:52:56 -04:00
class Quox(Application):
2023-08-16 14:27:13 -04:00
STEP_DELAY = 250
2023-08-16 09:45:20 -04:00
TURN_DELAY_BASE = 1400
2023-08-16 14:27:13 -04:00
MOVE_DELAY = 100
TOP = -90
BOTTOM = 70 # so its always a bit visible
LEFT = -90
RIGHT = 70
2023-08-16 09:45:20 -04:00
2023-08-17 05:16:05 -04:00
@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"
2023-08-16 14:52:56 -04:00
def __init__(self, app_ctx):
super().__init__(app_ctx)
2023-08-16 09:45:20 -04:00
self.random = Random()
2023-08-17 05:16:05 -04:00
2023-08-16 09:45:20 -04:00
self.choose_direction()
self.choose_turn_delay()
2023-08-17 05:16:05 -04:00
2023-08-16 09:45:20 -04:00
self.step = 0
2023-08-16 14:52:56 -04:00
self.step_delay = Quox.STEP_DELAY
self.move_delay = Quox.MOVE_DELAY
2023-08-17 05:16:05 -04:00
2023-08-16 14:27:13 -04:00
self.x = -10
self.y = -10
2023-08-16 09:45:20 -04:00
def draw(self, ctx):
self.clear(ctx)
# self.text(ctx)
2023-08-16 13:51:55 -04:00
self.image(ctx)
2023-08-16 09:45:20 -04:00
def clear(self, ctx):
ctx.rgb(0, 0, 0) \
.rectangle(-120, -120, 240, 240) \
.fill()
2023-08-16 13:51:55 -04:00
def image(self, ctx):
2023-08-17 05:16:05 -04:00
path = Quox.image_path(self.direction, self.step)
2023-08-16 14:27:13 -04:00
ctx.image(path, self.x, self.y, -1, -1)
2023-08-16 13:51:55 -04:00
2023-08-16 09:45:20 -04:00
@staticmethod
def coord(d):
if d == UP:
return (-20, -100)
elif d == LEFT:
return (-100, -20)
elif d == RIGHT:
return (100, 20)
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)
2023-08-16 14:52:56 -04:00
ctx.rectangle(0, 0, *Quox.coord(self.direction)).fill()
2023-08-16 09:45:20 -04:00
def text(self, ctx):
2023-08-16 14:27:13 -04:00
msg = f"{self.x}, {self.y}"
2023-08-16 13:51:55 -04:00
ctx.font_size = 16.0
ctx.text_align = ctx.CENTER
ctx.rgb(1, 1, 1).move_to(0, -100).text(msg)
2023-08-16 09:45:20 -04:00
def think(self, state, Δ):
2023-08-16 14:52:56 -04:00
super().think(state, Δ)
2023-08-16 09:45:20 -04:00
self.update_delay(Δ)
def update_delay(self, Δ):
self.turn_delay -= Δ
self.step_delay -= Δ
2023-08-16 14:27:13 -04:00
self.move_delay -= Δ
2023-08-16 09:45:20 -04:00
if self.turn_delay <= 0:
old_delay = self.turn_delay
self.choose_direction()
self.choose_turn_delay(old_delay)
if self.step_delay <= 0:
2023-08-16 14:52:56 -04:00
self.step_delay = Quox.STEP_DELAY
2023-08-16 13:51:55 -04:00
self.step = (self.step + 1) & 3
2023-08-16 14:27:13 -04:00
if self.move_delay <= 0:
2023-08-16 14:52:56 -04:00
self.move_delay += Quox.MOVE_DELAY
2023-08-16 14:27:13 -04:00
self.move()
2023-08-16 09:45:20 -04:00
def choose_direction(self):
self.direction = self.random.direction()
def choose_turn_delay(self, subtract=0):
2023-08-16 14:27:13 -04:00
self.turn_delay = self.random.delay() + subtract
def at_edge(self):
2023-08-16 14:52:56 -04:00
return ((self.direction == UP and self.y <= Quox.TOP) or
(self.direction == DOWN and self.y >= Quox.BOTTOM) or
(self.direction == LEFT and self.x <= Quox.LEFT) or
(self.direction == RIGHT and self.x >= Quox.RIGHT))
2023-08-16 14:27:13 -04:00
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
2023-08-16 09:45:20 -04:00
2023-08-16 14:52:56 -04:00
if __name__ == '__main__':
run_view(Quox(ApplicationContext()))