added mouse dragging to cards
This commit is contained in:
parent
8c544556ed
commit
a25f4c3eba
@ -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
|
Binary file not shown.
@ -49,5 +49,5 @@ class World():
|
||||
def getCardWidth(self) -> int:
|
||||
return self.__cardWidth
|
||||
|
||||
def getCardHeifht(self) -> int:
|
||||
def getCardHeight(self) -> int:
|
||||
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):
|
||||
|
||||
# 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):
|
||||
|
@ -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
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user