first
This commit is contained in:
commit
d928e2b09b
1 changed files with 124 additions and 0 deletions
124
main.py
Normal file
124
main.py
Normal file
|
@ -0,0 +1,124 @@
|
|||
from st3m.reactor import Responder
|
||||
from st3m.run import run_responder
|
||||
|
||||
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):
|
||||
return Box.TURN_DELAY_BASE + self.next()
|
||||
|
||||
# screen coords range ±120 each side
|
||||
|
||||
TRANSPARENT = (1, 0, 1)
|
||||
|
||||
def to_image(str):
|
||||
cols = {
|
||||
' ': TRANSPARENT,
|
||||
'#': (0, 0, 0),
|
||||
'%': (.6, .6, .6),
|
||||
'*': (.26, .26, .26),
|
||||
':': (0, .41, 0),
|
||||
"'": (.87, .87, .87),
|
||||
'.': (.87, .87, 0),
|
||||
}
|
||||
lines = str.strip().split('\n')
|
||||
lmap = lambda f, xs: list(map(f, xs))
|
||||
return lmap(lambda l: lmap(lambda c: cols[c], l), lines)
|
||||
|
||||
|
||||
quox_left_1 = to_image("""
|
||||
% *
|
||||
% % * %*% * :#
|
||||
%%%% %%% *%%*%%* ::%
|
||||
%%#%%% *%%*%%*%* :#
|
||||
%%'%%% *%%*%*%* :#
|
||||
%%%%%%: *%*%** ::%
|
||||
%% %%#:::*%**%*: :##
|
||||
%%::::::%*::::%
|
||||
##:::::::::::##
|
||||
%:::%#%:::%#%
|
||||
#*:::##*:::#
|
||||
##***::#***::###
|
||||
##.%%:##.%%:##
|
||||
""")
|
||||
|
||||
class Box(Responder):
|
||||
STEP_DELAY = 500
|
||||
TURN_DELAY_BASE = 1400
|
||||
|
||||
def __init__(self):
|
||||
self.random = Random()
|
||||
self.choose_direction()
|
||||
self.choose_turn_delay()
|
||||
self.step = 0
|
||||
self.step_delay = Box.STEP_DELAY
|
||||
|
||||
def draw(self, ctx):
|
||||
self.clear(ctx)
|
||||
self.rect(ctx)
|
||||
# self.text(ctx)
|
||||
|
||||
def clear(self, ctx):
|
||||
ctx.rgb(0, 0, 0) \
|
||||
.rectangle(-120, -120, 240, 240) \
|
||||
.fill()
|
||||
|
||||
@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)
|
||||
ctx.rectangle(0, 0, *Box.coord(self.direction)).fill()
|
||||
|
||||
def text(self, ctx):
|
||||
ctx.rgb(1, 1, 1) \
|
||||
.move_to(0, 0).text(str(self.delay) + '; ' + str(self.direction))
|
||||
|
||||
def think(self, state, Δ):
|
||||
self.update_delay(Δ)
|
||||
|
||||
def update_delay(self, Δ):
|
||||
self.turn_delay -= Δ
|
||||
self.step_delay -= Δ
|
||||
if self.turn_delay <= 0:
|
||||
old_delay = self.turn_delay
|
||||
self.choose_direction()
|
||||
self.choose_turn_delay(old_delay)
|
||||
if self.step_delay <= 0:
|
||||
self.step_delay = Box.STEP_DELAY
|
||||
self.step = (self.step + 1) & 4
|
||||
|
||||
def choose_direction(self):
|
||||
self.direction = self.random.direction()
|
||||
|
||||
def choose_turn_delay(self, subtract=0):
|
||||
self.turn_delay = self.random.delay() - subtract
|
||||
|
||||
run_responder(Box())
|
Loading…
Reference in a new issue