added server communication between client and server

This commit is contained in:
2024-01-07 19:26:41 +01:00
parent fab79061ea
commit 6adea1730e
31 changed files with 173 additions and 129 deletions

View File

@@ -7,6 +7,8 @@ from Classes.System.Components.InputHandler import InputHandler
from Classes.Game.World import World
from Classes.System.Network.NetworkManager import NetworkManager
from Classes.Game.Events.Login import Login
from Classes.Game.Events.PlaceCard import PlaceCard
from Classes.Game.Player import Player
class App:
@@ -24,9 +26,15 @@ class App:
self.__myFont = pygame.font.SysFont('Comic Sans MS', 30)
self.__window = Window(width=width, height=height, title=title)
self.__inputHandler = InputHandler()
self.__networkManager("127.0.0.1", "54322", "54323")
Login(self.__networkManager) # will login to the server
try:
self.__networkManager = NetworkManager("127.0.0.1", "54322", "54323")
Login(self.__networkManager) # will login to the server
except Exception as e:
print(f"failed to login due to error {e}")
print("server connection failed or got refused")
pass
# game word
self.__world = World(self.__window.getScreen())
@@ -38,12 +46,12 @@ class App:
# create sprite groups
# todo: remove these and let server handle card creation instead
# blocker: server - client communication [WIP]
self.__world.spawnCard("Assets/Cards/MonsterCards/testmonstercard/", (pygame.Vector2(500, 1050), self.__inputHandler))
self.__world.spawnCard("Assets/Cards/MonsterCards/testmonstercard/", (pygame.Vector2(600, 1050), self.__inputHandler))
self.__world.spawnCard("Assets/Cards/MonsterCards/testmonstercard/", (pygame.Vector2(700, 1050), self.__inputHandler))
self.__world.spawnCard("Assets/Cards/MonsterCards/testmonstercard/", (pygame.Vector2(800, 1050), self.__inputHandler))
self.__world.spawnCard("Assets/Cards/MonsterCards/testmonstercard/", (pygame.Vector2(900, 1050), self.__inputHandler))
self.__world.spawnCard("Assets/Cards/MonsterCards/testmonstercard/", (pygame.Vector2(1000, 1050), self.__inputHandler))
self.__world.spawnMonsterCard("Assets/Cards/1/", pygame.Vector2(500, 1050), self.__inputHandler, Player(1000, 0, "test"))
self.__world.spawnMonsterCard("Assets/Cards/1/", pygame.Vector2(600, 1050), self.__inputHandler, Player(1000, 0, "test"))
self.__world.spawnMonsterCard("Assets/Cards/1/", pygame.Vector2(700, 1050), self.__inputHandler, Player(1000, 0, "test"))
self.__world.spawnMonsterCard("Assets/Cards/1/", pygame.Vector2(800, 1050), self.__inputHandler, Player(1000, 0, "test"))
self.__world.spawnMonsterCard("Assets/Cards/1/", pygame.Vector2(900, 1050), self.__inputHandler, Player(1000, 0, "test"))
self.__world.spawnMonsterCard("Assets/Cards/1/", pygame.Vector2(1000, 1050), self.__inputHandler, Player(1000, 0, "test"))
while self.__running:
self.__clock.tick(self.__FPS)
@@ -57,7 +65,8 @@ class App:
self.__world.getCards().update()
# draw groups
self.__window.drawSpriteGroup(self.cards)
self.__window.drawSpriteGroup(self.__world.getCards())
self.__window.drawSpriteGroup(self.__world.getHandCards())
# event handler
self.handleEvent(pygame.event.get())
@@ -69,7 +78,7 @@ class App:
def handleEvent(self, events):
# TODO: fix bug that stacks cards when dragging them around
selectedCard = None
for event in events:
if event.type == pygame.QUIT:
self.onCleanup()
@@ -95,7 +104,9 @@ class App:
card.rect.center = field.rect.center
field.image = card.image.copy()
card.setDragging(False)
elif self.__inputHandler.getPressed()[pygame.K_SPACE]:
print("card spawned")
self.__world.spawnMonsterCard("Assets/Cards/MonsterCards/testmonstercard/", self.__inputHandler.getMousePos(), self.__inputHandler)
elif event.type == pygame.MOUSEBUTTONUP:
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_pos = pygame.Vector2(mouse_x, mouse_y)
@@ -104,6 +115,7 @@ class App:
card.setDragging(False)
# TODO: send place card event to server
# resets the currently selected card in order to prevent it getting moved
PlaceCard(self.__networkManager, card) # tells the server that the player placed this card
if not card == None:
card = None