successfully added card snapping
This commit is contained in:
@ -2,14 +2,16 @@ import pygame
|
|||||||
|
|
||||||
class BoardField():
|
class BoardField():
|
||||||
__name:str
|
__name:str
|
||||||
|
__side:str
|
||||||
__type:str
|
__type:str
|
||||||
__pos:pygame.Vector2
|
__pos:pygame.Vector2
|
||||||
__size:tuple
|
__size:tuple
|
||||||
__color:tuple = (255,255,255)
|
__color:tuple = (255,255,255)
|
||||||
_rect:pygame.Rect
|
_rect:pygame.Rect
|
||||||
|
|
||||||
def __init__(self, name:str, type:str, pos:pygame.Vector2, size:tuple, color:tuple):
|
def __init__(self, name:str, side:str, type:str, pos:pygame.Vector2, size:tuple, color:tuple):
|
||||||
self.__name = name
|
self.__name = name
|
||||||
|
self.__side = side
|
||||||
self.__type = type
|
self.__type = type
|
||||||
self.__pos = pos
|
self.__pos = pos
|
||||||
self.__size = size
|
self.__size = size
|
||||||
@ -19,6 +21,9 @@ class BoardField():
|
|||||||
def getName(self) -> str:
|
def getName(self) -> str:
|
||||||
return self.__name
|
return self.__name
|
||||||
|
|
||||||
|
def getSide(self) -> str:
|
||||||
|
return self.__side
|
||||||
|
|
||||||
def getType(self) -> str:
|
def getType(self) -> str:
|
||||||
return self.__type
|
return self.__type
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ class MonsterCard(pygame.sprite.Sprite):
|
|||||||
__dragging:bool = False
|
__dragging:bool = False
|
||||||
__offset:pygame.Vector2 = pygame.Vector2(0,0)
|
__offset:pygame.Vector2 = pygame.Vector2(0,0)
|
||||||
__inputHandler: InputHandler
|
__inputHandler: InputHandler
|
||||||
|
__type:str = "MonsterCard"
|
||||||
image:pygame.image
|
image:pygame.image
|
||||||
rect:pygame.rect
|
rect:pygame.rect
|
||||||
|
|
||||||
@ -37,8 +38,6 @@ class MonsterCard(pygame.sprite.Sprite):
|
|||||||
self.__attacks.append(attack)
|
self.__attacks.append(attack)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
print("card update")
|
|
||||||
print(self.getDragging())
|
|
||||||
if self.getDragging():
|
if self.getDragging():
|
||||||
mouse_pos = self.__inputHandler.getMousePos()
|
mouse_pos = self.__inputHandler.getMousePos()
|
||||||
self.__pos = mouse_pos
|
self.__pos = mouse_pos
|
||||||
@ -65,8 +64,14 @@ class MonsterCard(pygame.sprite.Sprite):
|
|||||||
def getPos(self):
|
def getPos(self):
|
||||||
return self.__pos
|
return self.__pos
|
||||||
|
|
||||||
|
def getType(self):
|
||||||
|
return self.__type
|
||||||
|
|
||||||
def setDragging(self, dragging:bool):
|
def setDragging(self, dragging:bool):
|
||||||
self.__dragging = dragging
|
self.__dragging = dragging
|
||||||
|
|
||||||
def setOffset(self, offset:pygame.Vector2):
|
def setOffset(self, offset:pygame.Vector2):
|
||||||
self.__offset = offset
|
self.__offset = offset
|
||||||
|
|
||||||
|
def setPos(self, pos:pygame.Vector2):
|
||||||
|
self.__pos = pos
|
Binary file not shown.
@ -19,29 +19,29 @@ class World():
|
|||||||
# construct elements arround the playerfield
|
# construct elements arround the playerfield
|
||||||
# Todo add lifepoint label for player and enemy and make them scriptable
|
# Todo add lifepoint label for player and enemy and make them scriptable
|
||||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 0)), 15)
|
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 0)), 15)
|
||||||
self.__boardFields.append(BoardField("EnemyDeck", "Deck", pVector, (self.__cardWidth, self.__cardHeight), (0,255,0)))
|
self.__boardFields.append(BoardField("EnemyDeck", "Enemy", "Deck", pVector, (self.__cardWidth, self.__cardHeight), (0,255,0)))
|
||||||
|
|
||||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 0)), (self.__cardHeight + 25))
|
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 0)), (self.__cardHeight + 25))
|
||||||
self.__boardFields.append(BoardField("EnemyGraveyard", "Grave", pVector, (self.__cardWidth, self.__cardHeight), (0,255,0)))
|
self.__boardFields.append(BoardField("EnemyGraveyard", "Enemy", "Grave", pVector, (self.__cardWidth, self.__cardHeight), (0,255,0)))
|
||||||
|
|
||||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 5)), ((2 * self.__cardHeight) + 60))
|
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 5)), ((2 * self.__cardHeight) + 60))
|
||||||
self.__boardFields.append(BoardField("PlayerDeck", "Deck", pVector, (self.__cardWidth, self.__cardHeight), (255,0,0)))
|
self.__boardFields.append(BoardField("PlayerDeck", "Player", "Deck", pVector, (self.__cardWidth, self.__cardHeight), (255,0,0)))
|
||||||
|
|
||||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 5)), ((2 * self.__cardHeight) + self.__cardHeight + 70))
|
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 5)), ((2 * self.__cardHeight) + self.__cardHeight + 70))
|
||||||
self.__boardFields.append(BoardField("PlayerGraveyard", "Grave", pVector, (self.__cardWidth, self.__cardHeight), (255,0,0)))
|
self.__boardFields.append(BoardField("PlayerGraveyard", "Player", "Grave", pVector, (self.__cardWidth, self.__cardHeight), (255,0,0)))
|
||||||
|
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * i)), ((2 * self.__cardHeight) + 60))
|
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * i)), ((2 * self.__cardHeight) + 60))
|
||||||
self.__boardFields.append(BoardField("PlayerMonsterField-"+str(i),"MonsterField",pVector,(self.__cardWidth, self.__cardHeight),(255,255,255)))
|
self.__boardFields.append(BoardField("PlayerMonsterField-"+str(i), "Player", "MonsterField",pVector,(self.__cardWidth, self.__cardHeight),(255,255,255)))
|
||||||
|
|
||||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * i)), ((2 * self.__cardHeight) + self.__cardHeight + 70))
|
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * i)), ((2 * self.__cardHeight) + self.__cardHeight + 70))
|
||||||
self.__boardFields.append(BoardField("PlayerMonsterField-"+str(i), "EffectField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
self.__boardFields.append(BoardField("PlayerMonsterField-"+str(i), "Player", "EffectField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
||||||
|
|
||||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * (i+1)), 15), (self.__cardWidth, self.__cardHeight))
|
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * (i+1)), 15), (self.__cardWidth, self.__cardHeight))
|
||||||
self.__boardFields.append(BoardField("EnemyMonsterField-"+str(i), "MonsterField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
self.__boardFields.append(BoardField("EnemyMonsterField-"+str(i), "Enemy", "MonsterField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
||||||
|
|
||||||
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * (i+1))), (self.__cardHeight + 25))
|
pVector = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * (i+1))), (self.__cardHeight + 25))
|
||||||
self.__boardFields.append(BoardField("EnemySpellTrapField-"+str(i), "EffectField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
self.__boardFields.append(BoardField("EnemySpellTrapField-"+str(i), "Enemy", "EffectField", pVector, (self.__cardWidth, self.__cardHeight), (255,255,255)))
|
||||||
|
|
||||||
def getBoardFields(self) -> list:
|
def getBoardFields(self) -> list:
|
||||||
return self.__boardFields
|
return self.__boardFields
|
||||||
|
Binary file not shown.
Binary file not shown.
@ -62,15 +62,22 @@ class App:
|
|||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.onCleanup()
|
self.onCleanup()
|
||||||
elif pygame.mouse.get_pressed()[0]: # Wenn linke Maustaste gedrückt wird
|
elif pygame.mouse.get_pressed()[0]: # Wenn linke Maustaste gedrückt wird
|
||||||
print("mousebutton down")
|
|
||||||
mouse_x, mouse_y = pygame.mouse.get_pos()
|
mouse_x, mouse_y = pygame.mouse.get_pos()
|
||||||
mouse_pos = pygame.Vector2(mouse_x, mouse_y)
|
mouse_pos = pygame.Vector2(mouse_x, mouse_y)
|
||||||
for card in self.cards:
|
for card in self.cards:
|
||||||
if card.rect.collidepoint(mouse_pos):
|
if card.rect.collidepoint(mouse_pos):
|
||||||
card.setDragging(True)
|
card.setDragging(True)
|
||||||
print(card.getDragging())
|
|
||||||
# Berechnung des Offset zwischen der Karte und der Mausposition
|
# card.setOffset(mouse_pos - card.getPos())
|
||||||
card.setOffset(mouse_pos - card.getPos())
|
for field in self.__world.getBoardFields():
|
||||||
|
if field.getRect().collidepoint(mouse_pos):
|
||||||
|
if field.getSide() == "Player":
|
||||||
|
if field.getType() == "MonsterField" and card.getType() == "MonsterCard":
|
||||||
|
# todo: resize card so that it fits into the card field
|
||||||
|
pygame.transform.scale(card.image,(int((field.getRect().width - 10)),int((field.getRect().height - 10))))
|
||||||
|
card.rect.center = field.getRect().center
|
||||||
|
card.setDragging(False)
|
||||||
|
self.__window.getScreen().blit(card.image, field.getRect().center)
|
||||||
|
|
||||||
elif event.type == pygame.MOUSEBUTTONUP:
|
elif event.type == pygame.MOUSEBUTTONUP:
|
||||||
if event.button == 1: # Wenn linke Maustaste losgelassen wird
|
if event.button == 1: # Wenn linke Maustaste losgelassen wird
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user