def propagate(cm: ColorMatrix, base: Color, explosion: Color): current = cm.find_all(explosion) offsets = RC(-1, -1), RC(1, 1), RC(-1, 1), RC(1, -1) for c_rc in current: for o in offsets: cm[c_rc + o] = explosion for c_rc in current: cm[c_rc] = base return cm
def for_talk(): g = AutoSnekGame( shape=RC(16, 16), tick_rate_secs=.05, callbacks=Callbacks(lights_tick, on_death, on_success, lights_intro), snek_color=Colors.GREEN, # snek_color=Themes.rainbow, background_color=Colors.SNES_DARK_GREY._replace(brightness=6554), food_color=Colors.YALE_BLUE) g.run()
def __init__(self, snek_color: Color = Colors.GREEN, background_color: ColorTheme = Colors.OFF, food_color: ColorTheme = Colors.YALE_BLUE, *, snek_growth_amount=2, shape=RC(16, 16), tick_rate_secs=2.0, callbacks: Callbacks = Callbacks()): self.board = self._init_board(background_color, shape) self._board_positions = set(RC(0, 0).to(shape)) self.snek = Snek(snek_color, shape) self.food_colors = _colors(food_color) self.tick_rate_secs = tick_rate_secs self.callbacks = callbacks self.snek_growth_amount = snek_growth_amount self._set_food(init=True) self._dir: Dir = None self._prev_dir: Dir = None
def _validate_pos(self, pos: RC) -> RC: """check if snek ran into self or wall; return pos""" if pos in self and not pos == self.sneque[0].pos: raise SnekDead('snek ran into self :(') if self.allow_wrap: return pos % self.board_shape if not RC(0, 0) <= pos < self.board_shape: raise SnekDead('snek ran off the board :(') return pos
def play(in_terminal=False): if in_terminal: g = SnekGame(shape=RC(16, 16), tick_rate_secs=.2, callbacks=Callbacks(terminal_tick, terminal_on_death, on_success), background_color=Colors.SNES_LIGHT_GREY, snek_color=Colors.COPILOT_BLUE_GREEN, food_color=Colors.SNES_LIGHT_PURPLE, snek_growth_amount=2) else: g = SnekGame( shape=RC(16, 16), tick_rate_secs=.2, callbacks=Callbacks(lights_tick, on_death, on_success, lights_intro), snek_color=Colors.GREEN, # snek_color=Themes.july_4th, background_color=Colors.SNES_DARK_GREY._replace(brightness=6554), food_color=Colors.YALE_BLUE) g.run()
def explode(base_color: Color = Colors.STEELERS_RED, explosion_color: Color = Colors.COLD_WHITE, in_terminal=False): colors = to_n_colors(base_color.r_brightness(20000), n=256) cm = ColorMatrix.from_colors(colors, RC(16, 16)) start, end = RC(7, 7), RC(9, 9) cm[start] = cm[end] = explosion_color # for rc in start.to(end): # cm[rc] = explosion_color # expand with suppress(IndexError): for _ in range(10): set_cm(cm, strip=False, in_terminal=in_terminal) propagate(cm, base_color, explosion_color) time.sleep(.1) if in_terminal: return # fill white for offset in range(8): s, e = start - RC(offset, offset), end + RC(offset, offset) for rc in s.to(e): cm[rc] = explosion_color set_cm(cm, strip=False, in_terminal=in_terminal) time.sleep(.1) # fade to black colors = to_n_colors(Colors.OFF, n=256) cm = ColorMatrix.from_colors(colors, RC(16, 16)) set_cm(cm, strip=False, duration_msec=3000)
def run_as_ambiance(): background = Colors.SNES_DARK_GREY._replace(brightness=6554) color_options = list(Colors) + list(Themes) tick_rate_secs = randrange(80) / 100.0 + .5 food_c, snek_c = (o[1] for o in sample(color_options, 2)) with suppress(SnekDead, SnekSucceeds): g = AutoSnekGame(shape=RC(16, 16), tick_rate_secs=tick_rate_secs, callbacks=Callbacks(lights_tick, on_death, on_success), food_color=food_c, snek_color=snek_c, background_color=background, snek_growth_amount=randrange(1, 7)) g.run()
def __main(): while True: with suppress(SnekDead, SnekSucceeds): run_as_ambiance() # return for_talk() # return play() g = AutoSnekGame(shape=RC(16, 16), tick_rate_secs=.05, callbacks=Callbacks(terminal_tick, terminal_on_death, on_success), background_color=Colors.SNES_LIGHT_GREY, snek_color=Colors.COPILOT_BLUE_GREEN, food_color=Colors.SNES_LIGHT_PURPLE, snek_growth_amount=2) # g = SnekGame(shape=RC(16, 16), tick_rate_secs=.05, # callbacks=Callbacks(lights_cb, on_death, on_success, lights_intro), # background_color=Colors.OFF, snek_color=Colors.GREEN) # g = AutoSnekGame(shape=RC(16, 16), tick_rate_secs=.05, # callbacks=Callbacks(lights_tick, on_death, on_success, lights_intro), # snek_color=Colors.GREEN, # # snek_color=Themes.july_4th, # background_color=Colors.SNES_DARK_GREY._replace(brightness=6554), # food_color=Colors.YALE_BLUE) g.run()
def _rand_point(shape: RC) -> RC: return RC(randint(0, shape.r - 1), randint(0, shape.c - 1))
import os import time from contextlib import suppress from itertools import cycle, chain from random import randint, choice, sample, randrange from threading import Thread from typing import NamedTuple, Deque, Dict, Set, Callable, Optional, Iterable from lifxlan3 import Color, deque, Dir, Colors, Themes from lifxlan3.routines import parse_keyboard_inputs, dir_map, ColorTheme, colors_to_theme from lifxlan3.routines.tile.core import set_cm, translate, RC, ColorMatrix from lifxlan3.routines.tile.tile_utils import to_n_colors, a_star dir_rc_map: Dict[Dir, RC] = { Dir.right: RC(0, 1), Dir.left: RC(0, -1), Dir.up: RC(-1, 0), Dir.down: RC(1, 0) } rc_dir_map: Dict[RC, Dir] = {v: k for k, v in dir_rc_map.items()} class Cell(NamedTuple): pos: RC color: Color def _rand_point(shape: RC) -> RC: return RC(randint(0, shape.r - 1), randint(0, shape.c - 1))