Collisions are working
This commit is contained in:
parent
8641e7d13d
commit
54bb2d1416
|
@ -6,13 +6,13 @@ class Player(FightingEntity):
|
||||||
strength = 5
|
strength = 5
|
||||||
|
|
||||||
def move_up(self) -> None:
|
def move_up(self) -> None:
|
||||||
self.y -= 1
|
self.check_move(self.y - 1, self.x, True)
|
||||||
|
|
||||||
def move_down(self) -> None:
|
def move_down(self) -> None:
|
||||||
self.y += 1
|
self.check_move(self.y + 1, self.x, True)
|
||||||
|
|
||||||
def move_left(self) -> None:
|
def move_left(self) -> None:
|
||||||
self.x -= 1
|
self.check_move(self.y, self.x - 1, True)
|
||||||
|
|
||||||
def move_right(self) -> None:
|
def move_right(self) -> None:
|
||||||
self.x += 1
|
self.check_move(self.y, self.x + 1, True)
|
||||||
|
|
|
@ -23,7 +23,7 @@ class Game:
|
||||||
m = Map.load("example_map.txt")
|
m = Map.load("example_map.txt")
|
||||||
player = Player()
|
player = Player()
|
||||||
self.player = player
|
self.player = player
|
||||||
m.entities.append(player)
|
m.add_entity(player)
|
||||||
player.move(1, 6)
|
player.move(1, 6)
|
||||||
d = MapDisplay(m, player)
|
d = MapDisplay(m, player)
|
||||||
while True:
|
while True:
|
||||||
|
@ -43,4 +43,3 @@ class Game:
|
||||||
self.player.move_left()
|
self.player.move_left()
|
||||||
if key == 'd' or key == 'KEY_RIGHT':
|
if key == 'd' or key == 'KEY_RIGHT':
|
||||||
self.player.move_right()
|
self.player.move_right()
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,18 @@ class Map:
|
||||||
height: int
|
height: int
|
||||||
tiles: list
|
tiles: list
|
||||||
|
|
||||||
def __init__(self, width: int, height: int, tiles: list,
|
def __init__(self, width: int, height: int, tiles: list):
|
||||||
entities: list = None):
|
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.tiles = tiles
|
self.tiles = tiles
|
||||||
self.entities = entities or []
|
self.entities = []
|
||||||
|
|
||||||
|
def add_entity(self, entity: "Entity") -> None:
|
||||||
|
"""
|
||||||
|
Register a new entity in the map.
|
||||||
|
"""
|
||||||
|
self.entities.append(entity)
|
||||||
|
entity.map = self
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load(filename: str):
|
def load(filename: str):
|
||||||
|
@ -39,7 +45,7 @@ class Map:
|
||||||
tiles = [[Tile(c)
|
tiles = [[Tile(c)
|
||||||
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
||||||
|
|
||||||
return Map(width, height, tiles, [])
|
return Map(width, height, tiles)
|
||||||
|
|
||||||
def draw_string(self) -> str:
|
def draw_string(self) -> str:
|
||||||
"""
|
"""
|
||||||
|
@ -68,11 +74,19 @@ class Tile(Enum):
|
||||||
class Entity:
|
class Entity:
|
||||||
y: int
|
y: int
|
||||||
x: int
|
x: int
|
||||||
|
map: Map
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.y = 0
|
self.y = 0
|
||||||
self.x = 0
|
self.x = 0
|
||||||
|
|
||||||
|
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
|
||||||
|
-> bool:
|
||||||
|
tile = self.map.tiles[y][x]
|
||||||
|
if tile.can_walk() and move_if_possible:
|
||||||
|
self.move(y, x)
|
||||||
|
return tile.can_walk()
|
||||||
|
|
||||||
def move(self, y: int, x: int) -> None:
|
def move(self, y: int, x: int) -> None:
|
||||||
self.y = y
|
self.y = y
|
||||||
self.x = x
|
self.x = x
|
||||||
|
|
|
@ -59,8 +59,8 @@ class TestEntities(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
item = Bomb()
|
item = Bomb()
|
||||||
squirrel = Squirrel()
|
squirrel = Squirrel()
|
||||||
self.map.entities.append(item)
|
self.map.add_entity(item)
|
||||||
self.map.entities.append(squirrel)
|
self.map.add_entity(squirrel)
|
||||||
squirrel.health = 2
|
squirrel.health = 2
|
||||||
squirrel.move(41, 42)
|
squirrel.move(41, 42)
|
||||||
item.act(self.map)
|
item.act(self.map)
|
||||||
|
|
Loading…
Reference in New Issue