From c2c401a75b5ed7dbdb32de98b76820981478b865 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Sat, 30 Apr 2022 22:06:56 -0500 Subject: [PATCH] finished implementing chess game --- __main__.py | 22 +++++++++++++++++--- plugin/chess/__init__.py | 5 +++-- plugin/chess/draw.py | 43 ++++++++++++++++++++-------------------- plugin/chess/game.py | 4 ++-- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/__main__.py b/__main__.py index a8b852d..10a0909 100644 --- a/__main__.py +++ b/__main__.py @@ -12,10 +12,14 @@ menu_config = [ MenuItem("Apps", MenuType.SUB_MENU, {"sub_menu": [ - MenuItem("Chess", + MenuItem("Chess Game", MenuType.PLUGIN, {"plugin": "chess", - "arg": None}), + "arg": "game"}), + MenuItem("Chess Puzzles", + MenuType.PLUGIN, + {"plugin": "chess", + "arg": "puzzles"}), MenuItem("Cube Timer", MenuType.PLUGIN, {"plugin": "cube", @@ -25,6 +29,18 @@ menu_config = [ MenuType.PLUGIN, {"plugin": "info", "arg": None}), + MenuItem("Config", + MenuType.SUB_MENU, + {"sub_menu": [ + MenuItem("Brightness", + MenuType.PLUGIN, + {"plugin": "config", + "arg": "brightness"}), + MenuItem("Wifi", + MenuType.PLUGIN, + {"plugin": "config", + "arg": "wifi"}), + ]}), MenuItem("Reboot", MenuType.EXIT_CMD, {"command": "sudo reboot"}), @@ -47,12 +63,12 @@ signal.signal(signal.SIGTERM, program_exit) try: while True: item = menu.get_selection() - ic(item) if item.menu_type == MenuType.PLUGIN: try: graphics.clear() graphics.text("Loading...", 0, 0, 1) graphics.show() + ic(item) plugin = import_module("plugin." + item.data["plugin"]) ic(plugin) plugin.execute(cinput, graphics, item.data["arg"]) diff --git a/plugin/chess/__init__.py b/plugin/chess/__init__.py index 920cae0..ca560e6 100644 --- a/plugin/chess/__init__.py +++ b/plugin/chess/__init__.py @@ -2,5 +2,6 @@ from cinput import ControlInput from graphics import Graphics from .game import ChessGame -def execute(cinput: ControlInput, graphics: Graphics, _): - ChessGame(cinput, graphics).run() +def execute(cinput: ControlInput, graphics: Graphics, arg: str): + if arg == "game": + ChessGame(cinput, graphics).run() diff --git a/plugin/chess/draw.py b/plugin/chess/draw.py index 227906e..f57edf0 100644 --- a/plugin/chess/draw.py +++ b/plugin/chess/draw.py @@ -1,7 +1,7 @@ from typing import Optional from icecream import ic from graphics import Graphics -import chess +from chess import Board, Outcome, Color, WHITE class Draw: BOARD_SIZE = 64 @@ -90,37 +90,41 @@ class Draw: c = 0 if c != 0 else 1 self._graphics.pixel(x, y, c) - def _cursor_clear(self, except_for: list[str], player_color: chess.Color): - c = 0 + def _cursor_clear(self, except_for: list[str], player_color: Color): + c = 1 for row in range(8): - for col in range(8): - sqx = col if player_color == chess.WHITE else 7 - col - sqy = row if player_color == chess.BLACK else 7 - row + for col in range(8): # 0,0 w = a8 b = h1 ... 7,7 w = h1 b = a8 + sqx = col if player_color == WHITE else 7 - col + sqy = 7 - row if player_color == WHITE else row square = chr(sqx + 97) + str(sqy + 1) if not (square in except_for): # see if square is right color - sqc = self._graphics.pixel(sqx * 8, sqy * 8) + sqc = self._graphics.pixel(col * 8, row * 8) if sqc != c: - ic(square) self._cursor(square, player_color); c = 0 if c == 1 else 1 c = 0 if c == 1 else 1 - def _cursor(self, square: str, player_color: chess.Color): + def _cursor(self, square: str, player_color: Color): sqx = ord(square[0]) - 97 sqy = int(square[1]) - 1 - if player_color == chess.WHITE: + if player_color == WHITE: sqy = 7 - sqy else: sqx = 7 - sqx - for i in range(7): - self._invert_pixel(sqx * 8 + i, sqy * 8) #0,0 - 7,0 - self._invert_pixel(sqx * 8 + i, sqy * 8 + 7) # 0,7 - 7,7 + for i in range(8): + # top and bottom row + self._invert_pixel(sqx * 8 + i, sqy * 8) + self._invert_pixel(sqx * 8 + i, sqy * 8 + 7) + if i > 0 and i < 7: + # sides (skipping top and bottom row) + self._invert_pixel(sqx * 8, sqy * 8 + i) + self._invert_pixel(sqx * 8 + 7, sqy * 8 + i) - def draw_board(self, board: chess.Board, player_color: chess.Color): + def draw_board(self, board: Board, player_color: Color): self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0) nb = "".join(str(board).split()).replace(" ", "") - nb = nb if player_color == chess.WHITE else nb[::-1] + nb = nb if player_color == WHITE else nb[::-1] c = True for row in range(8): for col in range(8): @@ -159,28 +163,25 @@ class Draw: "you win!" if player_won else "you lose!", 12, 2, 1) self._graphics.show() - def draw_select(self, board: chess.Board, last_move: str, player_color: chess.Color, src: Optional[str], dst: Optional[str] = None): + def draw_select(self, last_move: str, player_color: Color, src: Optional[str], dst: Optional[str] = None): self._clear_info() self._graphics.text(self._format_move(last_move), 12, 0, 1) self._graphics.text("You move:", 12, 2, 1) if src != None and dst == None: - ic('skipping src only') self._graphics.text("<" + src.upper() + "> - __", 12, 3, 1) self._cursor_clear([src], player_color) self._cursor(src, player_color) elif src != None and dst != None: - ic('skipping src and dest') self._graphics.text(src.upper() + " - <" + dst.upper() + ">", 12, 3, 1) self._cursor_clear([src, dst], player_color) self._cursor(src, player_color) self._cursor(dst, player_color) else: - ic('clearing all cursors') self._cursor_clear(list(), player_color) self._graphics.show() - def draw_game_over(self, outcome: Optional[chess.Outcome]): + def draw_game_over(self, outcome: Optional[Outcome]): self._clear_info() self._graphics.text("Game over", 12, 0, 1) if outcome != None: @@ -188,6 +189,6 @@ class Draw: self._graphics.text("Draw!", 12, 2, 1) else: self._graphics.text("Winner:", 12, 2, 1) - self._graphics.text("White!" if outcome.winner == chess.WHITE else "Black!", + self._graphics.text("White!" if outcome.winner == WHITE else "Black!", 12, 3, 1) self._graphics.show() diff --git a/plugin/chess/game.py b/plugin/chess/game.py index a1cf97c..64d42e5 100644 --- a/plugin/chess/game.py +++ b/plugin/chess/game.py @@ -120,7 +120,7 @@ class ChessGame: elif self._state == GameState.CHOOSE_SRC: if len(self._get_sources()) > 0: src = self._get_sources()[self._src_idx] - self._draw.draw_select(self._board, self._move, self._player_color, src) + self._draw.draw_select(self._move, self._player_color, src) key = self._cinput.get_one_shot(0.1) else: self._state = GameState.GAME_OVER @@ -129,7 +129,7 @@ class ChessGame: elif self._state == GameState.CHOOSE_DST: src = self._get_sources()[self._src_idx] dst = self._get_dests()[self._dst_idx] - self._draw.draw_select(self._board, self._move, self._player_color, src, dst) + self._draw.draw_select(self._move, self._player_color, src, dst) key = self._cinput.get_one_shot(0.1) # game has ended