Map: collidable group of sprites

TerrainTile: position attributes
EventManager: collision method
This commit is contained in:
Wirus 2020-04-05 19:08:08 +02:00
parent 16e361540e
commit 853a3b1601
6 changed files with 26 additions and 16 deletions

View File

@ -14,7 +14,7 @@ class Collidable(Entity):
return False return False
col = Collidable(1, 1, 1) #col = Collidable(1, 1, 1)
""" """

View File

@ -18,7 +18,7 @@ class Player(Entity):
self.rotate(rotation) self.rotate(rotation)
# Otherwise, move one tile to a given direction # Otherwise, move one tile to a given direction
else: else:
# TODO: Collision checks # TODO: Collision checks "and not to events"
if rotation.value == Rotations.NORTH.value: if rotation.value == Rotations.NORTH.value:
self.rect.y -= self.rect.w self.rect.y -= self.rect.w
elif rotation.value == Rotations.EAST.value: 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.image = pygame.transform.rotate(self.image, ((self.rotation.value - rotation.value) * 90))
self.rotation = rotation self.rotation = rotation
class Rotations(Enum): class Rotations(Enum):
NORTH = 0 NORTH = 0
EAST = 1 EAST = 1

View File

@ -7,6 +7,7 @@ TIMEOUT = 100
class EventManager: class EventManager:
keyTimeout = 0 keyTimeout = 0
#self.game.map
def __init__(self, gameObject, player): def __init__(self, gameObject, player):
self.game = gameObject self.game = gameObject
self.player = player self.player = player
@ -33,12 +34,19 @@ class EventManager:
# Key names are temporary # Key names are temporary
# TODO: Load key bindings from JSON # TODO: Load key bindings from JSON
if keys[pygame.K_w]: if keys[pygame.K_w] and not self.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w):
self.player.move(Rotations.NORTH) self.player.move(Rotations.NORTH)
if keys[pygame.K_s]: if keys[pygame.K_s] and not self.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w):
self.player.move(Rotations.SOUTH) self.player.move(Rotations.SOUTH)
if keys[pygame.K_d]: if keys[pygame.K_d] and not self.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y):
self.player.move(Rotations.EAST) self.player.move(Rotations.EAST)
if keys[pygame.K_a]: if keys[pygame.K_a] and not self.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y):
self.player.move(Rotations.WEST) 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

@ -28,6 +28,7 @@ class Game:
print("Initializing pygame...", end=" ") print("Initializing pygame...", end=" ")
pygame.init() pygame.init()
self.spritesList = pygame.sprite.Group() self.spritesList = pygame.sprite.Group()
print("OK") print("OK")
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ") print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ")
@ -39,16 +40,12 @@ class Game:
self.screen = Screen(self, self.config["window"]) self.screen = Screen(self, self.config["window"])
print("OK") print("OK")
self.mapDataFolder = path.dirname("../data/mapdata/") self.mapDataFolder = path.dirname("../data/mapdata/")
self.map = Map(path.join(self.mapDataFolder, 'map.txt'), self.screen) self.map = Map(path.join(self.mapDataFolder, 'map.txt'), self.screen)
self.player = Player((0, 0), self.map.tileSize) self.player = Player((0, 0), self.map.tileSize)
self.map.addEntity(self.player) self.map.addEntity(self.player)
self.eventManager = EventManager(self, self.player) self.eventManager = EventManager(self, self.player)
self.mainLoop() self.mainLoop()
def mainLoop(self): def mainLoop(self):

View File

@ -8,6 +8,7 @@ class Map:
self.terrain = [] self.terrain = []
self.entities = [] self.entities = []
self.collidableTerrain = [] self.collidableTerrain = []
self.collidables = pygame.sprite.Group()
with open(filename, 'rt') as f: with open(filename, 'rt') as f:
for line in f: for line in f:
@ -26,12 +27,13 @@ class Map:
for row, tiles in enumerate(self.terrain): for row, tiles in enumerate(self.terrain):
for col, tile in enumerate(tiles): for col, tile in enumerate(tiles):
if tile == 'w': 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.collidableTerrain.append(self.terrain) self.screen.draw(object, Locations.MAP, 0, 0)
self.collidables.add(object)
elif tile == ',': 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 == '.': 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): def addEntity(self, entity):
self.entities.append(entity) self.entities.append(entity)

View File

@ -4,10 +4,14 @@ from os import path
class TerrainTile(pygame.sprite.Sprite): class TerrainTile(pygame.sprite.Sprite):
def __init__(self, texture, tileSize): def __init__(self, x, y, texture, tileSize):
super().__init__() super().__init__()
self.imagesFolder = path.dirname("../data/images/") self.imagesFolder = path.dirname("../data/images/")
self.terrainFolder = path.join(self.imagesFolder, 'terrain') self.terrainFolder = path.join(self.imagesFolder, 'terrain')
self.image = pygame.image.load(os.path.join(self.terrainFolder, texture)).convert() self.image = pygame.image.load(os.path.join(self.terrainFolder, texture)).convert()
self.image = pygame.transform.scale(self.image, (tileSize, tileSize)) self.image = pygame.transform.scale(self.image, (tileSize, tileSize))
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * tileSize
self.rect.y = y * tileSize