diff --git a/source/gui/scene/Game.py b/source/gui/scene/Game.py new file mode 100644 index 0000000..61569e0 --- /dev/null +++ b/source/gui/scene/Game.py @@ -0,0 +1,137 @@ +from typing import TYPE_CHECKING + +import pyglet + +from source.gui.scene.abc import Scene +from source.gui.widget import GameGrid, Text, Input, Button + +if TYPE_CHECKING: + from source.gui.window import Window + + +class Game(Scene): + def __init__(self, window: "Window", **kwargs): + super().__init__(window, **kwargs) + + texture_input_normal = pyglet.image.load("assets/image/input/normal.png") + texture_input_active = pyglet.image.load("./assets/image/input/active.png") + texture_input_error = pyglet.image.load("./assets/image/input/error.png") + + texture_button_normal = pyglet.image.load("./assets/image/button/normal.png") + texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png") + texture_button_click = pyglet.image.load("./assets/image/button/clicking.png") + + texture_grid_background = pyglet.image.load("./assets/image/grid/background.png") + + self.grid_ally = self.add_widget( + GameGrid, + + x=75, y=0.25, width=0.35, height=0.5, + + texture_background=texture_grid_background, + rows=8, columns=8, + ) + + self.grid_enemy = self.add_widget( + GameGrid, + + x=lambda widget: widget.scene.window.width - 75 - widget.width, y=0.25, width=0.35, height=0.5, + + texture_background=texture_grid_background, + rows=8, columns=8, + ) + + self.name_ally = self.add_widget( + Text, + + x=0.35, y=0.9, + + text="Raphael", + font_size=20, + anchor_x="center", anchor_y="center", + ) + + self.name_enemy = self.add_widget( + Text, + + x=0.65, y=0.9, + + text="Leo", + font_size=20, + anchor_x="center", anchor_y="center", + ) + + self.score_ally = self.add_widget( + Text, + + x=0.48, y=0.9, + + text="7", + font_size=25, + anchor_x="right", anchor_y="center" + ) + + self.score_enemy = self.add_widget( + Text, + + x=0.52, y=0.9, + + text="5", + font_size=25, + anchor_x="left", anchor_y="center" + ) + + self.chat_log = self.add_widget( + Text, + + x=10, y=70, width=0.5, height=200, + + text="FARAPHEL - HELLO BILLY\nLEO - HELLO BOLLO", + multiline=True + ) + + self.chat_input = self.add_widget( + Input, + + x=10, y=10, width=0.5, height=50, + + texture_normal=texture_input_normal, + texture_active=texture_input_active, + texture_error=texture_input_error, + ) + + self.button_save = self.add_widget( + Button, + + x=0.7, y=0, width=0.15, height=0.1, + + texture_normal=texture_button_normal, + texture_hover=texture_button_hover, + texture_click=texture_button_click + ) + + self.button_quit = self.add_widget( + Button, + + x=0.85, y=0, width=0.15, height=0.1, + + texture_normal=texture_button_normal, + texture_hover=texture_button_hover, + texture_click=texture_button_click + ) + + def on_draw(self): + self.grid_ally.draw() + self.grid_enemy.draw() + + self.name_ally.draw() + self.name_enemy.draw() + + self.score_ally.draw() + self.score_enemy.draw() + + self.chat_log.draw() + self.chat_input.draw() + + self.button_save.draw() + self.button_quit.draw() diff --git a/source/gui/scene/MainMenu.py b/source/gui/scene/MainMenu.py index ebd3950..fe3f6b9 100644 --- a/source/gui/scene/MainMenu.py +++ b/source/gui/scene/MainMenu.py @@ -2,7 +2,7 @@ from typing import TYPE_CHECKING import pyglet -from source.gui.scene import RoomJoin, RoomCreate, Settings +from source.gui.scene import RoomJoin, RoomCreate, Settings, Game from source.gui.scene.abc import Scene from source.gui.widget import Image, Text, Button from source.gui.widget.debug import FPSDisplay @@ -76,7 +76,8 @@ class MainMenu(Scene): texture_click=texture_button_click ) - self.settings.on_release = lambda *_: self.window.set_scene(Settings) + # self.settings.on_release = lambda *_: self.window.set_scene(Settings) + self.settings.on_release = lambda *_: self.window.set_scene(Game) self.fps_display = self.add_widget(FPSDisplay, color=(255, 255, 255, 180)) diff --git a/source/gui/scene/RoomCreate.py b/source/gui/scene/RoomCreate.py index 14a3b65..c050ec2 100644 --- a/source/gui/scene/RoomCreate.py +++ b/source/gui/scene/RoomCreate.py @@ -3,6 +3,7 @@ from typing import TYPE_CHECKING import pyglet import requests +from source import network from source.gui.scene.abc import Scene from source.gui.widget import Text, Button @@ -17,7 +18,7 @@ class RoomCreate(Scene): r = requests.get('https://api.ipify.org') r.raise_for_status() ip_address: str = r.content.decode('utf8') - port: str = "6464" + port: int = 52321 texture_button_normal = pyglet.image.load("./assets/image/button/normal.png") texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png") @@ -52,6 +53,9 @@ class RoomCreate(Scene): text="En attente d'un second joueur..." ) + self.thread = network.Host(window=self.window, daemon=True) + self.thread.start() + def on_draw(self): self.back.draw() self.label_ip.draw() diff --git a/source/gui/scene/RoomJoin.py b/source/gui/scene/RoomJoin.py index 0c7cf9d..53b2322 100644 --- a/source/gui/scene/RoomJoin.py +++ b/source/gui/scene/RoomJoin.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING import pyglet +from source import network from source.gui.scene.abc import Scene from source.gui.widget import Input, Button @@ -68,6 +69,12 @@ class RoomJoin(Scene): texture_click=texture_button_click ) + self.connect.on_release = lambda *_: network.Client( + window=self.window, + ip_address=self.entry_ip.text, + daemon=True + ).start() + def on_draw(self): self.back.draw() self.entry_ip.draw() diff --git a/source/gui/scene/__init__.py b/source/gui/scene/__init__.py index 1a6051f..6ac122e 100644 --- a/source/gui/scene/__init__.py +++ b/source/gui/scene/__init__.py @@ -1,5 +1,6 @@ from .RoomCreate import RoomCreate from .RoomJoin import RoomJoin from .Settings import Settings +from .Game import Game from .MainMenu import MainMenu diff --git a/source/gui/widget/Button.py b/source/gui/widget/Button.py index d2d7bf9..2b786eb 100644 --- a/source/gui/widget/Button.py +++ b/source/gui/widget/Button.py @@ -72,8 +72,8 @@ class Button(BoxWidget): self.background.image = self.background_texture def _refresh_size(self) -> None: - self.background.x, self.background.y = self.x, self.y - self.background.width, self.background.height = self.width, self.height + self.background.x, self.background.y = self.xy + self.background.width, self.background.height = self.size # center the label self.label.x = self.x + (self.width / 2) diff --git a/source/gui/widget/Checkbox.py b/source/gui/widget/Checkbox.py index b3de67d..59aaeb8 100644 --- a/source/gui/widget/Checkbox.py +++ b/source/gui/widget/Checkbox.py @@ -46,8 +46,8 @@ class Checkbox(BoxWidget): self.tick.image = self.tick_texture def _refresh_size(self): - self.tick.x, self.tick.y = self.x, self.y - self.tick.width, self.tick.height = self.width, self.height + self.tick.x, self.tick.y = self.xy + self.tick.width, self.tick.height = self.size # property diff --git a/source/gui/widget/GameGrid.py b/source/gui/widget/GameGrid.py index 1f6a9a1..d21e489 100644 --- a/source/gui/widget/GameGrid.py +++ b/source/gui/widget/GameGrid.py @@ -5,7 +5,7 @@ import pyglet.shapes from source.gui.sprite import Sprite from source.gui.widget.abc import BoxWidget from source.type import Distance - +from source.utils import dict_prefix if TYPE_CHECKING: from source.gui.scene.abc import Scene @@ -19,8 +19,6 @@ class GameGrid(BoxWidget): texture_background: pyglet.image.AbstractImage, - line_width: int = 2, - x: Distance = 0, y: Distance = 0, width: Distance = None, @@ -32,14 +30,24 @@ class GameGrid(BoxWidget): self._rows = rows self._columns = columns - self.background = Sprite(img=texture_background) + self.background = Sprite( + img=texture_background, + **dict_prefix("background_", kwargs) + ) self.lines: list[pyglet.shapes.Line] = [ - pyglet.shapes.Line(0, 0, 0, 0, width=line_width) + pyglet.shapes.Line( + 0, 0, 0, 0, + **dict_prefix("line_", kwargs) + ) for _ in range((self._columns - 1) + (self._rows - 1)) ] - self.cursor = pyglet.shapes.Rectangle(0, 0, 0, 0, color=(0, 0, 0, 100)) + self.cursor = pyglet.shapes.Rectangle( + 0, 0, 0, 0, + color=(0, 0, 0, 100), + **dict_prefix("cursor_", kwargs) + ) self._refresh_size() @@ -53,8 +61,8 @@ class GameGrid(BoxWidget): # refresh def _refresh_size(self): - self.background.x, self.background.y = self.x, self.y - self.background.width, self.background.height = self.width, self.height + self.background.x, self.background.y = self.xy + self.background.width, self.background.height = self.size for column, line in enumerate(self.lines[:self._columns-1], start=1): line.x = self.x + self.cell_width * column diff --git a/source/gui/widget/Image.py b/source/gui/widget/Image.py index 69ca323..476fa32 100644 --- a/source/gui/widget/Image.py +++ b/source/gui/widget/Image.py @@ -30,7 +30,7 @@ class Image(BoxWidget): # refresh def _refresh_size(self): - self.image.width, self.image.height = self.width, self.height + self.image.width, self.image.height = self.size # event diff --git a/source/gui/widget/Input.py b/source/gui/widget/Input.py index b8059bd..7cfac8d 100644 --- a/source/gui/widget/Input.py +++ b/source/gui/widget/Input.py @@ -72,8 +72,8 @@ class Input(BoxWidget): self.background.image = self.background_texture def _refresh_size(self) -> None: - self.background.x, self.background.y = self.x, self.y - self.background.width, self.background.height = self.width, self.height + self.background.x, self.background.y = self.xy + self.background.width, self.background.height = self.size # center the label self.label.x = self.x + (self.width / 2) diff --git a/source/gui/widget/Scroller.py b/source/gui/widget/Scroller.py index 1272d32..a9c4532 100644 --- a/source/gui/widget/Scroller.py +++ b/source/gui/widget/Scroller.py @@ -4,7 +4,7 @@ import pyglet.image from source.gui.sprite import Sprite from source.gui.widget.abc import BoxWidget -from source.type import Distance, Percentage +from source.type import Distance from source.utils import dict_prefix if TYPE_CHECKING: @@ -56,8 +56,8 @@ class Scroller(BoxWidget): def _refresh(self): # background - self.background.x, self.background.y = self.x, self.y - self.background.width, self.background.height = self.width, self.height + self.background.x, self.background.y = self.xy + self.background.width, self.background.height = self.size # cursor self.cursor.width = self.width * self.cursor_width diff --git a/source/gui/widget/Text.py b/source/gui/widget/Text.py index f02e7f0..6a04c99 100644 --- a/source/gui/widget/Text.py +++ b/source/gui/widget/Text.py @@ -25,15 +25,16 @@ class Text(BoxWidget): **kwargs): super().__init__(scene, x, y, width, height) - self.label = pyglet.text.Label(**kwargs) + self.label = pyglet.text.Label( + x=self.x, y=self.y, width=self.width, height=self.height, + **kwargs + ) self._refresh_size() def _refresh_size(self): - self.label.x = self.x - self.label.y = self.y - self.label.width = self.width - self.label.height = self.height + self.label.x, self.label.y = self.xy + self.label.width, self.label.height = self.size def on_resize(self, width: int, height: int): self._refresh_size() diff --git a/source/network/Client.py b/source/network/Client.py new file mode 100644 index 0000000..cdce1ad --- /dev/null +++ b/source/network/Client.py @@ -0,0 +1,23 @@ +import socket +from threading import Thread + +from source.gui.scene import Game + + +class Client(Thread): + def __init__(self, window: "Window", ip_address: str, port: int = 52321, **kw): + super().__init__(**kw) + + self.window = window + self.ip_address = ip_address + self.port = port + + def run(self) -> None: + print("[Client] Thread démarré") + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((self.ip_address, self.port)) + + print(f"[Client] Connecté avec {s}") + + self.window.set_scene(Game) diff --git a/source/network/Host.py b/source/network/Host.py new file mode 100644 index 0000000..4f91721 --- /dev/null +++ b/source/network/Host.py @@ -0,0 +1,24 @@ +import socket +from threading import Thread + +from source.gui.scene import Game + + +class Host(Thread): + def __init__(self, window: "Window", port: int = 52321, **kw): + super().__init__(**kw) + + self.window = window + self.port = port + + def run(self) -> None: + print("[Serveur] Thread démarré") + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("", self.port)) + s.listen() + connection, address = s.accept() + + print(f"[Serveur] Connecté avec {address}") + + self.window.set_scene(Game) diff --git a/source/network/__init__.py b/source/network/__init__.py new file mode 100644 index 0000000..4f77a77 --- /dev/null +++ b/source/network/__init__.py @@ -0,0 +1,2 @@ +from .Client import Client +from .Host import Host diff --git a/source/network/test/__init__.py b/source/network/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/source/reseau_test/client.py b/source/network/test/client.py similarity index 98% rename from source/reseau_test/client.py rename to source/network/test/client.py index 71945aa..4f7ced0 100644 --- a/source/reseau_test/client.py +++ b/source/network/test/client.py @@ -8,7 +8,7 @@ from source.core.enums import Orientation, BombState from source.core.error import InvalidBoatPosition, InvalidBombPosition, PositionAlreadyShot with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - s.connect(("127.0.0.1", 52321)) + s.connect(("127.0.0.1", 52322)) print(f"[Client] Connecté avec {s}") diff --git a/source/reseau_test/server.py b/source/network/test/server.py similarity index 98% rename from source/reseau_test/server.py rename to source/network/test/server.py index d91ea33..f31e438 100644 --- a/source/reseau_test/server.py +++ b/source/network/test/server.py @@ -8,8 +8,7 @@ from source.core.enums import Orientation, BombState from source.core.error import InvalidBoatPosition, InvalidBombPosition, PositionAlreadyShot with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - host = socket.gethostname() - s.bind((host, 52321)) + s.bind(("", 52321)) s.listen() conn, addr = s.accept()