diff --git a/Game_Client/Classes/Objects/Cards/MonsterCard.py b/Game_Client/Classes/Objects/Cards/MonsterCard.py index e6b6054..ff85836 100644 --- a/Game_Client/Classes/Objects/Cards/MonsterCard.py +++ b/Game_Client/Classes/Objects/Cards/MonsterCard.py @@ -3,32 +3,46 @@ from typing import Any import pygame +from Classes.System.InputHandler import InputHandler + class MonsterCard(pygame.sprite.Sprite): __attacks = [] __name:str __description:str + __pos:pygame.Vector2 + __dragging:bool = False + __offset:pygame.Vector2 = pygame.Vector2(0,0) + __inputHandler: InputHandler image:pygame.image rect:pygame.rect - def __init__(self, pos:tuple, assetDir:str): + def __init__(self, pos:pygame.Vector2, assetDir:str, inputHandler:InputHandler): if assetDir == "": return ValueError.add_note("Card: imagePath cannot be empty") pygame.sprite.Sprite.__init__(self) data = json.load(open(assetDir + "/testmonstercard.json")) - + self.__pos = pos self.__name = data["name"] self.image = pygame.image.load(assetDir + "/card.png").convert_alpha() self.rect = self.image.get_rect() - self.rect.center = pos + self.dragging = False + self.offset = pygame.Vector2(0, 0) # Offset zwischen der Karte und der Mausposition + self.__inputHandler = inputHandler + self.rect.center = self.__pos self.__description = data["description"] for attack in data["attacks"]: self.__attacks.append(attack) def update(self): - pass + print("card update") + print(self.getDragging()) + if self.getDragging(): + mouse_pos = self.__inputHandler.getMousePos() + self.__pos = mouse_pos + self.rect.center = self.__pos def attacks(self): return self.__attacks @@ -41,3 +55,18 @@ class MonsterCard(pygame.sprite.Sprite): def getDescription(self): return self.__description + + def getDragging(self): + return self.__dragging + + def getOffset(self): + return self.__offset + + def getPos(self): + return self.__pos + + def setDragging(self, dragging:bool): + self.__dragging = dragging + + def setOffset(self, offset:pygame.Vector2): + self.__offset = offset \ No newline at end of file diff --git a/Game_Client/Classes/Objects/Cards/__pycache__/MonsterCard.cpython-311.pyc b/Game_Client/Classes/Objects/Cards/__pycache__/MonsterCard.cpython-311.pyc index 5c820ac..8716d37 100644 Binary files a/Game_Client/Classes/Objects/Cards/__pycache__/MonsterCard.cpython-311.pyc and b/Game_Client/Classes/Objects/Cards/__pycache__/MonsterCard.cpython-311.pyc differ diff --git a/Game_Client/Classes/Objects/World.py b/Game_Client/Classes/Objects/World.py index 1fb6eab..37c5b77 100644 --- a/Game_Client/Classes/Objects/World.py +++ b/Game_Client/Classes/Objects/World.py @@ -49,5 +49,5 @@ class World(): def getCardWidth(self) -> int: return self.__cardWidth - def getCardHeifht(self) -> int: + def getCardHeight(self) -> int: return self.__cardHeight \ No newline at end of file diff --git a/Game_Client/Classes/Objects/__pycache__/BoardField.cpython-311.pyc b/Game_Client/Classes/Objects/__pycache__/BoardField.cpython-311.pyc new file mode 100644 index 0000000..aeeb3ec Binary files /dev/null and b/Game_Client/Classes/Objects/__pycache__/BoardField.cpython-311.pyc differ diff --git a/Game_Client/Classes/Objects/__pycache__/World.cpython-311.pyc b/Game_Client/Classes/Objects/__pycache__/World.cpython-311.pyc new file mode 100644 index 0000000..c7b425c Binary files /dev/null and b/Game_Client/Classes/Objects/__pycache__/World.cpython-311.pyc differ diff --git a/Game_Client/Classes/System/App.py b/Game_Client/Classes/System/App.py index b67f46a..075704d 100644 --- a/Game_Client/Classes/System/App.py +++ b/Game_Client/Classes/System/App.py @@ -31,10 +31,10 @@ class App: def startGameLoop(self): # create sprite groups - cards = pygame.sprite.Group() + self.cards = pygame.sprite.Group() - testMonsterCard = MonsterCard((500,500), "Assets/Cards/MonsterCards/testmonstercard/") - cards.add(testMonsterCard) + testMonsterCard = MonsterCard(pygame.Vector2(500,500), "Assets/Cards/MonsterCards/testmonstercard/", self.__inputHandler) + self.cards.add(testMonsterCard) while self.__running: self.__clock.tick(self.__FPS) @@ -42,14 +42,13 @@ class App: self.__window.getScreen().fill((0,0,0)) # render world - self.__inputHandler.getMouseHover(self.__world) self.__window.drawWorld(self.__world) # update sprite groups - cards.update() + self.cards.update() # draw groups - self.__window.drawSpriteGroup(cards) + self.__window.drawSpriteGroup(self.cards) # event handler self.handleEvent(pygame.event.get()) @@ -62,10 +61,21 @@ class App: for event in events: if event.type == pygame.QUIT: self.onCleanup() - if event.type == pygame.MOUSEBUTTONDOWN: - pass - if event.type == pygame.MOUSEBUTTONUP: - pass + elif pygame.mouse.get_pressed()[0]: # Wenn linke Maustaste gedrückt wird + print("mousebutton down") + mouse_x, mouse_y = pygame.mouse.get_pos() + mouse_pos = pygame.Vector2(mouse_x, mouse_y) + for card in self.cards: + if card.rect.collidepoint(mouse_pos): + card.setDragging(True) + print(card.getDragging()) + # Berechnung des Offset zwischen der Karte und der Mausposition + card.setOffset(mouse_pos - card.getPos()) + + elif event.type == pygame.MOUSEBUTTONUP: + if event.button == 1: # Wenn linke Maustaste losgelassen wird + for card in self.cards: + card.setDragging(False) # sets the running state for the gameloop def setRunning(self, running:bool): diff --git a/Game_Client/Classes/System/InputHandler.py b/Game_Client/Classes/System/InputHandler.py index 293f55e..cf05696 100644 --- a/Game_Client/Classes/System/InputHandler.py +++ b/Game_Client/Classes/System/InputHandler.py @@ -31,15 +31,16 @@ class InputHandler: # get field under mousbutton def getMouseHover(self, world:World) -> BoardField: mouse_pos = self.getMousePos() - xPos = [int(v//world.getCardWidth()) for v in mouse_pos.x] - yPos = [int(v//world.getCardHeifht()) for v in mouse_pos.y] + x_pos = mouse_pos.x / world.getCardWidth() + y_pos = mouse_pos.y / world.getCardHeight() - try: - for field in world.getBoardFields(): - for x in xPos: - for y in yPos: - if x == field.getPos().x and y == field.getPos().y: - return field - except IndexError: pass + for field in world.getBoardFields(): + field_x = field.getPos().x + field_y = field.getPos().y + field_width = world.getCardWidth() # Annahme: Jedes Feld hat eine Breite von 1 Einheit + field_height = world.getCardHeight() # 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 \ No newline at end of file diff --git a/Game_Client/Classes/System/__pycache__/App.cpython-311.pyc b/Game_Client/Classes/System/__pycache__/App.cpython-311.pyc index f8f707c..d826af9 100644 Binary files a/Game_Client/Classes/System/__pycache__/App.cpython-311.pyc and b/Game_Client/Classes/System/__pycache__/App.cpython-311.pyc differ diff --git a/Game_Client/Classes/System/__pycache__/InputHandler.cpython-311.pyc b/Game_Client/Classes/System/__pycache__/InputHandler.cpython-311.pyc index d1c7b3b..6739cf5 100644 Binary files a/Game_Client/Classes/System/__pycache__/InputHandler.cpython-311.pyc and b/Game_Client/Classes/System/__pycache__/InputHandler.cpython-311.pyc differ diff --git a/Game_Client/Classes/System/__pycache__/Window.cpython-311.pyc b/Game_Client/Classes/System/__pycache__/Window.cpython-311.pyc index 682f95e..3a9adcb 100644 Binary files a/Game_Client/Classes/System/__pycache__/Window.cpython-311.pyc and b/Game_Client/Classes/System/__pycache__/Window.cpython-311.pyc differ