Added collision check for gameobjects

Some cleanup
This commit is contained in:
Christian Bobe
2024-07-21 22:13:32 +02:00
parent f3d43d55a2
commit 9b7e03b865
2 changed files with 58 additions and 40 deletions

73
main.py
View File

@@ -82,19 +82,16 @@ def generate_map(*mapfile):
startpos_y = 0
id = 0
for line in map_file:
#print(line)
for byte in line:
#print(byte)
if(byte == "#"):
map1.append({"id":id,"object": pygame.image.load("models/wall_1.png"), "x":startpos_x, "y":startpos_y})
if(byte == " "):
#movement_map.append({"x":startpos_x, "y":startpos_y})
movement_map.append({"id":id,"object": pygame.image.load("models/wall_1.png"), "x":startpos_x, "y":startpos_y})
startpos_x += map_wall_size
id += 1
#print(line)
startpos_x = 0
startpos_y += map_wall_size
@@ -103,42 +100,42 @@ generate_map()
movement_map_center = (map_space_size-pacman_size)/2
print(F"Center correction: {movement_map_center}")
#Spawn pacman on top of the map
pacman_posx += movement_map[0]["x"]+movement_map_center
pacman_posy += movement_map[0]["y"]+movement_map_center
pacman.set_position(movement_map[0]["x"]+movement_map_center, movement_map[0]["y"]+movement_map_center)
count = 0
count2 = 0
while(gamestate==True):
MAINSCREEN.fill(WEISS)
PLAYGROUND.fill(WEISS)
#PLAYGROUND.fill(WEISS)
for event in pygame.event.get():
if (event.type == pygame.QUIT):
print("Programm wird geschlossen!")
gamestate = False
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_UP):
movedirection["up"] = True
movedirection["down"] = False
print("hoch")
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_DOWN):
movedirection["down"] = True
movedirection["up"] = False
print("runter")
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_LEFT):
movedirection["left"] = True
movedirection["right"] = False
print("links")
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_RIGHT):
movedirection["right"] = True
movedirection["left"] = False
print("rechts")
if (event.type == pygame.KEYDOWN):
if(event.key == pygame.K_ESCAPE):
gamestate = False
if (event.type == pygame.KEYDOWN):
match event.key:
case pygame.K_ESCAPE:
print("ESC... stopping Game!")
gamestate = False
case pygame.K_UP:
movedirection["up"] = True
movedirection["down"] = False
print("hoch")
case pygame.K_DOWN:
movedirection["down"] = True
movedirection["up"] = False
print("runter")
case pygame.K_RIGHT:
movedirection["right"] = True
movedirection["left"] = False
print("rechts")
case pygame.K_LEFT:
movedirection["left"] = True
movedirection["right"] = False
print("links")
#TODO Check for wall-collision!!!!
@@ -167,10 +164,16 @@ while(gamestate==True):
if debug_mode:
#Adds visual character for vaild movementpath (DEBUG ONLY)
for coord in movement_map:
coord:list
tmp = font1.render("X", 1, ROT)
MAINSCREEN.blit(tmp, (coord["x"], coord["y"]))
result = MAINSCREEN.blit(tmp, (coord["x"], coord["y"]))
#Removes collided objects from list
if(pacman.check_collision(result) and len(movement_map) >= 1):
print(len(movement_map))
print(coord)
movement_map.remove(coord)
pass
pacman_pos_label = font1.render(F"x:{pacman.get_position()[0]} y:{pacman.get_position()[1]}", 1, SCHWARZ)
fps_label = font1.render(F"FPS: {int(clock.get_fps())}", 1, SCHWARZ)

View File

@@ -7,22 +7,37 @@ class GameObject():
self._size = [sizex, sizey]
self._name = name
self._surface = surface
self._speed:int = 0
self._spritepath:str = ""
self._sprite:pygame.Surface
self._rect:pygame.rect
self._rect:pygame.rect.RectType
if(len(spritepath) > 0):
self._sprite = pygame.image.load(spritepath)
self._rect = self._sprite.get_rect()
def move(self, x,y):
self._posx += x
self._posy += y
self._posy += y
def draw(self):
self._rect = pygame.Surface.blit(self._surface, self._sprite, (self._posx, self._posy))
def check_collision(self, GameObject:pygame.rect.RectType):
#print(self._rect)
#print(GameObject)
if(pygame.Rect.colliderect(self._rect, GameObject) == True):
return True
else: False
def get_position(self):
return(self._posx, self._posy)
return(self._posx, self._posy)
def get_movespeed(self):
return self._speed
def set_movespeed(self, speed:int):
self._speed = speed
def set_position(self, x, y):
self._posx = x
self._posy = y