Added collision control
TODO: Add playerfollower after collide
This commit is contained in:
@@ -15,15 +15,19 @@ clock = pygame.time.Clock()
|
|||||||
ballpos_x = 10
|
ballpos_x = 10
|
||||||
ballpos_y = 10
|
ballpos_y = 10
|
||||||
|
|
||||||
|
#TODO: Make Grid for running Rect
|
||||||
|
|
||||||
class GameObject():
|
class GameObject():
|
||||||
__name = str()
|
__name = str()
|
||||||
__pos_x = int()
|
__pos_x = int()
|
||||||
__pos_y = int()
|
__pos_y = int()
|
||||||
__color = (0,0,0)
|
__color = (0,0,0)
|
||||||
|
__rect_size = []
|
||||||
__movespeed = int()
|
__movespeed = int()
|
||||||
__rectobjekt = pygame.Rect
|
__rectobjekt = pygame.Rect
|
||||||
__surface = MAINSCREEN
|
__surface = MAINSCREEN
|
||||||
__removed = False
|
__removed = False
|
||||||
|
__collided = False
|
||||||
|
|
||||||
__movedirection = {"up":False, "down":False, "left":False, "right":False}
|
__movedirection = {"up":False, "down":False, "left":False, "right":False}
|
||||||
|
|
||||||
@@ -78,7 +82,7 @@ class GameObject():
|
|||||||
#font = pygame.font.SysFont(None, 70)
|
#font = pygame.font.SysFont(None, 70)
|
||||||
#text = font.render("TEST", True, (0,0,0) )
|
#text = font.render("TEST", True, (0,0,0) )
|
||||||
if(self.__removed == False):
|
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):
|
def get_rect(self):
|
||||||
return self.__rectobjekt
|
return self.__rectobjekt
|
||||||
@@ -86,15 +90,30 @@ class GameObject():
|
|||||||
def get_movespeed(self):
|
def get_movespeed(self):
|
||||||
return self.__movespeed
|
return self.__movespeed
|
||||||
|
|
||||||
|
def is_collided(self):
|
||||||
|
return self.__collided
|
||||||
|
|
||||||
|
def set_collided(self):
|
||||||
|
self.__collided = True
|
||||||
|
|
||||||
def collide(self, GameObject):
|
def collide(self, GameObject):
|
||||||
#print(GameObject)
|
#print(GameObject)
|
||||||
#print(GameObject.get_rect())
|
#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())):
|
if(pygame.rect.Rect.colliderect(self.__rectobjekt, GameObject.get_rect())):
|
||||||
print("Kollision")
|
print("\nKollision") #Funktioniert für das erste!
|
||||||
pass
|
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):
|
def remove(self):
|
||||||
print("Gameoject mit der ID " + self.__name + " gelöscht!")
|
print("Gameoject mit der ID " + self.__name + " gelöscht!")
|
||||||
@@ -112,14 +131,13 @@ def spawn_entities(x:int):
|
|||||||
return tuple(Objects)
|
return tuple(Objects)
|
||||||
|
|
||||||
|
|
||||||
object1 = GameObject("Testobject", 50, 50)
|
Player = GameObject("Testobject", 10, 10)
|
||||||
object2 = GameObject("Testobject2", 100, 100)
|
|
||||||
|
|
||||||
background = pygame.surface.Surface((640,480))
|
background = pygame.surface.Surface((640,480))
|
||||||
|
|
||||||
x = 3600
|
x = 3600
|
||||||
invert = False
|
invert = False
|
||||||
spawned_enteties = spawn_entities(5)
|
spawned_entities = spawn_entities(50) #Spawnt eine definierte Anzahl an GameObjekten
|
||||||
#Fenster-Hauptschleife
|
#Fenster-Hauptschleife
|
||||||
while active == True:
|
while active == True:
|
||||||
MAINSCREEN.fill((255,255,255))
|
MAINSCREEN.fill((255,255,255))
|
||||||
@@ -130,36 +148,29 @@ while active == True:
|
|||||||
elif (event.type == pygame.KEYDOWN):
|
elif (event.type == pygame.KEYDOWN):
|
||||||
if (event.key == pygame.K_UP):
|
if (event.key == pygame.K_UP):
|
||||||
print("Keydown")
|
print("Keydown")
|
||||||
object1.change_direction("up")
|
Player.change_direction("up")
|
||||||
|
|
||||||
elif (event.key == pygame.K_DOWN):
|
elif (event.key == pygame.K_DOWN):
|
||||||
print("Keydown")
|
print("Keydown")
|
||||||
object1.change_direction("down")
|
Player.change_direction("down")
|
||||||
#rect1.move(ballpos_x, ballpos_y)
|
|
||||||
|
|
||||||
elif (event.key == pygame.K_RIGHT):
|
elif (event.key == pygame.K_RIGHT):
|
||||||
print("Keydown")
|
print("Keydown")
|
||||||
object1.change_direction("right")
|
Player.change_direction("right")
|
||||||
#rect1.move(ballpos_x, ballpos_y)
|
|
||||||
ballpos_x += 10
|
|
||||||
elif (event.key == pygame.K_LEFT):
|
elif (event.key == pygame.K_LEFT):
|
||||||
print("Keydown")
|
print("Keydown")
|
||||||
object1.change_direction("left")
|
Player.change_direction("left")
|
||||||
#rect1.move(ballpos_x, ballpos_y)
|
|
||||||
ballpos_x -= 10
|
|
||||||
|
|
||||||
for y in spawned_enteties:
|
Player.draw()
|
||||||
|
Player.move(5)
|
||||||
|
|
||||||
|
for y in spawned_entities:
|
||||||
y.draw()
|
y.draw()
|
||||||
y.move(y.get_movespeed())
|
y.collide(Player)
|
||||||
|
y.remove
|
||||||
|
|
||||||
object1.draw()
|
Player.draw_follower(spawned_entities)
|
||||||
object1.move(10)
|
|
||||||
object2.draw()
|
|
||||||
object2.move(1)
|
|
||||||
object2.collide(object1)
|
|
||||||
|
|
||||||
if(x == 0):
|
if(x == 0):
|
||||||
for y in spawned_enteties:
|
for y in spawned_entities:
|
||||||
y.remove()
|
y.remove()
|
||||||
pass
|
pass
|
||||||
x -= 1
|
x -= 1
|
||||||
|
|||||||
Reference in New Issue
Block a user