This commit is contained in:
Yohann D'ANELLO 2020-11-06 15:33:26 +01:00
parent e00d98739a
commit 8ccb74ea54
8 changed files with 83 additions and 63 deletions

View File

@ -1,33 +1,36 @@
from ..interfaces import Entity, FightingEntity from ..interfaces import Entity, FightingEntity, Map
class Item(Entity): class Item(Entity):
held: bool held: bool
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs) super().__init__(*args, **kwargs)
self.held = False self.held = False
def drop(self, x:int, y:int): def drop(self, x: int, y: int) -> None:
self.held = False self.held = False
self.move(x, y) self.move(x, y)
def hold(self): def hold(self) -> None:
self.held = True self.held = True
class Bomb(Item): class Bomb(Item):
damage: int = 5 damage: int = 5
exploding: bool exploding: bool
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs) super().__init__(*args, **kwargs)
self.exploding = False self.exploding = False
def drop(self, x:int, y:int): def drop(self, x: int, y: int) -> None:
super.drop(self, x, y) super().drop(x, y)
self.exploding = True self.exploding = True
def act(self, map): def act(self, m: Map) -> None:
if self.exploding: if self.exploding:
for e in map.entities: for e in m.entities:
if abs (e.x - self.x) + abs (e.y - self.y) <= 1 and isinstance(e,FightingEntity): if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
isinstance(e, FightingEntity):
e.take_damage(self, self.damage) e.take_damage(self, self.damage)

View File

@ -1,9 +1,11 @@
from ..interfaces import FightingEntity from ..interfaces import FightingEntity, Map
class Monster(FightingEntity): class Monster(FightingEntity):
def act(self, map): def act(self, map: Map) -> None:
pass pass
class Squirrel(Monster): class Squirrel(Monster):
maxhealth = 10 maxhealth = 10
strength = 3 strength = 3

View File

@ -1,5 +1,6 @@
from ..interfaces import FightingEntity from ..interfaces import FightingEntity
class Player(FightingEntity): class Player(FightingEntity):
maxhealth = 20 maxhealth = 20
strength = 5 strength = 5

View File

@ -1,3 +1,5 @@
from typing import Any
from .interfaces import Map from .interfaces import Map
from .mapdisplay import MapDisplay from .mapdisplay import MapDisplay
from .settings import Settings from .settings import Settings
@ -15,7 +17,7 @@ class Game:
with TermManager() as term_manager: with TermManager() as term_manager:
self._start_game(term_manager.screen) self._start_game(term_manager.screen)
def _start_game(self, screen): def _start_game(self, screen: Any) -> None:
# TODO Generate map, or make the possibility to load another one # TODO Generate map, or make the possibility to load another one
m = Map.load("example_map.txt") m = Map.load("example_map.txt")
player = Player() player = Player()
@ -26,11 +28,11 @@ class Game:
while True: while True:
screen.clear() screen.clear()
screen.refresh() screen.refresh()
d.display(player.getPosY(), player.getPosX()) d.display(player.y, player.y)
key = screen.getkey() key = screen.getkey()
self.handle_key_pressed(key) self.handle_key_pressed(key)
def handle_key_pressed(self, key): def handle_key_pressed(self, key: str) -> None:
# TODO load keys from settings # TODO load keys from settings
if key == 'z' or key == 'KEY_UP': if key == 'z' or key == 'KEY_UP':
self.player.move_up() self.player.move_up()
@ -47,20 +49,14 @@ class Player:
y: int = 0 y: int = 0
x: int = 0 x: int = 0
def move_up(self): def move_up(self) -> None:
self.y -= 1 self.y -= 1
def move_down(self): def move_down(self) -> None:
self.y += 1 self.y += 1
def move_left(self): def move_left(self) -> None:
self.x -= 1 self.x -= 1
def move_right(self): def move_right(self) -> None:
self.x += 1 self.x += 1
def getPosX(self):
return self.x
def getPosY(self):
return self.y

View File

@ -11,11 +11,12 @@ class Map:
height: int height: int
tiles: list tiles: list
def __init__(self, width: int, height: int, tiles: list, entities = []): 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 self.entities = entities or []
@staticmethod @staticmethod
def load(filename: str): def load(filename: str):
@ -40,7 +41,6 @@ class Map:
return Map(width, height, tiles, []) return Map(width, height, tiles, [])
def draw_string(self) -> str: def draw_string(self) -> str:
""" """
Draw the current map as a string object that can be rendered Draw the current map as a string object that can be rendered
@ -73,9 +73,14 @@ class Entity:
self.x = x self.x = x
self.y = y self.y = y
def act(self, m:Map): def act(self, m: Map) -> None:
"""
Define the action of the entity that is ran each tick.
By default, does nothing.
"""
pass pass
class FightingEntity(Entity): class FightingEntity(Entity):
maxhealth: int maxhealth: int
health: int health: int
@ -84,10 +89,10 @@ class FightingEntity(Entity):
def __init__(self): def __init__(self):
self.health = self.maxhealth self.health = self.maxhealth
def hit(self, opponent) -> None: def hit(self, opponent: "FightingEntity") -> None:
opponent.take_damage(self, self.strength) opponent.take_damage(self, self.strength)
def take_damage(self, attacker, amount:int) -> None: def take_damage(self, attacker: "Entity", amount: int) -> None:
self.health -= amount self.health -= amount
if self.health <= 0: if self.health <= 0:
self.die() self.die()

View File

@ -1,21 +1,24 @@
#!/usr/bin/env python #!/usr/bin/env python
import curses import curses
from typing import Any
from dungeonbattle.interfaces import Map from dungeonbattle.interfaces import Map
class MapDisplay:
def __init__(self, m: Map, player): class MapDisplay:
def __init__(self, m: Map, player: Any):
# TODO Type the player field with the good type
self.map = m self.map = m
self.pad = curses.newpad(m.height, m.width + 1) self.pad = curses.newpad(m.height, m.width + 1)
self.player = player self.player = player
def update_pad(self): def update_pad(self) -> None:
self.pad.addstr(0, 0, self.map.draw_string()) self.pad.addstr(0, 0, self.map.draw_string())
for e in self.map.entities: for e in self.map.entities:
self.pad.addch(e.y, e.x, e.img) self.pad.addch(e.y, e.x, e.img)
self.pad.addstr(self.player.getPosY(), self.player.getPosX(), '🐿') self.pad.addstr(self.player.getPosY(), self.player.getPosX(), '🐿')
def display(self, y, x): def display(self, y: int, x: int) -> None:
deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS // 2) + 1 deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS // 2) + 1
pminrow, pmincol = y - deltay, x - deltax pminrow, pmincol = y - deltay, x - deltax
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0) sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)

View File

@ -11,14 +11,22 @@ class Settings:
We can define the setting by simply use settings.TEXTURE_PACK = 'new_key' We can define the setting by simply use settings.TEXTURE_PACK = 'new_key'
""" """
def __init__(self): def __init__(self):
self.KEY_UP_PRIMARY = ['z', 'Touche principale pour aller vers le haut'] self.KEY_UP_PRIMARY = \
self.KEY_UP_SECONDARY = ['KEY_UP', 'Touche secondaire pour aller vers le haut'] ['z', 'Touche principale pour aller vers le haut']
self.KEY_DOWN_PRIMARY = ['s', 'Touche principale pour aller vers le bas'] self.KEY_UP_SECONDARY = \
self.KEY_DOWN_SECONDARY = ['KEY_DOWN', 'Touche secondaire pour aller vers le bas'] ['KEY_UP', 'Touche secondaire pour aller vers le haut']
self.KEY_LEFT_PRIMARY = ['q', 'Touche principale pour aller vers la gauche'] self.KEY_DOWN_PRIMARY = \
self.KEY_LEFT_SECONDARY = ['KEY_LEFT', 'Touche secondaire pour aller vers la gauche'] ['s', 'Touche principale pour aller vers le bas']
self.KEY_RIGHT_PRIMARY = ['d', 'Touche principale pour aller vers la droite'] self.KEY_DOWN_SECONDARY = \
self.KEY_RIGHT_SECONDARY = ['KEY_RIGHT', 'Touche secondaire pour aller vers la droite'] ['KEY_DOWN', 'Touche secondaire pour aller vers le bas']
self.KEY_LEFT_PRIMARY = \
['q', 'Touche principale pour aller vers la gauche']
self.KEY_LEFT_SECONDARY = \
['KEY_LEFT', 'Touche secondaire pour aller vers la gauche']
self.KEY_RIGHT_PRIMARY = \
['d', 'Touche principale pour aller vers la droite']
self.KEY_RIGHT_SECONDARY = \
['KEY_RIGHT', 'Touche secondaire pour aller vers la droite']
self.TEXTURE_PACK = ['ASCII', 'Pack de textures utilisé'] self.TEXTURE_PACK = ['ASCII', 'Pack de textures utilisé']
def __getattribute__(self, item: str) -> Any: def __getattribute__(self, item: str) -> Any:

View File

@ -18,8 +18,10 @@ class TestSettings(unittest.TestCase):
self.assertEqual(settings.KEY_LEFT_SECONDARY, 'KEY_LEFT') self.assertEqual(settings.KEY_LEFT_SECONDARY, 'KEY_LEFT')
self.assertEqual(settings.KEY_RIGHT_SECONDARY, 'KEY_RIGHT') self.assertEqual(settings.KEY_RIGHT_SECONDARY, 'KEY_RIGHT')
self.assertEqual(settings.TEXTURE_PACK, 'ASCII') self.assertEqual(settings.TEXTURE_PACK, 'ASCII')
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), settings.get_comment('TEXTURE_PACK')) self.assertEqual(settings.get_comment(settings.TEXTURE_PACK),
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), 'Pack de textures utilisé') settings.get_comment('TEXTURE_PACK'))
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK),
'Pack de textures utilisé')
settings.TEXTURE_PACK = 'UNICODE' settings.TEXTURE_PACK = 'UNICODE'
self.assertEqual(settings.TEXTURE_PACK, 'UNICODE') self.assertEqual(settings.TEXTURE_PACK, 'UNICODE')