From 4856bcda5af1d54e6ee21cae8c5976a9fcf97ca6 Mon Sep 17 00:00:00 2001 From: Giovani Date: Wed, 14 Jul 2021 15:21:53 -0400 Subject: [PATCH] wip: add connection to server scene --- config.yaml | 13 +++++++--- tetri5/scene.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++- tetri5/util.py | 5 ++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/config.yaml b/config.yaml index 5b1193f..af07dd0 100644 --- a/config.yaml +++ b/config.yaml @@ -18,13 +18,13 @@ image: font: "resource/image/press-start-2p-font.bmp" position: - # title screen + # title scene title-logo: [240, 60] option-one: [320, 390] option-two: [320, 440] cursor-option-one: [300, 399] cursor-option-two: [300, 449] - # single player game screen + # single player game scene top-label: [80, 120] top-value: [80, 140] score-label: [80, 180] @@ -35,7 +35,11 @@ position: well: [280, 80] next-piece: [620, 160] spawn-piece: [-260, -60] - # multi player game screen + # connection scene + game-id-label: [200, 300] + connecting-label: [280, 300] + waiting-opponent-label: [200, 300] + # multi player game scene well-player-1: [80, 80] well-player-2: [480, 80] score-label-player-1: [140, 20] @@ -55,7 +59,8 @@ engine: fps: 60 tile-size: 20 cursor-blink-interval: 150 - lines-per-level: 5 + period-blink-interval: 300 + lines-per-level: 55 # piece piece-drop-delay: 600 piece-lock-delay: 1000 diff --git a/tetri5/scene.py b/tetri5/scene.py index ab30039..71f6ced 100644 --- a/tetri5/scene.py +++ b/tetri5/scene.py @@ -72,7 +72,7 @@ class TitleScene(Scene): self._cursor_off = not self._cursor_off if Controller.key_pressed(pygame.K_RETURN): - self._change_scence(SinglePlayerScene(self._change_scence) if not self._is_multiplayer else MultiplayerScene(self._change_scence)) + self._change_scence(SinglePlayerScene(self._change_scence) if not self._is_multiplayer else ConnectionScene(self._change_scence)) """ TODO @@ -162,6 +162,69 @@ class SinglePlayerScene(Scene): def _clear_current_piece(self) -> None: self._current_piece = None +class ConnectionScene(Scene): + + MAX_GAME_ID_LEN = 5 + MAX_PERIOD_COUNT = 3 + + def __init__(self, change_scene: FunctionType) -> None: + self._background_color = pygame.Color(ConfigurationManager.get("color", "window-bg")) + self._is_connecting = False + self._game_id_label_pos = ConfigurationManager.get("position", "game-id-label") + self._game_id_label = "Enter Game Id: " + self._game_id = "_" + self._cursor_blink_interval = ConfigurationManager.get("engine", "cursor-blink-interval") + self._cursor_blink_acc = 0 # blink accumulator + self._connecting_label_pos = ConfigurationManager.get("position", "connecting-label") + self._connecting_label = "Connecting" + self._period_blink_interval = ConfigurationManager.get("engine", "period-blink-interval") + self._period_blink_acc = 0 # period accumulator + + def draw(self, surface: pygame.Surface) -> None: + surface.fill(self._background_color) + + if not self._is_connecting: + TextGenerator.draw(self._game_id_label + self._game_id, self._game_id_label_pos, surface) + else: + TextGenerator.draw(self._connecting_label, self._connecting_label_pos, surface) + + def update(self, elapsed_time: int) -> None: + # cursor blink logic + self._cursor_blink_acc += elapsed_time + if self._cursor_blink_acc >= self._cursor_blink_interval: + self._cursor_blink_acc = 0 + if self._game_id.find("_") != -1: + self._game_id = self._game_id.replace("_", "") + else: + self._game_id += "_" + + # period ellipsis logic + if self._is_connecting: + self._period_blink_acc += elapsed_time + if self._period_blink_acc >= self._period_blink_interval: + self._period_blink_acc = 0 + period_count = self._connecting_label.count(".") + if period_count < self.MAX_PERIOD_COUNT: + self._connecting_label += "." + else: + self._connecting_label = self._connecting_label.replace(".", "") + + # keyboard input + for event in pygame.event.get(pygame.KEYDOWN): + # user input logic + if not self._is_connecting: + self._game_id = self._game_id.replace("_", "") + if event.key == pygame.K_BACKSPACE: + self._game_id = self._game_id[:-1] + elif (event.unicode.isalpha() or event.unicode.isdigit()) and\ + len(self._game_id) <= self.MAX_GAME_ID_LEN: + self._game_id += event.unicode.upper() + + # connection logic + if event.key == pygame.K_RETURN and len(self._game_id) > 0 and not self._is_connecting: + self._is_connecting = True + MultiplayerService.init() + MultiplayerService.enter_game(self._game_id) """ TODO """ diff --git a/tetri5/util.py b/tetri5/util.py index 0e737a6..3060aa5 100644 --- a/tetri5/util.py +++ b/tetri5/util.py @@ -147,6 +147,11 @@ class TextGenerator: cls._characters["7"] = (11 * glyph_size[0], 1 * glyph_size[1]) cls._characters["8"] = (0 * glyph_size[0], 2 * glyph_size[1]) cls._characters["9"] = (1 * glyph_size[0], 2 * glyph_size[1]) + + # symbols + cls._characters[":"] = (2 * glyph_size[0], 11 * glyph_size[1]) + cls._characters["_"] = (3 * glyph_size[0], 5 * glyph_size[1]) + cls._characters["."] = (2 * glyph_size[0], 1 * glyph_size[1]) @classmethod def draw(cls, text: str, position: Tuple, surface: pygame.Surface) -> None: