Duel arcade
demo-kit/ é o que simula o dispositivo para a demo na página inicial. O próprio dispositivo não precisa dele.
demo-kit/ é o que simula o dispositivo para a demo na página inicial. O próprio dispositivo não precisa dele.
"""The cabinet itself: its joystick, its button, its screen and its lights, behind one small class.
The rest of the program only knows ``Cabinet``. ``SimulatedCabinet`` is the one that works: a computer
player holds the stick and nothing is drawn. ``HardwareCabinet`` marks where a real cabinet would be
connected; it is NOT implemented and NOT tested.
"""
from __future__ import annotations
from typing import Any, Callable, Dict, Optional
from game import ComputerPlayer, Input
class Cabinet:
"""What the program needs from a cabinet. All of it is called from the event loop and must not block."""
computer = False
"""True when a computer player holds the stick; the companion page says so."""
def read_input(self, view: Optional[Dict[str, Any]]) -> Input:
"""The stick (x right, y down, each -1 to 1) and whether the button is down. Called about 60 times a
second on the host and 30 times on the guest. `view` is the game as the screen last showed it."""
raise NotImplementedError
def draw(self, view: Dict[str, Any]) -> None:
"""Shows the game: `view` is a snapshot as game.Game.snapshot() makes it."""
def event(self, event: Dict[str, Any]) -> None:
"""Something happened in the game (a shock, a hit, a fall, the end of a match): effects and sounds."""
def light_up(self, challenger: str) -> None:
"""The other cabinet's player asks for a match: light up, so that somebody in the room notices."""
class SimulatedCabinet(Cabinet):
"""No cabinet at all: a computer player plays this seat, nothing is drawn, and being challenged is a
line on the console."""
computer = True
def __init__(self, seat: int, log: Callable[[str], None]) -> None:
self._player = ComputerPlayer(seat)
self._log = log
def read_input(self, view: Optional[Dict[str, Any]]) -> Input:
return self._player.decide(view)
def event(self, event: Dict[str, Any]) -> None:
if event.get("event") == "fall":
self._log(f"round {event['round']}: {event['score'][0]} : {event['score'][1]}")
elif event.get("event") == "match":
self._log(f"match over, {event['score'][0]} : {event['score'][1]} after {event['rounds']} rounds")
def light_up(self, challenger: str) -> None:
self._log(f"(simulated cabinet) the lights would flash now: {challenger} challenges this cabinet")
class HardwareCabinet(Cabinet):
"""Where a real cabinet would be connected. Nothing in this class works: it has never been run on a
cabinet, and it is here only to show what would have to be filled in.
* read_input(): a digital arcade stick is four switches and the button is a fifth, each from a GPIO pin
to ground with the pull-up enabled (for example with ``gpiozero.Button``). x is right minus left, y is
down minus up; the game shortens a diagonal to length 1. An analog stick needs an ADC or a USB
controller read through the joystick interface of the screen library.
* draw(): a full-screen window on the cabinet's display (for example with ``pygame``): the plate with
radius ``view["rim"]`` around the centre, the two skimmers at ``view["p"][seat]`` with their charge,
the spark, the score. Lengths are in plate radii. The host draws what it simulates; the guest draws
the snapshots it receives, 30 a second, and can move the skimmers along their ``vx``, ``vy`` between
two of them.
* event(): the flash and the sound for a shock, a hit, a fall and the end of a match.
* light_up(): marquee lights or an LED strip on a GPIO pin, and the attract screen with the
challenger's name.
Drawing and reading pins must not block the event loop for longer than a frame.
"""
def __init__(self) -> None:
raise NotImplementedError(
"A real cabinet (screen, joystick, lights) is not implemented in this example (see the class HardwareCabinet in cabinet.py). "
"Run it with --cabinet simulator."
)