Ejemplo n.º 1
0
def translate(filename: str,
              *,
              sleep_secs: float = .5,
              in_terminal=False,
              size=RC(16, 16),
              split=True,
              dir: Dir = Dir.right,
              n_iterations: int = None):
    """move right"""
    cm = ColorMatrix.from_filename(filename)
    color_map = _get_color_replacements(filename)
    if split:
        cm = cm.split()[0]

    mult = 1 if dir is Dir.right else -1

    def _gen_offset():
        its = count() if n_iterations is None else range(n_iterations)
        for _ in its:
            for _c_offset in range(cm.width - size.c):
                yield mult * (cm.width - _c_offset - 1)

    for c_offset in _gen_offset():
        cm.replace(color_map)
        cm.wrap = True
        set_cm(cm, offset=RC(0, c_offset), size=size, in_terminal=in_terminal)
        sleep(sleep_secs)
Ejemplo n.º 2
0
def translate(filename: CFBI,
              *,
              sleep_secs: float = .5,
              in_terminal=False,
              size=RC(16, 16),
              split=True,
              dir: Dir = Dir.right,
              n_iterations: int = None,
              strip=False,
              pixels_per_step=1):
    """move window over image

    `n_iterations` represents how many full iterations of the message itself"""
    cm, color_map = _init_cm(filename)
    if split:
        cm = cm.split()[0]

    mult = 1 if dir is Dir.right else -1

    def _gen_offset():
        its = count() if n_iterations is None else range(n_iterations)
        for _ in its:
            for _c_offset in range(0, cm.width - size.c, pixels_per_step):
                yield mult * (cm.width - _c_offset - 1)

    for c_offset in _gen_offset():
        cm.replace(color_map)
        cm.wrap = True
        set_cm(cm,
               offset=RC(0, c_offset),
               size=size,
               in_terminal=in_terminal,
               strip=strip)
        sleep(sleep_secs)
Ejemplo n.º 3
0
def animate(filename: CFBI,
            *,
            center: bool = False,
            sleep_secs: float = .75,
            in_terminal=False,
            size=RC(16, 16),
            strip=True,
            how_long_secs=30):
    """split color matrix and change images every `sleep_secs` seconds"""
    cm, color_map = _init_cm(filename)
    end_time = time.time() + how_long_secs
    splits = cm.split()
    shuffle(splits)
    for cm in cycle(splits):
        log.info('.')
        c_offset = 0 if not center else max(0, ceil(cm.width / 2 - 8))
        cm.replace(color_map)
        try:
            set_cm(cm,
                   offset=RC(0, c_offset),
                   size=size,
                   in_terminal=in_terminal,
                   strip=strip)
        except ValueError:
            continue
        sleep(max(0, min(sleep_secs, end_time - time.time())))
        if time.time() >= end_time:
            break
Ejemplo n.º 4
0
 def _init_blocks(cls, shape, block_size) -> Dict[RC, Block]:
     res = {}
     for rc in RC(0, 0).to(RC(shape.r // 3, shape.c), col_inc=block_size):
         block = Block(_to_frozenset(rc, block_size),
                       _colors[rc.r % len(_colors)])
         for v in block.pos:
             res[v] = block
     return res
Ejemplo n.º 5
0
 def _paddle_dir(self) -> RC:
     res = None
     if min(self._paddle.pos).c == 0:
         res = RC(0, 1)
     if max(self._paddle.pos).c == self._shape.c - PADDLE_SIZE:
         res = RC(0, -1)
     if not res:
         res = random.choice([RC(0, -1), RC(0, 1)])
     print(res)
     return res
Ejemplo n.º 6
0
def quadrant(rc: RC, iteration: int):
    res = 0
    if rc < RC(8, 8):
        res = 0
    elif rc < RC(16, 8):
        res = 1
    elif rc < RC(8, 16):
        res = 2
    elif rc < RC(16, 16):
        res = 3
    return res + iteration
Ejemplo n.º 7
0
    def _handle_collisions(self):
        """collide against adjacent blocks/wall if necessary

        return next ball position"""
        bd = self._ball.dir

        # check row and column
        next_dir = self._process_ball(RC(bd.r, 0)) + self._process_ball(
            RC(0, bd.c))

        if next_dir == bd:
            # check diagonal
            next_dir = self._process_ball(bd)

        self._ball.dir = next_dir
        self._ball.pos += next_dir
Ejemplo n.º 8
0
 def _process_ball(self, offset) -> RC:
     next_pos = self._ball.pos + offset
     if next_pos in self._blocks:
         offset = -offset
         block = self._blocks[next_pos]
         if not block.on_collision():
             self._remove_block(block)
     elif not next_pos.in_bounds(RC(0, 0), self._shape):
         if next_pos.r >= self._shape.r:
             raise YouLose('nice try, loser :)')
         offset = -offset
     elif next_pos in self._paddle.pos:
         if next_pos == min(self._paddle.pos):
             return RC(-1, -1)
         elif next_pos == max(self._paddle.pos):
             return RC(1, -1)
     return offset
Ejemplo n.º 9
0
def animate(filename: str,
            *,
            center: bool = False,
            sleep_secs: float = .75,
            in_terminal=False,
            size=RC(16, 16),
            strip=True):
    """split color matrix and change images every `sleep_secs` seconds"""
    cm = ColorMatrix.from_filename(filename)
    color_map = _get_color_replacements(filename)
    for i, cm in enumerate(cycle(cm.split())):
        log.info('.')
        c_offset = 0 if not center else max(0, ceil(cm.width / 2 - 8))
        cm.replace(color_map)
        set_cm(cm,
               offset=RC(0, c_offset),
               size=size,
               in_terminal=in_terminal,
               strip=strip)
        sleep(sleep_secs)
Ejemplo n.º 10
0
def set_cm(cm: ColorMatrix,
           offset=RC(0, 0),
           size=RC(16, 16),
           *,
           in_terminal=False,
           with_mini=True,
           strip=True,
           verbose=True,
           duration_msec=0):
    """set color matrix either in terminal or on lights"""
    if strip:
        cm = cm.strip()

    cm = cm.get_range(RC(0, 0) + offset, size + offset)

    if in_terminal:
        print(cm.color_str)
        if verbose:
            print(cm.describe)
            print(cm.resize().color_str)
            print(cm.resize((4, 4)).color_str)
        return

    cm.set_max_brightness_pct(60)
    cm.replace({default_color: Color(1, 1, 100, 9000)})
    tiles = cm.to_tiles()

    idx_colors_map = {}
    for t_idx, t_cm in tiles.items():
        t_info = tile_map[t_idx]
        idx_colors_map[t_info.idx] = t_cm.flattened

    if with_mini:
        ti = tile_map[RC(2, -1)]
        idx_colors_map[ti.idx] = cm.resize(
            (8, 8)).rotate_from_origin(ti.origin).flattened

    tc = get_tile_chain()
    tc.set_tilechain_colors(idx_colors_map, duration=duration_msec)
Ejemplo n.º 11
0
 def cm(self) -> ColorMatrix:
     colors = [self._get_color(rc) for rc in RC(0, 0).to(self._shape)]
     return ColorMatrix.from_colors(colors, self._shape)
Ejemplo n.º 12
0
 def _init_ball_and_paddle(cls, shape) -> Tuple[Ball, Paddle]:
     start = RC(shape.r - 1, random.randrange(shape.c - PADDLE_SIZE))
     ball = Ball(start - RC(1, 0))
     return ball, Paddle(_to_frozenset(start, PADDLE_SIZE))
Ejemplo n.º 13
0
 def __init__(self, shape=tile_shape, *, block_size=BLOCK_SIZE):
     self._shape = shape
     self._blocks = self._init_blocks(self._shape, block_size)
     self._ball, self._paddle = self._init_ball_and_paddle(self._shape)
     self._cur_paddle_dir = RC(0, -1)
Ejemplo n.º 14
0
def _to_frozenset(rc: RC, size=1):
    return frozenset(rc + RC(0, n) for n in range(size))
Ejemplo n.º 15
0
class Ball:
    pos: RC
    dir: RC = RC(-1, -1)
    color: Color = Colors.WHITE
Ejemplo n.º 16
0
import random
import time
from dataclasses import dataclass
from typing import FrozenSet, Set, Dict, Union, Tuple

from lifxlan3 import Color, Colors
from lifxlan3.routines.tile.core import set_cm
from lifxlan3.routines.tile.tile_utils import ColorMatrix, RC, default_color

__author__ = 'acushner'

_colors = Colors.GREEN, Colors.PINK, Colors.BLUE, Colors.ORANGE, Colors.RED

tile_shape = RC(16, 16)
PADDLE_SIZE = 2
BLOCK_SIZE = 2


@dataclass
class Paddle:
    pos: FrozenSet[RC]
    color: Color = Colors.SNES_LIGHT_PURPLE

    def move(self, dir: RC):
        start = min(self.pos)
        start += dir
        print('before', self.pos)
        self.pos = _to_frozenset(start, PADDLE_SIZE)
        print('after', self.pos)