diff --git a/main.py b/main.py index 8dc4d79..3c30043 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,19 @@ 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(dir, step): + return f"/flash/sprites/{dir_str(dir)}{str(step + 1)}.png" + class Random: def __init__(self): @@ -25,41 +38,8 @@ class Random: # 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 + STEP_DELAY = 250 TURN_DELAY_BASE = 1400 def __init__(self): @@ -71,14 +51,18 @@ class Box(Responder): def draw(self, ctx): self.clear(ctx) - self.rect(ctx) # self.text(ctx) + self.image(ctx) 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, -10, -10, -1, -1) + @staticmethod def coord(d): if d == UP: @@ -98,8 +82,10 @@ class Box(Responder): 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)) + msg = f"{self.turn_delay}; {self.step_delay}; {self.direction}; {self.step}" + ctx.font_size = 16.0 + ctx.text_align = ctx.CENTER + ctx.rgb(1, 1, 1).move_to(0, -100).text(msg) def think(self, state, Δ): self.update_delay(Δ) @@ -113,7 +99,7 @@ class Box(Responder): self.choose_turn_delay(old_delay) if self.step_delay <= 0: self.step_delay = Box.STEP_DELAY - self.step = (self.step + 1) & 4 + self.step = (self.step + 1) & 3 def choose_direction(self): self.direction = self.random.direction() diff --git a/sprites/back1.png b/sprites/back1.png new file mode 100644 index 0000000..fafd48e Binary files /dev/null and b/sprites/back1.png differ diff --git a/sprites/back2.png b/sprites/back2.png new file mode 100644 index 0000000..9884f11 Binary files /dev/null and b/sprites/back2.png differ diff --git a/sprites/back3.png b/sprites/back3.png new file mode 100644 index 0000000..02d123c Binary files /dev/null and b/sprites/back3.png differ diff --git a/sprites/back4.png b/sprites/back4.png new file mode 100644 index 0000000..a33e060 Binary files /dev/null and b/sprites/back4.png differ diff --git a/sprites/front1.png b/sprites/front1.png new file mode 100644 index 0000000..ad0dad1 Binary files /dev/null and b/sprites/front1.png differ diff --git a/sprites/front2.png b/sprites/front2.png new file mode 100644 index 0000000..b2b5338 Binary files /dev/null and b/sprites/front2.png differ diff --git a/sprites/front3.png b/sprites/front3.png new file mode 100644 index 0000000..ff69054 Binary files /dev/null and b/sprites/front3.png differ diff --git a/sprites/front4.png b/sprites/front4.png new file mode 100644 index 0000000..a87eaa6 Binary files /dev/null and b/sprites/front4.png differ diff --git a/sprites/left1.png b/sprites/left1.png new file mode 100644 index 0000000..191a0ce Binary files /dev/null and b/sprites/left1.png differ diff --git a/sprites/left2.png b/sprites/left2.png new file mode 100644 index 0000000..f8267a0 Binary files /dev/null and b/sprites/left2.png differ diff --git a/sprites/left3.png b/sprites/left3.png new file mode 100644 index 0000000..191a0ce Binary files /dev/null and b/sprites/left3.png differ diff --git a/sprites/left4.png b/sprites/left4.png new file mode 100644 index 0000000..fa005c8 Binary files /dev/null and b/sprites/left4.png differ diff --git a/sprites/right1.png b/sprites/right1.png new file mode 100644 index 0000000..470a7f7 Binary files /dev/null and b/sprites/right1.png differ diff --git a/sprites/right2.png b/sprites/right2.png new file mode 100644 index 0000000..c0cccc3 Binary files /dev/null and b/sprites/right2.png differ diff --git a/sprites/right3.png b/sprites/right3.png new file mode 100644 index 0000000..a462f82 Binary files /dev/null and b/sprites/right3.png differ diff --git a/sprites/right4.png b/sprites/right4.png new file mode 100644 index 0000000..92e0840 Binary files /dev/null and b/sprites/right4.png differ