diff --git a/.gitignore b/.gitignore index 78b759b..215a7f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.idea/ __pycache__ +/venv/ diff --git a/data/images/entities/bush.png b/data/images/entities/bush.png new file mode 100644 index 0000000..a59627d Binary files /dev/null and b/data/images/entities/bush.png differ diff --git a/data/images/entities/treasure.png b/data/images/entities/treasure.png new file mode 100644 index 0000000..a795552 Binary files /dev/null and b/data/images/entities/treasure.png differ diff --git a/src/entities/Collidable.py b/src/entities/Collidable.py index 72a7c36..9ecf2eb 100644 --- a/src/entities/Collidable.py +++ b/src/entities/Collidable.py @@ -14,7 +14,7 @@ class Collidable(Entity): return False -col = Collidable(1, 1, 1) +#col = Collidable(1, 1, 1) """ diff --git a/src/entities/Player.py b/src/entities/Player.py index ed6f38a..6472d46 100644 --- a/src/entities/Player.py +++ b/src/entities/Player.py @@ -18,7 +18,7 @@ class Player(Entity): self.rotate(rotation) # Otherwise, move one tile to a given direction else: - # TODO: Collision checks + # TODO: Collision checks "and not to events" if rotation.value == Rotations.NORTH.value: self.rect.y -= self.rect.w elif rotation.value == Rotations.EAST.value: @@ -32,7 +32,6 @@ class Player(Entity): self.image = pygame.transform.rotate(self.image, ((self.rotation.value - rotation.value) * 90)) self.rotation = rotation - class Rotations(Enum): NORTH = 0 EAST = 1 diff --git a/src/game/EventManager.py b/src/game/EventManager.py index d2a9a13..f093015 100644 --- a/src/game/EventManager.py +++ b/src/game/EventManager.py @@ -7,6 +7,7 @@ TIMEOUT = 100 class EventManager: keyTimeout = 0 + #self.game.map def __init__(self, gameObject, player): self.game = gameObject self.player = player @@ -33,12 +34,14 @@ class EventManager: # Key names are temporary # TODO: Load key bindings from JSON - if keys[pygame.K_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]: + 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]: + 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]: + 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) + + diff --git a/src/game/Game.py b/src/game/Game.py index 6e6e8e4..88f4647 100644 --- a/src/game/Game.py +++ b/src/game/Game.py @@ -6,6 +6,7 @@ from os import path from game.EventManager import EventManager from game.Screen import Screen from game.Map import Map +from src.entities.Player import Player class Game: @@ -27,6 +28,7 @@ class Game: print("Initializing pygame...", end=" ") pygame.init() self.spritesList = pygame.sprite.Group() + print("OK") print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ") @@ -38,12 +40,11 @@ class Game: self.screen = Screen(self, self.config["window"]) print("OK") - self.eventManager = EventManager(self) - - # Start Map implement self.mapDataFolder = path.dirname("../data/mapdata/") self.map = Map(path.join(self.mapDataFolder, 'map.txt'), self.screen) - # End Map implement + self.player = Player((0, 0), self.map.tileSize) + self.map.addEntity(self.player) + self.eventManager = EventManager(self, self.player) self.mainLoop() diff --git a/src/game/Map.py b/src/game/Map.py index 7daa356..82a498f 100644 --- a/src/game/Map.py +++ b/src/game/Map.py @@ -6,7 +6,9 @@ class Map: def __init__(self, filename, screen): self.screen = screen self.terrain = [] - self.entities = [] + self.collidableTerrain = [] + self.collidables = pygame.sprite.Group() + with open(filename, 'rt') as f: for line in f: self.terrain.append(line) @@ -19,15 +21,26 @@ class Map: self.height = self.tileHeight * self.tileSize self.terrainDraw() - + def terrainDraw(self): for row, tiles in enumerate(self.terrain): for col, tile in enumerate(tiles): if tile == 'w': - self.screen.draw(TerrainTile('wall.png', self.tileSize), Locations.MAP, col*self.tileSize, row*self.tileSize) + object = TerrainTile(col, row, 'wall.png', self.tileSize) + self.screen.draw(object, Locations.MAP, 0, 0) + self.collidables.add(object) elif tile == ',': - self.screen.draw(TerrainTile('floor.png', self.tileSize), Locations.MAP, col*self.tileSize, row*self.tileSize) + self.screen.draw(TerrainTile(col, row, 'floor.png', self.tileSize), Locations.MAP, 0, 0) elif tile == '.': - self.screen.draw(TerrainTile('grass.png', self.tileSize), Locations.MAP, col*self.tileSize, row*self.tileSize) - + self.screen.draw(TerrainTile(col, row, 'grass.png', self.tileSize), Locations.MAP, 0, 0) + def addEntity(self, entity): + self.screen.draw(entity, Locations.MAP, 0, 0) + self.collidables.add(entity) + + # 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 diff --git a/src/game/TerrainTile.py b/src/game/TerrainTile.py index 636838b..064c303 100644 --- a/src/game/TerrainTile.py +++ b/src/game/TerrainTile.py @@ -4,10 +4,14 @@ from os import path class TerrainTile(pygame.sprite.Sprite): - def __init__(self, texture, tileSize): + def __init__(self, x, y, texture, tileSize): super().__init__() self.imagesFolder = path.dirname("../data/images/") self.terrainFolder = path.join(self.imagesFolder, 'terrain') self.image = pygame.image.load(os.path.join(self.terrainFolder, texture)).convert() self.image = pygame.transform.scale(self.image, (tileSize, tileSize)) self.rect = self.image.get_rect() + self.x = x + self.y = y + self.rect.x = x * tileSize + self.rect.y = y * tileSize