added background on game and renamed some minor variable

This commit is contained in:
Faraphel 2023-02-17 22:49:44 +01:00
parent 8181885c84
commit ca59abee01
12 changed files with 1582 additions and 75 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 983 KiB

View file

@ -1,6 +1,6 @@
import numpy as np import numpy as np
from source.core.Boat import Boat from source.core import Boat
from source.core.enums import Orientation, BombState from source.core.enums import Orientation, BombState
from source.core.error import InvalidBoatPosition, PositionAlreadyShot, InvalidBombPosition from source.core.error import InvalidBoatPosition, PositionAlreadyShot, InvalidBombPosition
from source.type import Point2D from source.type import Point2D
@ -8,27 +8,27 @@ from source.utils import copy_array_offset
class Board: class Board:
__slots__ = ("_width", "_height", "_boats", "_bombs") __slots__ = ("_columns", "_rows", "_boats", "_bombs")
def __init__( def __init__(
self, self,
width: int, rows: int,
height: int = None, columns: int = None,
boats: dict[Boat, Point2D] = None, boats: dict[Boat, Point2D] = None,
bombs: np.array = None bombs: np.array = None
) -> None: ) -> None:
self._width: int = width self._rows: int = rows
self._height: int = width if height is None else height self._columns: int = rows if columns is None else columns
# associate the boats to their position # associate the boats to their position
self._boats: dict[Boat, Point2D] = {} if boats is None else boats self._boats: dict[Boat, Point2D] = {} if boats is None else boats
# position that have been shot by a bomb # position that have been shot by a bomb
self._bombs: np.array = np.ones((self._width, self._height), dtype=np.bool_) if bombs is None else bombs self._bombs: np.array = np.ones((self._columns, self._rows), dtype=np.bool_) if bombs is None else bombs
def __repr__(self) -> str: def __repr__(self) -> str:
return f"<{self.__class__.__name__} width={self._width}, height={self._height}>" return f"<{self.__class__.__name__} width={self._columns}, height={self._rows}>"
def __str__(self) -> str: def __str__(self) -> str:
return str(self.get_matrice()) return str(self.get_matrice())
@ -78,7 +78,7 @@ class Board:
# if the bomb is inside the board # if the bomb is inside the board
x, y = position x, y = position
if x >= self._width or y >= self._height: raise InvalidBombPosition(position) if x >= self._columns or y >= self._rows: raise InvalidBombPosition(position)
# if this position have already been shot # if this position have already been shot
if not self._bombs[position]: raise PositionAlreadyShot(position) if not self._bombs[position]: raise PositionAlreadyShot(position)
@ -113,7 +113,7 @@ class Board:
""" """
:return: the boat represented as a matrice :return: the boat represented as a matrice
""" """
board = np.zeros((self._width, self._height), dtype=np.ushort) board = np.zeros((self._columns, self._rows), dtype=np.ushort)
for index, (boat, position) in enumerate(self._boats.items(), start=1): for index, (boat, position) in enumerate(self._boats.items(), start=1):
# Paste the boat into the board at the correct position. # Paste the boat into the board at the correct position.
@ -126,8 +126,8 @@ class Board:
def to_json(self) -> dict: def to_json(self) -> dict:
return { return {
"width": self._width, "columns": self._columns,
"height": self._height, "rows": self._rows,
"boats": [[boat.to_json(), position] for boat, position in self._boats.items()], "boats": [[boat.to_json(), position] for boat, position in self._boats.items()],
"bombs": self._bombs.tolist() "bombs": self._bombs.tolist()
} }
@ -135,8 +135,8 @@ class Board:
@classmethod @classmethod
def from_json(cls, json_: dict) -> "Board": def from_json(cls, json_: dict) -> "Board":
return Board( return Board(
width=json_["width"], rows=json_["columns"],
height=json_["height"], columns=json_["rows"],
boats={Boat.from_json(boat_json): tuple(position) for boat_json, position in json_["boats"]}, boats={Boat.from_json(boat_json): tuple(position) for boat_json, position in json_["boats"]},
bombs=np.array(json_["bombs"], dtype=np.bool_) bombs=np.array(json_["bombs"], dtype=np.bool_)
) )

View file

@ -0,0 +1,2 @@
from .Boat import Boat
from .Board import Board

View file

@ -3,7 +3,8 @@ from typing import TYPE_CHECKING
import pyglet import pyglet
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui.widget import GameGrid, Text, Input, Button from source.gui import widget
from source import core
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
@ -21,10 +22,20 @@ class Game(Scene):
texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png") texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png")
texture_button_click = pyglet.image.load("./assets/image/button/clicking.png") texture_button_click = pyglet.image.load("./assets/image/button/clicking.png")
texture_game_background = pyglet.image.load("./assets/image/background/game.png")
texture_grid_background = pyglet.image.load("./assets/image/grid/background.png") texture_grid_background = pyglet.image.load("./assets/image/grid/background.png")
self.background = self.add_widget(
widget.Image,
x=0, y=0, width=1.0, height=1.0,
image=texture_game_background,
)
self.grid_ally = self.add_widget( self.grid_ally = self.add_widget(
GameGrid, widget.GameGrid,
x=75, y=0.25, width=0.35, height=0.5, x=75, y=0.25, width=0.35, height=0.5,
@ -33,7 +44,7 @@ class Game(Scene):
) )
self.grid_enemy = self.add_widget( self.grid_enemy = self.add_widget(
GameGrid, widget.GameGrid,
x=lambda widget: widget.scene.window.width - 75 - widget.width, y=0.25, width=0.35, height=0.5, x=lambda widget: widget.scene.window.width - 75 - widget.width, y=0.25, width=0.35, height=0.5,
@ -42,9 +53,9 @@ class Game(Scene):
) )
self.name_ally = self.add_widget( self.name_ally = self.add_widget(
Text, widget.Text,
x=0.35, y=0.9, x=0.27, y=0.995,
text="Raphael", text="Raphael",
font_size=20, font_size=20,
@ -52,9 +63,9 @@ class Game(Scene):
) )
self.name_enemy = self.add_widget( self.name_enemy = self.add_widget(
Text, widget.Text,
x=0.65, y=0.9, x=0.73, y=0.995,
text="Leo", text="Leo",
font_size=20, font_size=20,
@ -62,36 +73,37 @@ class Game(Scene):
) )
self.score_ally = self.add_widget( self.score_ally = self.add_widget(
Text, widget.Text,
x=0.48, y=0.9, x=0.44, y=0.995,
text="7", text="7",
font_size=25, font_size=25,
anchor_x="right", anchor_y="center" anchor_x="center", anchor_y="center"
) )
self.score_enemy = self.add_widget( self.score_enemy = self.add_widget(
Text, widget.Text,
x=0.52, y=0.9, x=0.56, y=0.995,
text="5", text="5",
font_size=25, font_size=25,
anchor_x="left", anchor_y="center" anchor_x="center", anchor_y="center"
) )
self.chat_log = self.add_widget( self.chat_log = self.add_widget(
Text, widget.Text,
x=10, y=70, width=0.5, height=200, x=10, y=70, width=0.5,
text="FARAPHEL - HELLO BILLY\nLEO - HELLO BOLLO", text="FARAPHEL - HELLO BILLY\nLEO - HELLO BOLLO",
anchor_x="left", anchor_y="baseline",
multiline=True multiline=True
) )
self.chat_input = self.add_widget( self.chat_input = self.add_widget(
Input, widget.Input,
x=10, y=10, width=0.5, height=50, x=10, y=10, width=0.5, height=50,
@ -101,26 +113,35 @@ class Game(Scene):
) )
self.button_save = self.add_widget( self.button_save = self.add_widget(
Button, widget.Button,
x=0.7, y=0, width=0.15, height=0.1, x=0.7, y=0, width=0.15, height=0.1,
label_text="Sauvegarder",
texture_normal=texture_button_normal, texture_normal=texture_button_normal,
texture_hover=texture_button_hover, texture_hover=texture_button_hover,
texture_click=texture_button_click texture_click=texture_button_click
) )
self.button_quit = self.add_widget( self.button_quit = self.add_widget(
Button, widget.Button,
x=0.85, y=0, width=0.15, height=0.1, x=0.85, y=0, width=0.15, height=0.1,
label_text="Quitter",
texture_normal=texture_button_normal, texture_normal=texture_button_normal,
texture_hover=texture_button_hover, texture_hover=texture_button_hover,
texture_click=texture_button_click texture_click=texture_button_click
) )
self.board_ally = core.Board(rows=8, columns=8)
self.board_enemy = core.Board(rows=8, columns=8)
def on_draw(self): def on_draw(self):
self.background.draw()
self.grid_ally.draw() self.grid_ally.draw()
self.grid_enemy.draw() self.grid_enemy.draw()

View file

@ -2,9 +2,8 @@ from typing import TYPE_CHECKING
import pyglet import pyglet
from source.gui.scene import RoomJoin, RoomCreate, Settings, Game
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui.widget import Image, Text, Button from source.gui import widget, scene
from source.gui.widget.debug import FPSDisplay from source.gui.widget.debug import FPSDisplay
if TYPE_CHECKING: if TYPE_CHECKING:
@ -21,20 +20,20 @@ class MainMenu(Scene):
texture_button_click = pyglet.image.load("./assets/image/button/clicking.png") texture_button_click = pyglet.image.load("./assets/image/button/clicking.png")
self.background = self.add_widget( self.background = self.add_widget(
Image, widget.Image,
x=0.0, y=0.0, width=1.0, height=1.0, x=0.0, y=0.0, width=1.0, height=1.0,
image=texture_background image=texture_background
) )
self.title = self.add_widget( self.title = self.add_widget(
Text, widget.Text,
x=50, y=0.85, x=50, y=0.85,
text="Bataille Navale", text="Bataille Navale",
font_size=50 font_size=50
) )
self.game_create = self.add_widget( self.game_create = self.add_widget(
Button, widget.Button,
x=50, y=0.45, width=0.3, height=0.1, x=50, y=0.45, width=0.3, height=0.1,
@ -46,10 +45,10 @@ class MainMenu(Scene):
texture_click=texture_button_click texture_click=texture_button_click
) )
self.game_create.on_release = lambda *_: self.window.set_scene(RoomCreate) self.game_create.on_release = lambda *_: self.window.set_scene(scene.RoomCreate)
self.game_join = self.add_widget( self.game_join = self.add_widget(
Button, widget.Button,
x=50, y=0.3, width=0.3, height=0.1, x=50, y=0.3, width=0.3, height=0.1,
@ -61,10 +60,10 @@ class MainMenu(Scene):
texture_click=texture_button_click texture_click=texture_button_click
) )
self.game_join.on_release = lambda *_: self.window.set_scene(RoomJoin) self.game_join.on_release = lambda *_: self.window.set_scene(scene.RoomJoin)
self.settings = self.add_widget( self.settings = self.add_widget(
Button, widget.Button,
x=50, y=0.15, width=0.3, height=0.1, x=50, y=0.15, width=0.3, height=0.1,
@ -76,8 +75,7 @@ class MainMenu(Scene):
texture_click=texture_button_click texture_click=texture_button_click
) )
# self.settings.on_release = lambda *_: self.window.set_scene(Settings) self.settings.on_release = lambda *_: self.window.set_scene(scene.Settings)
self.settings.on_release = lambda *_: self.window.set_scene(Game)
self.fps_display = self.add_widget(FPSDisplay, color=(255, 255, 255, 180)) self.fps_display = self.add_widget(FPSDisplay, color=(255, 255, 255, 180))

View file

@ -5,7 +5,7 @@ import requests
from source import network from source import network
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui.widget import Text, Button from source.gui import widget
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
@ -25,7 +25,7 @@ class RoomCreate(Scene):
texture_button_click = pyglet.image.load("./assets/image/button/clicking.png") texture_button_click = pyglet.image.load("./assets/image/button/clicking.png")
self.back = self.add_widget( self.back = self.add_widget(
Button, widget.Button,
x=20, y=20, width=0.2, height=0.1, x=20, y=20, width=0.2, height=0.1,
label_text="Retour", label_text="Retour",
@ -39,7 +39,7 @@ class RoomCreate(Scene):
self.back.on_release = lambda *_: self.window.set_scene(MainMenu) self.back.on_release = lambda *_: self.window.set_scene(MainMenu)
self.label_ip = self.add_widget( self.label_ip = self.add_widget(
Text, widget.Text,
x=0.5, y=0.55, x=0.5, y=0.55,
anchor_x="center", anchor_y="center", anchor_x="center", anchor_y="center",
text=f"Votre IP - {ip_address}:{port}", text=f"Votre IP - {ip_address}:{port}",
@ -47,13 +47,13 @@ class RoomCreate(Scene):
) )
self.description = self.add_widget( self.description = self.add_widget(
Text, widget.Text,
x=0.5, y=0.45, x=0.5, y=0.45,
anchor_x="center", anchor_y="center", anchor_x="center", anchor_y="center",
text="En attente d'un second joueur..." text="En attente d'un second joueur..."
) )
self.thread = network.Host(window=self.window, daemon=True) self.thread = network.Host(window=self.window, daemon=True, username="Host")
self.thread.start() self.thread.start()
def on_draw(self): def on_draw(self):

View file

@ -4,7 +4,7 @@ import pyglet
from source import network from source import network
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui.widget import Input, Button from source.gui import widget
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
@ -23,7 +23,7 @@ class RoomJoin(Scene):
texture_input_error = pyglet.image.load("./assets/image/input/error.png") texture_input_error = pyglet.image.load("./assets/image/input/error.png")
self.back = self.add_widget( self.back = self.add_widget(
Button, widget.Button,
x=20, y=20, width=0.2, height=0.1, x=20, y=20, width=0.2, height=0.1,
label_text="Retour", label_text="Retour",
@ -37,7 +37,7 @@ class RoomJoin(Scene):
self.back.on_release = lambda *_: self.window.set_scene(MainMenu) self.back.on_release = lambda *_: self.window.set_scene(MainMenu)
self.entry_ip = self.add_widget( self.entry_ip = self.add_widget(
Input, widget.Input,
x=0.4, y=0.5, width=0.13, height=0.1, x=0.4, y=0.5, width=0.13, height=0.1,
regex=r"\d{1,3}(\.\d{1,3}){3}", regex=r"\d{1,3}(\.\d{1,3}){3}",
@ -48,7 +48,7 @@ class RoomJoin(Scene):
) )
self.entry_port = self.add_widget( self.entry_port = self.add_widget(
Input, widget.Input,
x=0.53, y=0.5, width=0.07, height=0.1, x=0.53, y=0.5, width=0.07, height=0.1,
regex=r"\d{0,5}", regex=r"\d{0,5}",
@ -59,7 +59,7 @@ class RoomJoin(Scene):
) )
self.connect = self.add_widget( self.connect = self.add_widget(
Button, widget.Button,
x=0.4, y=0.4, width=0.2, height=0.1, x=0.4, y=0.4, width=0.2, height=0.1,
label_text="Se connecter", label_text="Se connecter",
@ -72,7 +72,8 @@ class RoomJoin(Scene):
self.connect.on_release = lambda *_: network.Client( self.connect.on_release = lambda *_: network.Client(
window=self.window, window=self.window,
ip_address=self.entry_ip.text, ip_address=self.entry_ip.text,
daemon=True daemon=True,
username="Client"
).start() ).start()
def on_draw(self): def on_draw(self):

View file

@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
import pyglet import pyglet
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui.widget import Checkbox, Scroller, Button, GameGrid from source.gui import widget
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
@ -26,7 +26,7 @@ class Settings(Scene):
texture_grid_background = pyglet.image.load("./assets/image/grid/background.png") texture_grid_background = pyglet.image.load("./assets/image/grid/background.png")
self.back = self.add_widget( self.back = self.add_widget(
Button, widget.Button,
x=20, y=20, width=0.2, height=0.1, x=20, y=20, width=0.2, height=0.1,
label_text="Retour", label_text="Retour",
@ -40,7 +40,7 @@ class Settings(Scene):
self.back.on_release = lambda *_: self.window.set_scene(MainMenu) self.back.on_release = lambda *_: self.window.set_scene(MainMenu)
self.checkbox = self.add_widget( self.checkbox = self.add_widget(
Checkbox, widget.Checkbox,
x=0.45, y=0.45, width=0.1, height=0.1, x=0.45, y=0.45, width=0.1, height=0.1,
@ -49,7 +49,7 @@ class Settings(Scene):
) )
self.scroller = self.add_widget( self.scroller = self.add_widget(
Scroller, widget.Scroller,
x=0.3, y=0.2, width=0.3, height=0.1, x=0.3, y=0.2, width=0.3, height=0.1,
@ -59,17 +59,7 @@ class Settings(Scene):
text_transform=lambda value: round(value, 2), text_transform=lambda value: round(value, 2),
) )
self.grid = self.add_widget(
GameGrid,
x=0.5, y=0.5, width=0.4, height=0.4,
rows=10, columns=5,
texture_background=texture_grid_background,
)
def on_draw(self): def on_draw(self):
self.checkbox.draw() self.checkbox.draw()
self.scroller.draw() self.scroller.draw()
self.back.draw() self.back.draw()
self.grid.draw()

View file

@ -21,7 +21,7 @@ class Scene(ABC):
# Widget Managing # Widget Managing
def add_widget(self, widget_class: Type["Widget"], **widget_kwargs) -> "Widget": def add_widget(self, widget_class: Type["Widget"], **widget_kwargs):
""" """
Add a widget to the scene. Add a widget to the scene.
:widget_class: the class of the widget to add. :widget_class: the class of the widget to add.

View file

@ -1,16 +1,22 @@
import socket import socket
from threading import Thread from threading import Thread
from typing import TYPE_CHECKING
import pyglet.clock import pyglet.clock
from source.gui.scene import Game from source.gui import scene
if TYPE_CHECKING:
from source.gui.window import Window
class Client(Thread): class Client(Thread):
def __init__(self, window: "Window", ip_address: str, port: int = 52321, **kw): def __init__(self, window: "Window", username: str, ip_address: str, port: int = 52321, **kw):
super().__init__(**kw) super().__init__(**kw)
self.window = window self.window = window
self.username = username
self.ip_address = ip_address self.ip_address = ip_address
self.port = port self.port = port
@ -22,5 +28,5 @@ class Client(Thread):
print(f"[Client] Connecté avec {s}") print(f"[Client] Connecté avec {s}")
pyglet.clock.schedule_once(lambda dt: self.window.set_scene(Game), 0) pyglet.clock.schedule_once(lambda dt: self.window.set_scene(scene.Game), 0)

View file

@ -1,16 +1,22 @@
import socket import socket
from threading import Thread from threading import Thread
from typing import TYPE_CHECKING
import pyglet import pyglet
from source.gui.scene import Game from source.gui import scene
if TYPE_CHECKING:
from source.gui.window import Window
class Host(Thread): class Host(Thread):
def __init__(self, window: "Window", port: int = 52321, **kw): def __init__(self, window: "Window", username: str, port: int = 52321, **kw):
super().__init__(**kw) super().__init__(**kw)
self.window = window self.window = window
self.username = username
self.port = port self.port = port
def run(self) -> None: def run(self) -> None:
@ -23,4 +29,6 @@ class Host(Thread):
print(f"[Serveur] Connecté avec {address}") print(f"[Serveur] Connecté avec {address}")
pyglet.clock.schedule_once(lambda dt: self.window.set_scene(Game), 0) pyglet.clock.schedule_once(lambda dt: self.window.set_scene(scene.Game), 0)