move collision to Map class

This commit is contained in:
Wirus 2020-04-05 20:20:37 +02:00
parent 853a3b1601
commit c121519605
2 changed files with 11 additions and 10 deletions

View File

@ -34,19 +34,14 @@ class EventManager:
# Key names are temporary
# TODO: Load key bindings from JSON
if keys[pygame.K_w] and not self.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w):
if keys[pygame.K_w] and not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w):
self.player.move(Rotations.NORTH)
if keys[pygame.K_s] and not self.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w):
if keys[pygame.K_s] and not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w):
self.player.move(Rotations.SOUTH)
if keys[pygame.K_d] and not self.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y):
if keys[pygame.K_d] and not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y):
self.player.move(Rotations.EAST)
if keys[pygame.K_a] and not self.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y):
if keys[pygame.K_a] and not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y):
self.player.move(Rotations.WEST)
# add object to map.collidables list to be collidable
def collision(self, x, y):
for b in self.game.map.collidables:
if b.rect.x == x and b.rect.y == y:
return True
return False

View File

@ -39,3 +39,9 @@ class Map:
self.entities.append(entity)
self.screen.draw(entity, Locations.MAP, 0, 0)
# add object to map.collidables list to be collidable
def collision(self, x, y):
for b in self.collidables:
if b.rect.x == x and b.rect.y == y:
return True
return False