did invetory and some prompts

This commit is contained in:
Bizcochito 2023-03-22 18:17:44 +01:00
parent 0c1f5224e2
commit a1e46af961
4 changed files with 57 additions and 20 deletions

View File

@ -1,3 +1,4 @@
from typing import Dict
from actions import Action, Idle, NA from actions import Action, Idle, NA
class Entity(): class Entity():
@ -17,13 +18,25 @@ class Creature(Entity):
class Player(Creature): class Player(Creature):
def __init__(self): def __init__(self):
super().__init__("player") super().__init__("player")
self.main_hand = None self.equipment: Dict[str, Dict | None] = {
"main_hand": None,
"feet": None
}
def equip(self, item, slot) -> None:
self.equipment[slot] = ITEMS_DATA[item]
def total_damage(self) -> int:
dmg = self.damage
for item in self.equipment.values():
if item: dmg += item["damage"]
return dmg
class Enemy(Creature): class Enemy(Creature):
def __init__(self, codename): def __init__(self, codename):
super().__init__(codename) super().__init__(codename)
def calculate_action(self): def calculate_action(self) -> None:
self.action: Action = Idle() self.action: Action = Idle()
CREATURES_DATA = { CREATURES_DATA = {
@ -40,7 +53,12 @@ CREATURES_DATA = {
} }
ITEMS_DATA = { ITEMS_DATA = {
"none": { "shiv": {
"damage": 1 "damage": 3,
"name": "Shiv"
},
"fire_gem": {
"damage": 1,
"name": "Fire gem"
} }
} }

View File

@ -65,7 +65,7 @@ class Floor:
self.grid = [] self.grid = []
for row in range(width): for row in range(width):
self.grid.append([]) self.grid.append([])
for tile in range(height): for _ in range(height):
self.grid[row].append(TileType.AIR) self.grid[row].append(TileType.AIR)
self.grid[0][0] = TileType.WALL self.grid[0][0] = TileType.WALL
self.entities = EntityMap() self.entities = EntityMap()

38
game.py
View File

@ -1,5 +1,5 @@
from terminal import Terminal from terminal import Terminal
from entity import Creature, Player, Enemy from entity import ITEMS_DATA, Creature, Player, Enemy
from actions import * from actions import *
from floor import COLLIDABLE, Floor, TileType from floor import COLLIDABLE, Floor, TileType
import sys import sys
@ -48,6 +48,7 @@ class Game:
""" """
self.instance_creature(3, 3, self.player) self.instance_creature(3, 3, self.player)
self.instance_creature(6, 4, Enemy("slime")) self.instance_creature(6, 4, Enemy("slime"))
self.player.equip("shiv", "main_hand")
self.render() self.render()
while not self.should_exit: while not self.should_exit:
self.step() self.step()
@ -96,13 +97,17 @@ class Game:
for x in range(floor_frame_width): for x in range(floor_frame_width):
for y in range(floor_frame_height, term_height): for y in range(floor_frame_height, term_height):
self.term.put_char(x, y, ' ') 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, 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}" ) self.term.put_string(0, floor_frame_height + 1, f"MH: {'None' if not self.player.equipment['main_hand'] else self.player.equipment['main_hand']['name']}; Damage: {self.player.total_damage()}")
# Menu # Menu
for x in range(floor_frame_width, term_width): for x in range(floor_frame_width, term_width):
for y in range(term_height): for y in range(term_height):
self.term.put_char(x, y, 'M') self.term.put_char(x, y, ' ')
self.term.put_string(floor_frame_width, 0, f"Move: {KEYBINDS['move_left']} {KEYBINDS['move_up']} {KEYBINDS['move_right']} {KEYBINDS['move_down']}")
self.term.put_string(floor_frame_width, 1, f"Inventory: {KEYBINDS['inventory']}")
self.term.put_string(floor_frame_width, 2, f"Quit: {KEYBINDS['quit']}")
def step(self): def step(self):
""" """
@ -145,17 +150,30 @@ class Game:
Waits for a game event to happen, and behaves accordingly Waits for a game event to happen, and behaves accordingly
""" """
keycode = self.term.get_key() keycode = self.term.get_key()
if keycode == "q": if keycode == KEYBINDS["quit"]:
self.should_exit = True self.should_exit = True
elif keycode == "p": elif keycode == KEYBINDS["inventory"]:
self.hud += 1 self.hud += 1
print(self.hud, flush=True) print(self.hud, flush=True)
self.input_event() self.input_event()
elif keycode == "KEY_RIGHT": elif keycode == KEYBINDS["move_right"]:
self.player.action = Move(1, 0) self.player.action = Move(1, 0)
elif keycode == "KEY_LEFT": elif keycode == KEYBINDS["move_left"]:
self.player.action = Move(-1, 0) self.player.action = Move(-1, 0)
elif keycode == "KEY_DOWN": elif keycode == KEYBINDS["move_down"]:
self.player.action = Move(0, 1) self.player.action = Move(0, 1)
elif keycode == "KEY_UP": elif keycode == KEYBINDS["move_up"]:
self.player.action = Move(0, -1) self.player.action = Move(0, -1)
elif keycode == "KEY_RESIZE":
self.render()
self.input_event()
KEYBINDS = {
"move_right": "",
"move_left": "",
"move_down": "",
"move_up": "",
"quit": "q",
"inventory": "i",
}

View File

@ -2,10 +2,11 @@ import curses
from typing import Tuple from typing import Tuple
KEYCODES = { KEYCODES = {
curses.KEY_LEFT: "KEY_LEFT", curses.KEY_LEFT: "",
curses.KEY_RIGHT: "KEY_RIGHT", curses.KEY_RIGHT: "",
curses.KEY_UP: "KEY_UP", curses.KEY_UP: "",
curses.KEY_DOWN: "KEY_DOWN" curses.KEY_DOWN: "",
curses.KEY_RESIZE: "KEY_RESIZE"
} }
class Terminal: class Terminal: