Beispiel #1
0
    def animation(self):
        animation_state = AnimationState(self.mode)
        animation_list = [FieldColorsAnimation(animation_state, self.board.field)]
        while not self.animation_thread.stopped() or animation_list[-1].is_blocking() or not self.queue.empty():
            if not animation_list[-1].is_blocking():
                try:
                    event = self.queue.get_nowait()
                    # some animations have to stop when a new one is started
                    animation_list[-1].new_animation_available()
                    if event[0] == PLAY:
                        super().player_plays(*event[1:3])
                        animation_list.append(PlayAnimation(animation_state))
                    elif event[0] == UNDO:
                        x = event[1]
                        y = event[2]
                        z = self.get_z(x, y)
                        c = self.board.next_color
                        animation_list.append(UndoAnimation(animation_state, x, y, z, c))
                    elif event[0] == SELECT:
                        x = event[1]
                        y = event[2]
                        z = self.get_z(x, y)
                        c = self.board.next_color
                        top = self.board.field(x, y, z) is not EMPTY
                        animation_list.append(SelectAnimation(animation_state, x, y, z, c, top))
                    elif event[0] == FINISH:
                        c = RED
                        if self.board.next_color == RED:
                            c = BLUE
                        animation_list.append(FinishAnimation(animation_state, event[1], c))
                except Empty:
                    pass

            cube_buffer = get_empty_cube_buffer()
            for a in animation_list:
                a.animate(cube_buffer)

            # remove completed animations from list
            animation_list[:] = [a for a in animation_list if not a.is_done()]

            # draw the completed cube
            self.cube.draw(cube_buffer)
            self.cube.show()

            animation_state.update()

            if not is_a_raspberry():
                sleep(0.01)
Beispiel #2
0
 def run(self):
     steps = 100
     try:
         while True:
             self.wheel_stop = self.get_random_wheel()
             for i in range(steps):
                 if self.button_events.get_event(block=False):
                     LOG.debug("button pressed, interrupting rainbow")
                     raise RandomInterrupted()
                 self.step(self.wheel_start, self.wheel_stop, float(i) / steps)
                 self.cube.draw(self.cube_buffer)
                 self.cube.show()
                 if not is_a_raspberry():
                     sleep(0.02)
             self.wheel_start = copy.deepcopy(self.wheel_stop)
     except RandomInterrupted:
         return
Beispiel #3
0
 def run(self) -> None:
     try:
         last_time = monotonic()
         self.apple.set_random_position(self.snake.snake)
         while True:
             self.handle_events()
             current_time = monotonic()
             if (current_time - last_time) > DELAY:
                 last_time = current_time
                 self.snake.move(self.apple)
             cube_buffer = get_empty_cube_buffer()
             cube_buffer = self.apple.draw(cube_buffer)
             cube_buffer = self.snake.draw(cube_buffer)
             self.cube.draw(cube_buffer)
             self.cube.show()
             if not is_a_raspberry():
                 sleep(0.01)
     except SnakeCollision:
         LOG.debug("collision")
         self.death_animation()
         return
     except SnakeInterrupted:
         return
Beispiel #4
0
 def run(self):
     try:
         while True:
             self.handle_events()
             v = [
                 sin(self.theta) * cos(self.phi),
                 sin(self.theta) * sin(self.phi),
                 cos(self.theta)
             ]
             self.rainbow(self.cube_buffer, v, self.wheel_offset)
             self.cube.draw(self.cube_buffer)
             self.cube.show()
             # Rotate vector
             self.theta += self.theta_d
             self.phi += self.phi_d
             # Cycle through all colors so the rainbow not only rotates, but also
             # moves through the cube.
             self.wheel_offset += self.wheel_offset_d
             self.wheel_offset %= 256
             if not is_a_raspberry():
                 sleep(0.02)
     except RainbowInterrupted:
         return
Beispiel #5
0
import logging
import copy
from random import randint

from connect4cube.app import App
from connect4cube.hardware.button_events import ButtonEvents
from connect4cube.hardware.cube import Cube, get_empty_cube_buffer
from connect4cube.hardware.util import is_a_raspberry
from connect4cube.util.color import wheel

if not is_a_raspberry():
    from time import sleep

LOG = logging.getLogger(__name__)


class Random(App):
    """
    fill the cube with random stuff and change it slowly
    """
    def __init__(self):
        self.button_events = ButtonEvents()
        self.cube = Cube()
        self.cube_buffer = get_empty_cube_buffer()
        self.wheel_start = self.get_random_wheel()
        self.wheel_stop = self.get_random_wheel()

    def run(self):
        steps = 100
        try:
            while True:
Beispiel #6
0
        def __init__(self,
                     axis_up=19,
                     axis_down=26,
                     axis_left=6,
                     axis_right=13,
                     button_a=12,
                     button_b=16):
            """
            all pin numbers in BMC, see https://pinout.xyz/
            """
            if not is_a_raspberry():
                Device.pin_factory = MockFactory()

            pin2fn = {
                axis_up:
                (lambda: self.button_pressed_dir(EventEnum.UP_PRESSED),
                 lambda: self.button_repeated(EventEnum.UP_REPEATED),
                 lambda: self.button_released(EventEnum.UP_PRESSED)),
                axis_down:
                (lambda: self.button_pressed_dir(EventEnum.DOWN_PRESSED),
                 lambda: self.button_repeated(EventEnum.DOWN_REPEATED),
                 lambda: self.button_released(EventEnum.DOWN_PRESSED)),
                axis_left:
                (lambda: self.button_pressed_dir(EventEnum.LEFT_PRESSED),
                 lambda: self.button_repeated(EventEnum.LEFT_REPEATED),
                 lambda: self.button_released(EventEnum.LEFT_PRESSED)),
                axis_right:
                (lambda: self.button_pressed_dir(EventEnum.RIGHT_PRESSED),
                 lambda: self.button_repeated(EventEnum.RIGHT_REPEATED),
                 lambda: self.button_released(EventEnum.RIGHT_PRESSED)),
                button_a: (lambda: self.button_pressed(EventEnum.A_PRESSED),
                           lambda: self.button_repeated(EventEnum.A_REPEATED),
                           lambda: self.button_released(EventEnum.A_PRESSED)),
                button_b: (lambda: self.button_pressed(EventEnum.B_PRESSED),
                           lambda: self.button_repeated(EventEnum.B_REPEATED),
                           lambda: self.button_released(EventEnum.B_PRESSED)),
            }
            self.buttons = []
            for pin, fns in pin2fn.items():
                # Debouncing function of Button (argument bounce_time) is broken and is not usable.
                button = Button(pin, hold_repeat=True, hold_time=1)
                button.when_pressed = fns[0]
                button.when_held = fns[1]
                button.when_released = fns[2]
                self.buttons.append(button)

            self.last_up_time = self.LastEventTime()
            self.last_down_time = self.LastEventTime()
            self.last_left_time = self.LastEventTime()
            self.last_right_time = self.LastEventTime()
            self.last_a_time = self.LastEventTime()
            self.last_b_time = self.LastEventTime()
            self.last_event_times = {
                EventEnum.UP_PRESSED: self.last_up_time,
                EventEnum.DOWN_PRESSED: self.last_down_time,
                EventEnum.LEFT_PRESSED: self.last_left_time,
                EventEnum.RIGHT_PRESSED: self.last_right_time,
                EventEnum.A_PRESSED: self.last_a_time,
                EventEnum.B_PRESSED: self.last_b_time
            }
            # Dictionary to get the event times of the reverse direction.
            # Used for additional debouncing of joystick.
            self.last_reverse_dir_event_times = {
                EventEnum.UP_PRESSED: self.last_down_time,
                EventEnum.DOWN_PRESSED: self.last_up_time,
                EventEnum.LEFT_PRESSED: self.last_right_time,
                EventEnum.RIGHT_PRESSED: self.last_left_time
            }

            self.lock = Lock()
            self.event_queue = Queue()
Beispiel #7
0
 def __init__(self):
     if is_a_raspberry():
         self.cube = LedCube()
     else:
         self.cube = VPythonCube()
Beispiel #8
0
from typing import List, Tuple
from connect4cube.hardware.util import is_a_raspberry

if is_a_raspberry():
    from connect4cube.hardware.cube_led import LedCube
else:
    from connect4cube.hardware.cube_vpython import VPythonCube

CubeType = List[List[List[Tuple[int, int, int]]]]
CoordType = Tuple[int, int, int]


class Cube:
    """
    Singleton class of an LED cube. Depending on the target the real LED cube or a vpython mockup is used.
    """
    instance = None

    class __Cube:
        cube = None

        def __init__(self):
            if is_a_raspberry():
                self.cube = LedCube()
            else:
                self.cube = VPythonCube()

        def set_color(self, x: int, y: int, z: int, r: int, g: int,
                      b: int) -> None:
            self.cube.set_color(x, y, z, r, g, b)