started the networking and interface connection
This commit is contained in:
parent
b0844e981a
commit
ca001511c8
18 changed files with 236 additions and 29 deletions
137
source/gui/scene/Game.py
Normal file
137
source/gui/scene/Game.py
Normal file
|
@ -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()
|
|
@ -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))
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from .RoomCreate import RoomCreate
|
||||
from .RoomJoin import RoomJoin
|
||||
from .Settings import Settings
|
||||
from .Game import Game
|
||||
|
||||
from .MainMenu import MainMenu
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
23
source/network/Client.py
Normal file
23
source/network/Client.py
Normal file
|
@ -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)
|
24
source/network/Host.py
Normal file
24
source/network/Host.py
Normal file
|
@ -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)
|
2
source/network/__init__.py
Normal file
2
source/network/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
from .Client import Client
|
||||
from .Host import Host
|
0
source/network/test/__init__.py
Normal file
0
source/network/test/__init__.py
Normal file
|
@ -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}")
|
||||
|
|
@ -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()
|
||||
|
Loading…
Reference in a new issue