added mouse dragging to cards
This commit is contained in:
@ -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.
Reference in New Issue
Block a user