From c1215196055b1b7366697db9d50ea3ae532d1766 Mon Sep 17 00:00:00 2001 From: Wirus Date: Sun, 5 Apr 2020 20:20:37 +0200 Subject: [PATCH] move collision to Map class --- src/game/EventManager.py | 15 +++++---------- src/game/Map.py | 6 ++++++ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/game/EventManager.py b/src/game/EventManager.py index 5143fee..f093015 100644 --- a/src/game/EventManager.py +++ b/src/game/EventManager.py @@ -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 + diff --git a/src/game/Map.py b/src/game/Map.py index c072880..e5a1d27 100644 --- a/src/game/Map.py +++ b/src/game/Map.py @@ -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