import pygame from pygame.locals import * from Classes.Objects.Cards.MonsterCard import MonsterCard from Classes.System.Window import Window from Classes.System.InputHandler import InputHandler from Classes.Objects.World import World class App: __window:Window __running:bool = True __FPS = 60 __clock = pygame.time.Clock() __myFont:pygame.font __world:World __inputHandler: InputHandler def __init__(self, width:int=1920, height:int=1080, title:str="default title"): pygame.font.init() self.__myFont = pygame.font.SysFont('Comic Sans MS', 30) self.__window = Window(width=width, height=height, title=title) self.__inputHandler = InputHandler() # game word self.__world = World() self.startGameLoop() self.onCleanup() def startGameLoop(self): # create sprite groups self.cards = pygame.sprite.Group() testMonsterCard = MonsterCard(pygame.Vector2(500, 850), "Assets/Cards/MonsterCards/testmonstercard/", self.__inputHandler) self.cards.add(testMonsterCard) while self.__running: self.__clock.tick(self.__FPS) self.__window.getScreen().fill((0,0,0)) # render world self.__window.drawWorld(self.__world) # update sprite groups self.cards.update() # draw groups self.__window.drawSpriteGroup(self.cards) # event handler self.handleEvent(pygame.event.get()) # emits update to the game pygame.display.update() # handles incoming event queue def handleEvent(self, events): for event in events: if event.type == pygame.QUIT: self.onCleanup() elif pygame.mouse.get_pressed()[0]: # Wenn linke Maustaste gedrückt wird 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) # 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 card.image = pygame.transform.scale(card.image, (field.getSize()[0] - 10, field.getSize()[1] - 10)) card.rect.center = field.rect.center field.image = card.image.copy() card.setDragging(False) # card.kill() elif event.type == pygame.MOUSEBUTTONUP: mouse_x, mouse_y = pygame.mouse.get_pos() mouse_pos = pygame.Vector2(mouse_x, mouse_y) if event.button == 1: # Wenn linke Maustaste losgelassen wird for card in self.cards: if card.rect.collidepoint(mouse_pos): for field in self.__world.getBoardFields(): if field.getRect().collidepoint(mouse_pos): # Überprüfe, ob das Spielfeld ein MonsterField und die Karte eine MonsterCard ist if field.getSide() == "Player" and field.getType() == "MonsterField" and card.getType() == "MonsterCard": # Setze die Karte zurück, wenn sie aus dem Spielfeld herausgezogen wird card.image = pygame.transform.scale(card.image, card.original_size) card.rect.center = card.original_position card.setDragging(False) break # sets the running state for the gameloop def setRunning(self, running:bool): self.__running = running # ensures the gameloop stops running and the pygame instance is stopped properly def onCleanup(self): self.__running = False pygame.quit()