added the turn system
This commit is contained in:
parent
bda05d3f8e
commit
75d7c407c1
8 changed files with 29 additions and 12 deletions
11
NOTE.md
11
NOTE.md
|
@ -8,6 +8,12 @@ A faire :
|
||||||
- Voir si les event listener intégré à pyglet sont plus pratique que l'event propagation
|
- Voir si les event listener intégré à pyglet sont plus pratique que l'event propagation
|
||||||
- Documenter
|
- Documenter
|
||||||
|
|
||||||
|
- Ecran de victoire
|
||||||
|
- Affichage des coups de l'opposant
|
||||||
|
- Faire marcher le score
|
||||||
|
- Faire marcher le tchat
|
||||||
|
- Sauvegarde / Quitter
|
||||||
|
|
||||||
Bug :
|
Bug :
|
||||||
- /
|
- /
|
||||||
|
|
||||||
|
@ -15,10 +21,5 @@ Autre :
|
||||||
- Tester sur Linux
|
- Tester sur Linux
|
||||||
|
|
||||||
|
|
||||||
A expliquer :
|
|
||||||
- in_pyglet_context
|
|
||||||
- Packet
|
|
||||||
|
|
||||||
|
|
||||||
Bonus ultime :
|
Bonus ultime :
|
||||||
- Envoyer la texture de la grille à l'adversaire (???)
|
- Envoyer la texture de la grille à l'adversaire (???)
|
|
@ -11,4 +11,4 @@ class BombState(Enum):
|
||||||
SUNKEN = 2 # the bomb touched the last part of a boat
|
SUNKEN = 2 # the bomb touched the last part of a boat
|
||||||
WON = 3 # the bomb sunk the last boat
|
WON = 3 # the bomb sunk the last boat
|
||||||
|
|
||||||
ERROR = 10 # the bomb could not be placed
|
ERROR = -1 # the bomb could not be placed
|
||||||
|
|
|
@ -18,6 +18,10 @@ class Game(Scene):
|
||||||
def __init__(self, window: "Window", connection: socket.socket, **kwargs):
|
def __init__(self, window: "Window", connection: socket.socket, **kwargs):
|
||||||
super().__init__(window, **kwargs)
|
super().__init__(window, **kwargs)
|
||||||
|
|
||||||
|
self.my_turn = False
|
||||||
|
self.boat_ready_ally = False
|
||||||
|
self.boat_ready_enemy = False
|
||||||
|
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
|
|
||||||
self.batch_label = pyglet.graphics.Batch()
|
self.batch_label = pyglet.graphics.Batch()
|
||||||
|
@ -55,6 +59,7 @@ class Game(Scene):
|
||||||
)
|
)
|
||||||
|
|
||||||
def board_ally_ready():
|
def board_ally_ready():
|
||||||
|
self.boat_ready_ally = True
|
||||||
PacketBoatPlaced().send_connection(connection)
|
PacketBoatPlaced().send_connection(connection)
|
||||||
|
|
||||||
self.grid_ally.add_listener("on_all_boats_placed", board_ally_ready)
|
self.grid_ally.add_listener("on_all_boats_placed", board_ally_ready)
|
||||||
|
@ -75,7 +80,10 @@ class Game(Scene):
|
||||||
)
|
)
|
||||||
|
|
||||||
def board_enemy_bomb(cell: Point2D):
|
def board_enemy_bomb(cell: Point2D):
|
||||||
|
if not (self.boat_ready_ally and self.boat_ready_enemy): return
|
||||||
|
if not self.my_turn: return
|
||||||
PacketBombPlaced(position=cell).send_connection(connection)
|
PacketBombPlaced(position=cell).send_connection(connection)
|
||||||
|
self.my_turn = False
|
||||||
|
|
||||||
self.grid_enemy.add_listener("on_request_place_bomb", board_enemy_bomb)
|
self.grid_enemy.add_listener("on_request_place_bomb", board_enemy_bomb)
|
||||||
|
|
||||||
|
|
|
@ -30,4 +30,4 @@ class Client(StoppableThread):
|
||||||
|
|
||||||
print(f"[Client] Connecté avec {connection}")
|
print(f"[Client] Connecté avec {connection}")
|
||||||
|
|
||||||
game_network(self, self.window, connection)
|
game_network(self, self.window, connection, False)
|
||||||
|
|
|
@ -39,4 +39,4 @@ class Host(StoppableThread):
|
||||||
|
|
||||||
print(f"[Serveur] Connecté avec {address}")
|
print(f"[Serveur] Connecté avec {address}")
|
||||||
|
|
||||||
game_network(self, self.window, connection)
|
game_network(self, self.window, connection, True)
|
||||||
|
|
|
@ -12,7 +12,7 @@ from source.utils import StoppableThread
|
||||||
from source.utils.thread import in_pyglet_context
|
from source.utils.thread import in_pyglet_context
|
||||||
|
|
||||||
|
|
||||||
def game_network(thread: "StoppableThread", window: "Window", connection: socket.socket):
|
def game_network(thread: "StoppableThread", window: "Window", connection: socket.socket, host: bool):
|
||||||
"""
|
"""
|
||||||
Run the networking to make the game work and react with the other player
|
Run the networking to make the game work and react with the other player
|
||||||
:param thread: the thread where this function is called.
|
:param thread: the thread where this function is called.
|
||||||
|
@ -21,6 +21,7 @@ def game_network(thread: "StoppableThread", window: "Window", connection: socket
|
||||||
"""
|
"""
|
||||||
|
|
||||||
game_scene = in_pyglet_context(window.set_scene, scene.Game, connection=connection)
|
game_scene = in_pyglet_context(window.set_scene, scene.Game, connection=connection)
|
||||||
|
game_scene.my_turn = host
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data: Any = Packet.from_connection(connection)
|
data: Any = Packet.from_connection(connection)
|
||||||
|
@ -34,6 +35,7 @@ def game_network(thread: "StoppableThread", window: "Window", connection: socket
|
||||||
print(data.message)
|
print(data.message)
|
||||||
|
|
||||||
case packet.PacketBoatPlaced:
|
case packet.PacketBoatPlaced:
|
||||||
|
game_scene.boat_ready_enemy = True
|
||||||
print("adversaire à posé ses bateaux")
|
print("adversaire à posé ses bateaux")
|
||||||
|
|
||||||
case packet.PacketBombPlaced:
|
case packet.PacketBombPlaced:
|
||||||
|
@ -44,9 +46,16 @@ def game_network(thread: "StoppableThread", window: "Window", connection: socket
|
||||||
|
|
||||||
packet.PacketBombState(position=data.position, bomb_state=bomb_state).send_connection(connection)
|
packet.PacketBombState(position=data.position, bomb_state=bomb_state).send_connection(connection)
|
||||||
|
|
||||||
|
touched = bomb_state in [BombState.TOUCHED, BombState.SUNKEN, BombState.WON, BombState.ERROR]
|
||||||
|
game_scene.my_turn = not touched
|
||||||
|
|
||||||
case packet.PacketBombState:
|
case packet.PacketBombState:
|
||||||
if data.bomb_state is BombState.ERROR: continue
|
print(data.bomb_state)
|
||||||
|
if data.bomb_state is BombState.ERROR:
|
||||||
|
game_scene.my_turn = True
|
||||||
|
continue
|
||||||
|
|
||||||
touched = data.bomb_state in [BombState.TOUCHED, BombState.SUNKEN, BombState.WON]
|
touched = data.bomb_state in [BombState.TOUCHED, BombState.SUNKEN, BombState.WON]
|
||||||
|
game_scene.my_turn = touched
|
||||||
|
|
||||||
in_pyglet_context(game_scene.grid_enemy.place_bomb, data.position, touched)
|
in_pyglet_context(game_scene.grid_enemy.place_bomb, data.position, touched)
|
||||||
|
|
|
@ -13,7 +13,7 @@ class PacketChat(Packet):
|
||||||
|
|
||||||
packet_size: int = 256
|
packet_size: int = 256
|
||||||
|
|
||||||
def to_bytes(self):
|
def to_bytes(self) -> bytes:
|
||||||
return self.message.encode("utf-8")
|
return self.message.encode("utf-8")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -83,4 +83,3 @@ class Packet(ABC):
|
||||||
"""
|
"""
|
||||||
subcls = cls.cls_from_connection(connection)
|
subcls = cls.cls_from_connection(connection)
|
||||||
return None if subcls is None else subcls.instance_from_connection(connection)
|
return None if subcls is None else subcls.instance_from_connection(connection)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue