added mouse dragging to cards
This commit is contained in:
parent
8c544556ed
commit
a25f4c3eba
@ -3,32 +3,46 @@ from typing import Any
|
|||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
|
from Classes.System.InputHandler import InputHandler
|
||||||
|
|
||||||
class MonsterCard(pygame.sprite.Sprite):
|
class MonsterCard(pygame.sprite.Sprite):
|
||||||
__attacks = []
|
__attacks = []
|
||||||
__name:str
|
__name:str
|
||||||
__description:str
|
__description:str
|
||||||
|
__pos:pygame.Vector2
|
||||||
|
__dragging:bool = False
|
||||||
|
__offset:pygame.Vector2 = pygame.Vector2(0,0)
|
||||||
|
__inputHandler: InputHandler
|
||||||
image:pygame.image
|
image:pygame.image
|
||||||
rect:pygame.rect
|
rect:pygame.rect
|
||||||
|
|
||||||
def __init__(self, pos:tuple, assetDir:str):
|
def __init__(self, pos:pygame.Vector2, assetDir:str, inputHandler:InputHandler):
|
||||||
if assetDir == "":
|
if assetDir == "":
|
||||||
return ValueError.add_note("Card: imagePath cannot be empty")
|
return ValueError.add_note("Card: imagePath cannot be empty")
|
||||||
|
|
||||||
pygame.sprite.Sprite.__init__(self)
|
pygame.sprite.Sprite.__init__(self)
|
||||||
|
|
||||||
data = json.load(open(assetDir + "/testmonstercard.json"))
|
data = json.load(open(assetDir + "/testmonstercard.json"))
|
||||||
|
self.__pos = pos
|
||||||
self.__name = data["name"]
|
self.__name = data["name"]
|
||||||
self.image = pygame.image.load(assetDir + "/card.png").convert_alpha()
|
self.image = pygame.image.load(assetDir + "/card.png").convert_alpha()
|
||||||
self.rect = self.image.get_rect()
|
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"]
|
self.__description = data["description"]
|
||||||
|
|
||||||
for attack in data["attacks"]:
|
for attack in data["attacks"]:
|
||||||
self.__attacks.append(attack)
|
self.__attacks.append(attack)
|
||||||
|
|
||||||
def update(self):
|
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):
|
def attacks(self):
|
||||||
return self.__attacks
|
return self.__attacks
|
||||||
@ -41,3 +55,18 @@ class MonsterCard(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
def getDescription(self):
|
def getDescription(self):
|
||||||
return self.__description
|
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
|
Binary file not shown.
@ -49,5 +49,5 @@ class World():
|
|||||||
def getCardWidth(self) -> int:
|
def getCardWidth(self) -> int:
|
||||||
return self.__cardWidth
|
return self.__cardWidth
|
||||||
|
|
||||||
def getCardHeifht(self) -> int:
|
def getCardHeight(self) -> int:
|
||||||
return self.__cardHeight
|
return self.__cardHeight
|
Binary file not shown.
BIN
Game_Client/Classes/Objects/__pycache__/World.cpython-311.pyc
Normal file
BIN
Game_Client/Classes/Objects/__pycache__/World.cpython-311.pyc
Normal file
Binary file not shown.
@ -31,10 +31,10 @@ class App:
|
|||||||
def startGameLoop(self):
|
def startGameLoop(self):
|
||||||
|
|
||||||
# create sprite groups
|
# create sprite groups
|
||||||
cards = pygame.sprite.Group()
|
self.cards = pygame.sprite.Group()
|
||||||
|
|
||||||
testMonsterCard = MonsterCard((500,500), "Assets/Cards/MonsterCards/testmonstercard/")
|
testMonsterCard = MonsterCard(pygame.Vector2(500,500), "Assets/Cards/MonsterCards/testmonstercard/", self.__inputHandler)
|
||||||
cards.add(testMonsterCard)
|
self.cards.add(testMonsterCard)
|
||||||
|
|
||||||
while self.__running:
|
while self.__running:
|
||||||
self.__clock.tick(self.__FPS)
|
self.__clock.tick(self.__FPS)
|
||||||
@ -42,14 +42,13 @@ class App:
|
|||||||
self.__window.getScreen().fill((0,0,0))
|
self.__window.getScreen().fill((0,0,0))
|
||||||
|
|
||||||
# render world
|
# render world
|
||||||
self.__inputHandler.getMouseHover(self.__world)
|
|
||||||
self.__window.drawWorld(self.__world)
|
self.__window.drawWorld(self.__world)
|
||||||
|
|
||||||
# update sprite groups
|
# update sprite groups
|
||||||
cards.update()
|
self.cards.update()
|
||||||
|
|
||||||
# draw groups
|
# draw groups
|
||||||
self.__window.drawSpriteGroup(cards)
|
self.__window.drawSpriteGroup(self.cards)
|
||||||
|
|
||||||
# event handler
|
# event handler
|
||||||
self.handleEvent(pygame.event.get())
|
self.handleEvent(pygame.event.get())
|
||||||
@ -62,10 +61,21 @@ class App:
|
|||||||
for event in events:
|
for event in events:
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.onCleanup()
|
self.onCleanup()
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
elif pygame.mouse.get_pressed()[0]: # Wenn linke Maustaste gedrückt wird
|
||||||
pass
|
print("mousebutton down")
|
||||||
if event.type == pygame.MOUSEBUTTONUP:
|
mouse_x, mouse_y = pygame.mouse.get_pos()
|
||||||
pass
|
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
|
# sets the running state for the gameloop
|
||||||
def setRunning(self, running:bool):
|
def setRunning(self, running:bool):
|
||||||
|
@ -31,15 +31,16 @@ class InputHandler:
|
|||||||
# get field under mousbutton
|
# get field under mousbutton
|
||||||
def getMouseHover(self, world:World) -> BoardField:
|
def getMouseHover(self, world:World) -> BoardField:
|
||||||
mouse_pos = self.getMousePos()
|
mouse_pos = self.getMousePos()
|
||||||
xPos = [int(v//world.getCardWidth()) for v in mouse_pos.x]
|
x_pos = mouse_pos.x / world.getCardWidth()
|
||||||
yPos = [int(v//world.getCardHeifht()) for v in mouse_pos.y]
|
y_pos = mouse_pos.y / world.getCardHeight()
|
||||||
|
|
||||||
try:
|
for field in world.getBoardFields():
|
||||||
for field in world.getBoardFields():
|
field_x = field.getPos().x
|
||||||
for x in xPos:
|
field_y = field.getPos().y
|
||||||
for y in yPos:
|
field_width = world.getCardWidth() # Annahme: Jedes Feld hat eine Breite von 1 Einheit
|
||||||
if x == field.getPos().x and y == field.getPos().y:
|
field_height = world.getCardHeight() # Annahme: Jedes Feld hat eine Höhe von 1 Einheit
|
||||||
return field
|
|
||||||
except IndexError: pass
|
if field_x <= x_pos < field_x + field_width and field_y <= y_pos < field_y + field_height:
|
||||||
|
return field
|
||||||
|
|
||||||
return None
|
return None
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user