added old code, fixed it (mostly), merged some new code to the old code, made it mostly runnable
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
import pygame
|
||||
from pygame.locals import *
|
||||
|
||||
from Classes.Game.Cards.MonsterCard import MonsterCard
|
||||
from Classes.System.Components.Window import Window
|
||||
from Classes.System.Components.InputHandler import InputHandler
|
||||
from Classes.Game.World import World
|
||||
from Classes.System.Network.TCPClient import TCPClient
|
||||
from Classes.Game.Events.Login import Login
|
||||
from Classes.Game.Events.PlaceCard import PlaceCard
|
||||
from Classes.Game.Player import Player
|
||||
from Classes.Game.Cards.Card import Card
|
||||
from Classes.System.Utils.Path import PathUtil
|
||||
|
||||
class App:
|
||||
|
||||
__window:Window
|
||||
__running:bool = True
|
||||
__FPS = 60
|
||||
__clock = pygame.time.Clock()
|
||||
__myFont:pygame.font
|
||||
__world:World
|
||||
__inputHandler: InputHandler
|
||||
__tcpClient: TCPClient
|
||||
|
||||
def __init__(self, width:int=1920, height:int=1080, title:str="default title"):
|
||||
pygame.font.init()
|
||||
self.__myFont = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
self.__window = Window(width=width, height=height, title=title)
|
||||
self.__inputHandler = InputHandler()
|
||||
self.selectedCard = None
|
||||
|
||||
# game word
|
||||
self.__world = World(self.__window.getScreen())
|
||||
|
||||
try:
|
||||
self.__tcpClient = TCPClient("127.0.0.1", "54322", self.__world, self.__inputHandler)
|
||||
Login(self.__tcpClient) # 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
|
||||
|
||||
self.startGameLoop()
|
||||
self.onCleanup()
|
||||
|
||||
def startGameLoop(self):
|
||||
|
||||
# create sprite groups
|
||||
# todo: remove these and let server handle card creation instead
|
||||
# blocker: server - client communication [WIP]
|
||||
self.__world.spawnCard(PathUtil.getAbsolutePathTo("Assets/Cards/1/"), pygame.Vector2(500, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard(PathUtil.getAbsolutePathTo("Assets/Cards/1/"), pygame.Vector2(600, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard(PathUtil.getAbsolutePathTo("Assets/Cards/1/"), pygame.Vector2(700, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard(PathUtil.getAbsolutePathTo("Assets/Cards/1/"), pygame.Vector2(800, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard(PathUtil.getAbsolutePathTo("Assets/Cards/1/"), pygame.Vector2(900, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard(PathUtil.getAbsolutePathTo("Assets/Cards/1/"), pygame.Vector2(1000, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
|
||||
while self.__running:
|
||||
self.__clock.tick(self.__FPS)
|
||||
|
||||
self.__window.getScreen().fill((0,0,0))
|
||||
|
||||
# render world
|
||||
self.__window.drawWorld(self.__world)
|
||||
|
||||
# updates all cards inside the cards Spritegroup at each step the gameloops does
|
||||
self.__world.getCards().update()
|
||||
self.__world.getHandCards().update()
|
||||
|
||||
# draw groups
|
||||
self.__window.drawSpriteGroup(self.__world.getCards())
|
||||
self.__window.drawSpriteGroup(self.__world.getHandCards())
|
||||
|
||||
# event handler
|
||||
self.handleEvent(pygame.event.get())
|
||||
|
||||
# emits update to the game
|
||||
pygame.display.update()
|
||||
|
||||
# handles incoming event queue
|
||||
def handleEvent(self, events):
|
||||
# TODO: fix bug that stacks cards when dragging them around
|
||||
try:
|
||||
for event in events:
|
||||
if event.type == pygame.QUIT:
|
||||
self.onCleanup()
|
||||
elif self.__inputHandler.getMousePressed()[0]:
|
||||
for card in self.__world.getCards():
|
||||
if card.rect.collidepoint(self.__inputHandler.getMousePos()):
|
||||
#self.__logger.info(f"dragging card {card}")
|
||||
self.selectedCard = card
|
||||
|
||||
# failsafe to prevent NoneType errors
|
||||
if self.selectedCard != None:
|
||||
#self.__logger.info(f"working with card: {self.selectedCard}")
|
||||
self.selectedCard.setDragging(True)
|
||||
elif event.type == pygame.MOUSEBUTTONUP:
|
||||
print("left mousebutton")
|
||||
if event.button == 1:
|
||||
if self.selectedCard != None:
|
||||
self.selectedCard.setDragging(False)
|
||||
print(self.selectedCard)
|
||||
for field in self.__world.getBoardFields():
|
||||
print(f"checking field {field} is field mathinc? {field.getRect().collidepoint(self.__inputHandler.getMousePos())}")
|
||||
if field.getRect().collidepoint(self.__inputHandler.getMousePos()):
|
||||
print(f"is position valid? {field.getSide() == 'Player' and field.getType() == self.selectedCard.getType()} ")
|
||||
print(f"side {field.getSide()} fieldtype {field.getType()} card type {self.selectedCard.getType()}")
|
||||
|
||||
if field.getSide() == "Player" and field.getType() == self.selectedCard.getType():
|
||||
print(f"found field {field}")
|
||||
try:
|
||||
print(f"placing card {self.selectedCard} in field {field}")
|
||||
# snap card into the correct field
|
||||
self.selectedCard.rect.center = field.rect.center
|
||||
self.selectedCard.setDragging(False)
|
||||
|
||||
print(self.selectedCard)
|
||||
# TODO: adapt this into the new game engine version
|
||||
PlaceCard(self.__tcpClient, self.selectedCard, self.__world.getPlayer()) # tells te server that the player placed this card
|
||||
self.selectedCard = None
|
||||
except Exception as e:
|
||||
print(f"failed to place card on server due to error: {e}")
|
||||
|
||||
if self.selectedCard != None:
|
||||
self.selectedCard = None
|
||||
else:
|
||||
raise ValueError("selected card in event handler was found empty this should never happen!")
|
||||
pass
|
||||
except:
|
||||
pass
|
||||
|
||||
# sets the running state for the gameloop
|
||||
def setRunning(self, running:bool):
|
||||
self.__running = running
|
||||
|
||||
# ensures the gameloop stops running and the pygame instance is stopped properly
|
||||
def onCleanup(self):
|
||||
self.__running = False
|
||||
pygame.quit()
|
||||
@@ -0,0 +1,53 @@
|
||||
import pygame
|
||||
|
||||
from Classes.Game.BoardField import BoardField
|
||||
|
||||
class InputHandler:
|
||||
# returns pressed key
|
||||
@staticmethod
|
||||
def getPressed():
|
||||
return pygame.key.get_pressed()
|
||||
|
||||
# returns pressed key
|
||||
@staticmethod
|
||||
def getMousePressed():
|
||||
return pygame.mouse.get_pressed()
|
||||
|
||||
# takes in movement inputs and maps them to x and y axis
|
||||
@staticmethod
|
||||
def getInputAxis() -> tuple:
|
||||
xvel = 0
|
||||
yvel = 0
|
||||
|
||||
# construct x and y velocity input axis
|
||||
if InputHandler.getPressed()[pygame.K_a] or InputHandler.getPressed()[pygame.K_LEFT]:
|
||||
xvel = -1
|
||||
if InputHandler.getPressed()[pygame.K_d] or InputHandler.getPressed()[pygame.K_RIGHT]:
|
||||
xvel = 1
|
||||
if InputHandler.getPressed()[pygame.K_w] or InputHandler.getPressed()[pygame.K_UP]:
|
||||
yvel = -1
|
||||
if InputHandler.getPressed()[pygame.K_s] or InputHandler.getPressed()[pygame.K_DOWN]:
|
||||
yvel = 1
|
||||
|
||||
return tuple((xvel, yvel))
|
||||
|
||||
@staticmethod
|
||||
def getMousePos() -> pygame.Vector2:
|
||||
return pygame.Vector2(pygame.mouse.get_pos())
|
||||
|
||||
# get field under mousbutton
|
||||
@staticmethod
|
||||
def getMouseHover(mouse_pos: pygame.Vector2, world_card_width: int, world_card_height: int, board_fields: list) -> BoardField:
|
||||
x_pos = mouse_pos.x / world_card_width
|
||||
y_pos = mouse_pos.y / world_card_height
|
||||
|
||||
for field in board_fields:
|
||||
field_x = field.getPos().x
|
||||
field_y = field.getPos().y
|
||||
field_width = world_card_width # Annahme: Jedes Feld hat eine Breite von 1 Einheit
|
||||
field_height = world_card_height # Annahme: Jedes Feld hat eine Höhe von 1 Einheit
|
||||
|
||||
if field_x <= x_pos < field_x + field_width and field_y <= y_pos < field_y + field_height:
|
||||
return field
|
||||
|
||||
return None
|
||||
@@ -0,0 +1,47 @@
|
||||
import pygame
|
||||
|
||||
class Label:
|
||||
rect:pygame.rect
|
||||
image:pygame.image
|
||||
__screen:pygame.surface
|
||||
__text:str
|
||||
__pos:pygame.Vector2
|
||||
__font:pygame.font
|
||||
font:pygame.font
|
||||
__name:str
|
||||
|
||||
def __init__(self, name:str, screen:pygame.surface, text:str, pos:pygame.Vector2, size:float=20, color:str="white"):
|
||||
self.__font = pygame.font.SysFont("Arial", size)
|
||||
self.font = pygame.font.SysFont("Arial", size)
|
||||
self.image = self.font.render(text, 1, color)
|
||||
_, _, w, h = self.image.get_rect()
|
||||
self.__pos = pos
|
||||
self.rect = pygame.Rect(self.__pos.x, self.__pos.y, w, h)
|
||||
self.__screen = screen
|
||||
self.__text = text
|
||||
self.__name = name
|
||||
|
||||
def getText(self) -> str:
|
||||
return self.__text
|
||||
|
||||
def getFont(self) -> pygame.font:
|
||||
return self.__font
|
||||
|
||||
def getPos(self) -> pygame.Vector2:
|
||||
return self.__pos
|
||||
|
||||
def getName(self) -> str:
|
||||
return self.__name
|
||||
|
||||
def setText(self, newtext:str, color:str="white"):
|
||||
self.image = self.font.render(newtext, 1, color)
|
||||
|
||||
def setFont(self, font:pygame.font, size:float, color:str="white"):
|
||||
self.__font = pygame.font.SysFont(font, size)
|
||||
self.change_text(self.text, color)
|
||||
|
||||
def setPos(self, pos:pygame.Vector2):
|
||||
self.__pos = pos
|
||||
|
||||
def draw(self):
|
||||
self.__screen.blit(self.image, (self.rect))
|
||||
@@ -0,0 +1,54 @@
|
||||
import pygame
|
||||
from pygame.locals import *
|
||||
|
||||
from Classes.Game.BoardField import BoardField
|
||||
from Classes.Game.World import World
|
||||
|
||||
class Window:
|
||||
__width:int = 800
|
||||
__height:int = 600 # takes 80% of width which tranlates to 640
|
||||
__title:str = "python game engine"
|
||||
__screen:pygame.Surface
|
||||
__clock:pygame.time.Clock
|
||||
|
||||
def __init__(self, width:int=800, height:int=600, title:str="python game engine"):
|
||||
self.__width = width
|
||||
self.__height = height
|
||||
self.__title = title
|
||||
|
||||
pygame.init()
|
||||
|
||||
self.__screen = pygame.display.set_mode((self.__width, self.__height))
|
||||
self.__screen.fill((236, 240, 241)) # Hier liegt der Fehler, es muss eine Tuple übergeben werden
|
||||
pygame.display.set_caption(self.__title)
|
||||
|
||||
self.__clock = pygame.time.Clock()
|
||||
self.__framerate = 60 # Framerate auf 60 FPS festlegen
|
||||
|
||||
# set framerate (where the fuck is it?)
|
||||
def Render(self):
|
||||
# dear future me figure out what past me did!
|
||||
pass
|
||||
|
||||
def setWidth(self, width:int):
|
||||
self.__width = width
|
||||
|
||||
def setHeight(self, height:int):
|
||||
self.__height = height
|
||||
|
||||
def setTitle(self, title:str):
|
||||
self.__title = title
|
||||
|
||||
def getScreen(self) -> pygame.surface:
|
||||
return self.__screen
|
||||
|
||||
# draws a passed sprite group to the screen
|
||||
def drawSpriteGroup(self, group:pygame.sprite.Group):
|
||||
group.draw(self.__screen)
|
||||
|
||||
# draws a given group of rectangles onto the screen
|
||||
def drawWorld(self, world:World):
|
||||
for field in world.getBoardFields():
|
||||
pygame.draw.rect(self.__screen, field.getColor(), field.getRect())
|
||||
for label in world.getLabels():
|
||||
label.draw()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,40 @@
|
||||
import socket
|
||||
|
||||
import pygame
|
||||
|
||||
from Classes.Game.Events.PlaceCard import CardPlaced
|
||||
from Classes.System.Components.InputHandler import InputHandler
|
||||
from Classes.Game.World import World
|
||||
from Classes.Game.Events.GameStart import GameStart
|
||||
from Classes.Game.Player import Player
|
||||
from Classes.Game.Events.Login import LoginResponse
|
||||
|
||||
class TCPEventHandler:
|
||||
def __init__(self, socket:socket):
|
||||
self.tcp_socket = socket
|
||||
|
||||
def handleEvents(self, message, inputHandler:InputHandler, world:World):
|
||||
print(message)
|
||||
if message["event"] == "loginresponse":
|
||||
LoginResponse(message, world)
|
||||
pass
|
||||
elif message["event"] == "startgame":
|
||||
world.player.setMana(message["player"]["mana"])
|
||||
world.player.setHp(message["player"]["hp"])
|
||||
GameStart(world, message["player"]["hand"], inputHandler, world.getPlayer(), Player(message["enemy"]["hp"],message["enemy"]["mana"],message["enemy"]["name"], message["enemy"]["id"]))
|
||||
pass
|
||||
elif message["event"] == "placecard":
|
||||
CardPlaced(world, message["card"], message["type"], message["owner"], pygame.Vector2(int(message["x"]), int(message["y"]), inputHandler))
|
||||
pass
|
||||
elif message["event"] == "MoveCard":
|
||||
pass
|
||||
elif message["event"] == "RemoveCard":
|
||||
pass
|
||||
elif message["event"] == "AttackCard":
|
||||
pass
|
||||
elif message["event"] == "AttackPlayer":
|
||||
pass
|
||||
elif message["event"] == "ActivateEffectCard":
|
||||
pass
|
||||
elif message["event"] == "ActivateMonsterCard":
|
||||
pass
|
||||
@@ -0,0 +1,46 @@
|
||||
import json
|
||||
import socket
|
||||
import threading
|
||||
|
||||
from Classes.System.Network.EventHandler import TCPEventHandler
|
||||
from Classes.Game.World import World
|
||||
from Classes.System.Components.InputHandler import InputHandler
|
||||
|
||||
class TCPClient:
|
||||
def __init__(self, addr: str, port: str, world:World, inputHandler:InputHandler):
|
||||
self.addr = addr
|
||||
self.port = int(port)
|
||||
self.tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.eventHandler = TCPEventHandler(self.tcpSocket)
|
||||
self.world = world
|
||||
self.inputHandler = inputHandler
|
||||
|
||||
try:
|
||||
self.tcpSocket.connect((self.addr, self.port))
|
||||
except Exception as e:
|
||||
print(f"Error connecting TCP socket: {e}")
|
||||
|
||||
# Starten des Listener-Threads
|
||||
self.listen()
|
||||
|
||||
def send(self, message: dict):
|
||||
try:
|
||||
self.tcpSocket.sendall(json.dumps(message).encode())
|
||||
except Exception as e:
|
||||
print(f"Error sending TCP data: {e}")
|
||||
|
||||
def receive(self):
|
||||
while True:
|
||||
try:
|
||||
data = self.tcpSocket.recv(1024)
|
||||
if data:
|
||||
decoded_data = json.loads(data.decode())
|
||||
self.eventHandler.handleEvents(decoded_data, self.inputHandler, self.world)
|
||||
except Exception as e:
|
||||
print(f"Error receiving TCP data: {e}")
|
||||
break
|
||||
|
||||
def listen(self):
|
||||
tcpThread = threading.Thread(target=self.receive)
|
||||
tcpThread.daemon = True
|
||||
tcpThread.start()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
|
||||
class PathUtil:
|
||||
def getAbsolutePathTo(notAbsolutPath:str) -> str:
|
||||
return os.path.abspath("Client/" + notAbsolutPath)
|
||||
@@ -0,0 +1,11 @@
|
||||
import random
|
||||
import string
|
||||
|
||||
|
||||
class StringUtils:
|
||||
def get_random_string(length) -> str:
|
||||
# choose from all lowercase letter
|
||||
letters = string.ascii_lowercase
|
||||
result_str = ''.join(random.choice(letters) for i in range(length))
|
||||
print("Random string of length", length, "is:", result_str)
|
||||
return result_str
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user