This commit is contained in:
Suguivy 2023-03-21 19:36:19 +01:00
parent 3a49aa1dd4
commit 0c1f5224e2
4 changed files with 77 additions and 11 deletions

View File

@ -7,17 +7,40 @@ class Item(Entity):
pass
class Creature(Entity):
def __init__(self):
self.speed = 1
def __init__(self, codename):
self.speed = CREATURES_DATA[codename]["speed"]
self.max_hp = CREATURES_DATA[codename]["max_hp"]
self.hp = self.max_hp
self.damage = CREATURES_DATA[codename]["damage"]
self.action: Action = NA()
class Player(Creature):
def __init__(self):
super().__init__()
super().__init__("player")
self.main_hand = None
class Enemy(Creature):
def __init__(self):
super().__init__()
def __init__(self, codename):
super().__init__(codename)
def calculate_action(self):
self.action: Action = Idle()
self.action: Action = Idle()
CREATURES_DATA = {
"player": {
"max_hp": 20,
"speed": 10,
"damage": 1
},
"slime": {
"max_hp": 5,
"speed": 5,
"damage": 1
}
}
ITEMS_DATA = {
"none": {
"damage": 1
}
}

View File

@ -71,5 +71,4 @@ class Floor:
self.entities = EntityMap()
def get_tile(self, x, y) -> TileType:
print(x,y)
return self.grid[x][y]

34
game.py
View File

@ -20,6 +20,10 @@ class Game:
self.ticks = 0
self.schedule = {}
self.redirect_io('prints.log', 'errors.log')
self.hud_height = 3
self.menu_width = 20
self.camera = (0, 0)
self.hud = 0
def __del__(self):
self.restore_io()
@ -43,7 +47,7 @@ class Game:
Runs the game
"""
self.instance_creature(3, 3, self.player)
self.instance_creature(6, 4, Enemy())
self.instance_creature(6, 4, Enemy("slime"))
self.render()
while not self.should_exit:
self.step()
@ -71,8 +75,16 @@ class Game:
"""
Renders the game
"""
for x in range(self.floor.width):
for y in range(self.floor.height):
(term_width, term_height) = self.term.get_dimensions()
(floor_frame_width, floor_frame_height) = (term_width - self.menu_width, term_height - self.hud_height)
self.term.clear()
# Floor
for x in range(min(self.floor.width, floor_frame_width)):
for y in range(min(self.floor.height, floor_frame_height)):
if (x, y) in self.floor.entities.pos_creatures:
self.term.put_char(x, y, TEXTURES[self.floor.entities.pos_creatures[(x,y)].__class__])
elif (x, y) in self.floor.entities.items:
@ -80,6 +92,18 @@ class Game:
else:
self.term.put_char(x, y, TEXTURES[self.floor.get_tile(x, y)])
# HUD
for x in range(floor_frame_width):
for y in range(floor_frame_height, term_height):
self.term.put_char(x, y, ' ')
self.term.put_string(0, floor_frame_height, f"HP:{self.player.hp}/{self.player.max_hp}" )
self.term.put_string(0, floor_frame_height + 1, f"MH:{'None' if not self.player.main_hand else 'Item'}; Damage: {self.player.damage}" )
# Menu
for x in range(floor_frame_width, term_width):
for y in range(term_height):
self.term.put_char(x, y, 'M')
def step(self):
"""
Perfoms a step in the game. Sets the `action_was_performed` attribute accordingly
@ -123,6 +147,10 @@ class Game:
keycode = self.term.get_key()
if keycode == "q":
self.should_exit = True
elif keycode == "p":
self.hud += 1
print(self.hud, flush=True)
self.input_event()
elif keycode == "KEY_RIGHT":
self.player.action = Move(1, 0)
elif keycode == "KEY_LEFT":

View File

@ -1,4 +1,5 @@
import curses
from typing import Tuple
KEYCODES = {
curses.KEY_LEFT: "KEY_LEFT",
@ -27,6 +28,9 @@ class Terminal:
curses.echo()
curses.endwin()
def clear(self):
self.scr.erase()
def get_key(self) -> str:
"""
Blocks until a key is read
@ -41,4 +45,16 @@ class Terminal:
"""
Prints a char at the specified position
"""
self.scr.addch(y, x, char)
self.scr.insch(y, x, char)
def put_string(self, x, y, string):
"""
Prints a string from the specified position
"""
self.scr.addstr(y, x, string)
def get_dimensions(self) -> Tuple[int, int]:
"""
Returns the screen dimensions as (width, height)
"""
return self.scr.getmaxyx()[::-1]