This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
88 lines (77 loc) · 3.87 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Structural process of the game
from board import board as classBoard
from player import player as classPlayer, enemy as classEnemy, ai as classAI
from PyQt6.QtCore import QThread, QThreadPool
from websocketClient import websocketClient
# from ui.gameMenu import gameMenu
from ui.mainMenu import mainMenu
from ui.graphics.boatImage import icons
class game():
"""Structural process of the game"""
def __init__(self):
self.players=[] # Track players for offline mode
self.gameMenu = mainMenu(self) # Start ui
def __str__(self):
"""Return game and its players as formatted string"""
formattedString = f"{self.__class__.__name__}\n"
for player in self.players:
formattedString += f"{str(player)}\n"
return formattedString
class boardBlueprint(classBoard):
"""Create a board with game specific information"""
def __init__(self, game, filler):
"""Extend the board class with game specific information"""
self.game=game
super().__init__(self.game.gameDetails["max boats"], self.game.gameDetails["board size"], filler)
def startGame(self, gameDetails):
"""Create game"""
self.gameDetails=gameDetails # Save details that were passed from the ui
if self.gameDetails["gameType"] == "online":
# Create player, enemies will be set up by the server
player = classPlayer(self, self.gameDetails["name"], self.gameDetails["room"], '0')
player.webSocket = websocketClientThread(player, self.gameDetails["address"], self.gameDetails["port"], player.roomName, player.name)
player.server = player.webSocket.webSocketClient
self.players.append(player)
elif self.gameDetails["gameType"] == "offline":
pass
if not hasattr(self, "boatIcons"):
self.boatIcons = iconGeneratorThread(self) # Generate boat icons in background
else:
print("Boat icons already generated")
pass
def autoSetup(self):
"""Setup the game automatically"""
pass
def play(self):
"""Play the game"""
pass
class websocketClientThread(QThread):
def __init__(self, *args):
super().__init__()
self.player = args[0]
self.threadPool = QThreadPool().globalInstance()
self.webSocketClient = websocketClient(*args)
self.webSocketClient.signals.opened.connect(lambda: self.player.createPlaceholder())
self.webSocketClient.signals.phase.connect(lambda data: self.player.setPhase(data))
self.webSocketClient.signals.shipPlaced.connect(lambda x, y, length, direction: self.player.shipPlaced(x, y, length, direction == "VERTICAL"))
self.webSocketClient.signals.playerChanged.connect(lambda player: self.player.playerChanged(player))
self.webSocketClient.signals.shotFired.connect(lambda x, y, player, result, shipCoordinates: self.player.shotFired(x, y, player, result, shipCoordinates))
self.webSocketClient.signals.message.connect(lambda message: print(message))
self.webSocketClient.signals.gameError.connect(lambda e: self.player.websocketError(e))
self.webSocketClient.signals.error.connect(lambda e: print(e))
self.webSocketClient.signals.closed.connect(lambda code, msg: self.player.websocketClosed(code, msg))
self.threadPool.start(self.webSocketClient)
class iconGeneratorThread(QThread):
def __init__(self, game):
super().__init__()
self.game = game
self.threadPool = QThreadPool().globalInstance()
self.iconGenerator = icons(self.game)
self.iconGenerator.signals.finished.connect(lambda: print("Icons generated"))
self.threadPool.start(self.iconGenerator)
if __name__ == "__main__":
import sys
from PyQt6.QtWidgets import QApplication
app = QApplication(sys.argv)
game = game()
sys.exit(app.exec())