diff --git a/pygame-test.py b/pygame-test.py index ed636ea..fa9be45 100644 --- a/pygame-test.py +++ b/pygame-test.py @@ -15,15 +15,19 @@ clock = pygame.time.Clock() ballpos_x = 10 ballpos_y = 10 +#TODO: Make Grid for running Rect + class GameObject(): __name = str() __pos_x = int() __pos_y = int() __color = (0,0,0) + __rect_size = [] __movespeed = int() __rectobjekt = pygame.Rect __surface = MAINSCREEN __removed = False + __collided = False __movedirection = {"up":False, "down":False, "left":False, "right":False} @@ -78,7 +82,7 @@ class GameObject(): #font = pygame.font.SysFont(None, 70) #text = font.render("TEST", True, (0,0,0) ) if(self.__removed == False): - self.__rectobjekt = pygame.draw.rect(self.__surface, self.__color, [self.__pos_x, self.__pos_y,100,100]) + self.__rectobjekt = pygame.draw.rect(self.__surface, self.__color, [self.__pos_x, self.__pos_y,20,20]) def get_rect(self): return self.__rectobjekt @@ -86,15 +90,30 @@ class GameObject(): def get_movespeed(self): return self.__movespeed + def is_collided(self): + return self.__collided + + def set_collided(self): + self.__collided = True + def collide(self, GameObject): #print(GameObject) #print(GameObject.get_rect()) - if(self.get_rect().x == GameObject.get_rect().x and self.get_rect().y == GameObject.get_rect().y): - """TODO: Hitbox with Objectsize""" + """TODO: Hitbox with Objectsize""" - if(pygame.rect.Rect.colliderect(self.__rectobjekt, GameObject.get_rect())): - print("Kollision") - pass + if(pygame.rect.Rect.colliderect(self.__rectobjekt, GameObject.get_rect())): + print("\nKollision") #Funktioniert für das erste! + self.set_collided() + self.remove() #Löscht das Objekt nach einem Zusammenstoß mit Playerobject + + def draw_follower(self, *Entities:tuple): + """Für alle Collidierten Gameobjects""" + for x in Entities: + y = GameObject(x) + if(y.is_collided() == True): + print("REST") + + def remove(self): print("Gameoject mit der ID " + self.__name + " gelöscht!") @@ -112,14 +131,13 @@ def spawn_entities(x:int): return tuple(Objects) -object1 = GameObject("Testobject", 50, 50) -object2 = GameObject("Testobject2", 100, 100) +Player = GameObject("Testobject", 10, 10) background = pygame.surface.Surface((640,480)) x = 3600 invert = False -spawned_enteties = spawn_entities(5) +spawned_entities = spawn_entities(50) #Spawnt eine definierte Anzahl an GameObjekten #Fenster-Hauptschleife while active == True: MAINSCREEN.fill((255,255,255)) @@ -130,36 +148,29 @@ while active == True: elif (event.type == pygame.KEYDOWN): if (event.key == pygame.K_UP): print("Keydown") - object1.change_direction("up") - + Player.change_direction("up") elif (event.key == pygame.K_DOWN): print("Keydown") - object1.change_direction("down") - #rect1.move(ballpos_x, ballpos_y) - + Player.change_direction("down") elif (event.key == pygame.K_RIGHT): print("Keydown") - object1.change_direction("right") - #rect1.move(ballpos_x, ballpos_y) - ballpos_x += 10 + Player.change_direction("right") elif (event.key == pygame.K_LEFT): print("Keydown") - object1.change_direction("left") - #rect1.move(ballpos_x, ballpos_y) - ballpos_x -= 10 + Player.change_direction("left") - for y in spawned_enteties: + Player.draw() + Player.move(5) + + for y in spawned_entities: y.draw() - y.move(y.get_movespeed()) + y.collide(Player) + y.remove - object1.draw() - object1.move(10) - object2.draw() - object2.move(1) - object2.collide(object1) + Player.draw_follower(spawned_entities) if(x == 0): - for y in spawned_enteties: + for y in spawned_entities: y.remove() pass x -= 1