From aa4b2f8eb2921843a08cd28d9425a19d5c636a2c Mon Sep 17 00:00:00 2001 From: Faraphel Date: Fri, 3 Mar 2023 18:26:23 +0100 Subject: [PATCH] added a popup for the quit button in the game --- source/gui/scene/Game.py | 17 +++++- source/gui/scene/GameQuit.py | 58 +++++++++++++++++++ source/gui/scene/{Result.py => GameResult.py} | 12 ++-- source/gui/scene/__init__.py | 3 +- source/network/Client.py | 1 + source/network/Host.py | 1 + 6 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 source/gui/scene/GameQuit.py rename source/gui/scene/{Result.py => GameResult.py} (63%) diff --git a/source/gui/scene/Game.py b/source/gui/scene/Game.py index b603bd0..b65d7a0 100644 --- a/source/gui/scene/Game.py +++ b/source/gui/scene/Game.py @@ -3,12 +3,13 @@ from typing import TYPE_CHECKING from source.core.enums import BombState from source.core.error import InvalidBombPosition, PositionAlreadyShot -from source.gui.scene import Result +from source.gui.scene import GameResult from source.gui.scene.abc import Scene -from source.gui import widget, texture +from source.gui import widget, texture, scene from source import core from source.network.packet import PacketChat, PacketBombPlaced, PacketBoatPlaced, PacketBombState from source.type import Point2D +from source.utils import StoppableThread if TYPE_CHECKING: from source.gui.window import Window @@ -16,6 +17,7 @@ if TYPE_CHECKING: class Game(Scene): def __init__(self, window: "Window", + thread: StoppableThread, connection: socket.socket, boats_length: list, @@ -28,6 +30,7 @@ class Game(Scene): **kwargs): super().__init__(window, **kwargs) + self.thread = thread self.connection = connection self.boats_length = boats_length self.name_ally = name_ally @@ -171,6 +174,9 @@ class Game(Scene): style=texture.Button.Style1 ) + self.button_quit.add_listener("on_click_release", + lambda *_: self.window.add_scene(scene.GameQuit, game_scene=self)) + self.label_state = self.add_widget( widget.Text, @@ -211,8 +217,13 @@ class Game(Scene): "L'adversaire place ses bombes..." ) + def quit(self): + self.thread.stop() + from source.gui.scene import MainMenu + self.window.set_scene(MainMenu) + def game_end(self, won: bool): - self.window.add_scene(Result, won=won) + self.window.add_scene(GameResult, won=won) def chat_new_message(self, author: str, content: str): message: str = f"[{author}] - {content}" diff --git a/source/gui/scene/GameQuit.py b/source/gui/scene/GameQuit.py new file mode 100644 index 0000000..449fe88 --- /dev/null +++ b/source/gui/scene/GameQuit.py @@ -0,0 +1,58 @@ +from typing import TYPE_CHECKING + +from source.gui import widget, texture +from source.gui.scene.abc.Popup import Popup + +if TYPE_CHECKING: + from source.gui.window import Window + from source.gui.scene import Game + + +class GameQuit(Popup): + def __init__(self, window: "Window", game_scene: "Game", **kwargs): + super().__init__(window, **kwargs) + + self.game_scene = game_scene + + self.background = self.add_widget( + widget.Image, + + x=0, y=0, width=1.0, height=1.0, + + image=texture.Popup.Style1.background + ) + + self.text = self.add_widget( + widget.Text, + + x=0.5, y=0.5, width=1.0, + + anchor_x="center", + + text="Voulez-vous vraiment quitter la partie ?\n(Votre partie ne sera pas sauvegardé !)", + font_size=28, + multiline=True, + align="center", + ) + + self.cancel = self.add_widget( + widget.Button, + x=0.20, y=0.20, width=0.2, height=0.1, + + label_text="Annuler", + + style=texture.Button.Style1 + ) + + self.cancel.add_listener("on_click_release", lambda *_: self.window.remove_scene(self)) + + self.confirm = self.add_widget( + widget.Button, + x=0.60, y=0.20, width=0.2, height=0.1, + + label_text="Confirmer", + + style=texture.Button.Style1 + ) + + self.confirm.add_listener("on_click_release", lambda *_: self.game_scene.quit()) diff --git a/source/gui/scene/Result.py b/source/gui/scene/GameResult.py similarity index 63% rename from source/gui/scene/Result.py rename to source/gui/scene/GameResult.py index 3f17d41..73b69cb 100644 --- a/source/gui/scene/Result.py +++ b/source/gui/scene/GameResult.py @@ -3,18 +3,19 @@ from typing import TYPE_CHECKING import pyglet.clock from source.gui import texture, widget -from source.gui.event import StopEvent -from source.gui.scene.abc import Scene from source.gui.scene.abc.Popup import Popup if TYPE_CHECKING: from source.gui.window import Window + from source.gui.scene import Game -class Result(Popup): - def __init__(self, window: "Window", won: bool, **kwargs): +class GameResult(Popup): + def __init__(self, window: "Window", game_scene: "Game", won: bool, **kwargs): super().__init__(window, **kwargs) + self.game_scene = game_scene + self.image = self.add_widget( widget.Image, @@ -24,5 +25,4 @@ class Result(Popup): # TODO: rendre l'image transparente si possible - from source.gui.scene import MainMenu - pyglet.clock.schedule_once(lambda dt: self.window.set_scene(MainMenu), 5.0) + pyglet.clock.schedule_once(lambda dt: self.game_scene.quit, 5.0) diff --git a/source/gui/scene/__init__.py b/source/gui/scene/__init__.py index b567231..3f5d5d0 100644 --- a/source/gui/scene/__init__.py +++ b/source/gui/scene/__init__.py @@ -1,5 +1,6 @@ -from .Result import Result +from .GameResult import GameResult from .Game import Game +from .GameQuit import GameQuit from .Settings import Settings from .RoomHost import RoomHost from .RoomJoin import RoomJoin diff --git a/source/network/Client.py b/source/network/Client.py index f7a7089..7b6964c 100644 --- a/source/network/Client.py +++ b/source/network/Client.py @@ -41,6 +41,7 @@ class Client(StoppableThread): self.window.set_scene, scene.Game, + thread=self, connection=connection, boats_length=settings.boats_length, diff --git a/source/network/Host.py b/source/network/Host.py index 55338e0..3da41e9 100644 --- a/source/network/Host.py +++ b/source/network/Host.py @@ -52,6 +52,7 @@ class Host(StoppableThread): self.window.set_scene, scene.Game, + thread=self, connection=connection, boats_length=self.settings.boats_length,