added a popup for the quit button in the game

This commit is contained in:
Faraphel 2023-03-03 18:26:23 +01:00
parent 16e88462a1
commit aa4b2f8eb2
6 changed files with 82 additions and 10 deletions

View file

@ -3,12 +3,13 @@ from typing import TYPE_CHECKING
from source.core.enums import BombState from source.core.enums import BombState
from source.core.error import InvalidBombPosition, PositionAlreadyShot 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.scene.abc import Scene
from source.gui import widget, texture from source.gui import widget, texture, scene
from source import core from source import core
from source.network.packet import PacketChat, PacketBombPlaced, PacketBoatPlaced, PacketBombState from source.network.packet import PacketChat, PacketBombPlaced, PacketBoatPlaced, PacketBombState
from source.type import Point2D from source.type import Point2D
from source.utils import StoppableThread
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
@ -16,6 +17,7 @@ if TYPE_CHECKING:
class Game(Scene): class Game(Scene):
def __init__(self, window: "Window", def __init__(self, window: "Window",
thread: StoppableThread,
connection: socket.socket, connection: socket.socket,
boats_length: list, boats_length: list,
@ -28,6 +30,7 @@ class Game(Scene):
**kwargs): **kwargs):
super().__init__(window, **kwargs) super().__init__(window, **kwargs)
self.thread = thread
self.connection = connection self.connection = connection
self.boats_length = boats_length self.boats_length = boats_length
self.name_ally = name_ally self.name_ally = name_ally
@ -171,6 +174,9 @@ class Game(Scene):
style=texture.Button.Style1 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( self.label_state = self.add_widget(
widget.Text, widget.Text,
@ -211,8 +217,13 @@ class Game(Scene):
"L'adversaire place ses bombes..." "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): 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): def chat_new_message(self, author: str, content: str):
message: str = f"[{author}] - {content}" message: str = f"[{author}] - {content}"

View file

@ -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())

View file

@ -3,18 +3,19 @@ from typing import TYPE_CHECKING
import pyglet.clock import pyglet.clock
from source.gui import texture, widget 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 from source.gui.scene.abc.Popup import Popup
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
from source.gui.scene import Game
class Result(Popup): class GameResult(Popup):
def __init__(self, window: "Window", won: bool, **kwargs): def __init__(self, window: "Window", game_scene: "Game", won: bool, **kwargs):
super().__init__(window, **kwargs) super().__init__(window, **kwargs)
self.game_scene = game_scene
self.image = self.add_widget( self.image = self.add_widget(
widget.Image, widget.Image,
@ -24,5 +25,4 @@ class Result(Popup):
# TODO: rendre l'image transparente si possible # TODO: rendre l'image transparente si possible
from source.gui.scene import MainMenu pyglet.clock.schedule_once(lambda dt: self.game_scene.quit, 5.0)
pyglet.clock.schedule_once(lambda dt: self.window.set_scene(MainMenu), 5.0)

View file

@ -1,5 +1,6 @@
from .Result import Result from .GameResult import GameResult
from .Game import Game from .Game import Game
from .GameQuit import GameQuit
from .Settings import Settings from .Settings import Settings
from .RoomHost import RoomHost from .RoomHost import RoomHost
from .RoomJoin import RoomJoin from .RoomJoin import RoomJoin

View file

@ -41,6 +41,7 @@ class Client(StoppableThread):
self.window.set_scene, self.window.set_scene,
scene.Game, scene.Game,
thread=self,
connection=connection, connection=connection,
boats_length=settings.boats_length, boats_length=settings.boats_length,

View file

@ -52,6 +52,7 @@ class Host(StoppableThread):
self.window.set_scene, self.window.set_scene,
scene.Game, scene.Game,
thread=self,
connection=connection, connection=connection,
boats_length=self.settings.boats_length, boats_length=self.settings.boats_length,