From 1c284bad97d118121dec426aa45d83d8da65a122a59a301b3a8ea040bb27c74b Mon Sep 17 00:00:00 2001 From: Crystal Date: Thu, 24 Jul 2025 22:36:36 +0200 Subject: [PATCH] basic player entity and debug module to quickly test player attack --- debug.py | 38 +++++++++++++++ entities/player.py | 114 ++++++++++++++++++++++++++++++--------------- main.py | 14 +++--- 3 files changed, 122 insertions(+), 44 deletions(-) create mode 100644 debug.py diff --git a/debug.py b/debug.py new file mode 100644 index 0000000..b5fac40 --- /dev/null +++ b/debug.py @@ -0,0 +1,38 @@ +import time as _time +import random as _rand +import main as _main +from entities import weapon_syms as _wsyms + +def init_debug(): + players= [ + { + 'name': 'Elara', + }, + { + 'name': 'Kaelen', + }, + { + 'name': 'Zephyr', + } + ] + w= _wsyms.KNIFE + #weapons= [{_wsyms.WEAPONS[w]['name' ]: 1}] + weapons= [{w: 1}] + + Arena= _main.init_arena(players, weapons) + print(f'Players: {Arena.get_players()}') + print(f'Weapons: {Arena.get_weapons()}') + while (len(Arena.get_alive_players()) > 1): + alive_players= Arena.get_alive_players() + + p_one, p_two= _rand.sample(alive_players, 2) + p_one.attack(p_two) + + #Start a day + #At 23:59: + Arena.next_day() + _time.sleep(0.3) + #End of day + +if __name__ == '__main__': + init_debug() diff --git a/entities/player.py b/entities/player.py index 1c68810..3a5b60b 100644 --- a/entities/player.py +++ b/entities/player.py @@ -1,72 +1,112 @@ import random as _random +import uuid as _uuid class BrSimPlayer(): def __init__(self, name, inventory= None): + self.id= str(_uuid.uuid4) self.name= name self.health= 1 self.inventory= inventory or [] self.damage= 1 # this is the punch damage amount self.max_weight= 5 # this is the max inventory weight self.agility= 10 # chance to avoid an hit + self.kills= 0 # track the number of kills + self.survived_days= 0 # track the number of the survived days + self.equipped_weapon= None - def is_alive(self): - return self.health > 0 + ### control methods - def attack(self, target): - if not self.is_alive(): return - if not target.is_alive(): return - if target.try_to_avoid_hit(): return # print something like 'enemy doges the attacl' - - target.accuses_damage(self.damage) + def get_id(self): + return self.id - def accuses_damage(self, damage): - self.health -= damage - if self.health <= 0: - self.health = 0 - # show something like 'player is dead' - else: - # show something like 'get hit' - pass - - def try_to_avoid_hit(self): - # maybe depend on the attack, if it is a gun shot it's quite impossible to dodge - rnd= _random.randint(0, 100) - if rnd < self.agility: return True - return False - - def steal(self): - #XXX can steal from death players or from sleeping players - pass - - def escape(self): - #XXX It can run away from the fighting - pass - - def heal(self): - #XXX if you have a wound and you have a medikit item, you can heal your wound or sickness - pass + def get_name(self): + return self.name def get_inventory(self): return self.inventory - def get_name(self): - return self.name + def get_inventory_weight(self): + weight= 0 + for inv in self.get_inventory(): + weight+= inv.get_weight() + return weight + + def get_max_weight(self): + return self.max_weight def get_health(self): return self.health def get_damage(self): - return self.damage + if not self.equipped_weapon: return self.damage + return self.equipped_weapon.damage def get_agility(self): return self.agility def get_data(self): return { + 'id': self.get_id(), 'name': self.get_name(), 'inventory': self.get_inventory(), + 'inventory_weight': self.get_inventory_weight(), 'health': self.get_health(), 'damage': self.get_damage(), 'agility': self.get_agility(), } + + def is_alive(self): + return self.health > 0 + + ### player actions + + def _equip_weapon(self): + if not self.inventory: return + + available_weapons= [] + for inv in self.get_inventory(): + # XXX + # i don't know yet if this is ok, + # we'll see it when weapon and items are defined + if not inv.damage: continue + available_weapons.append(inv) + self.equipped_weapon= random.sample(available_weapons, 1)[0] + + def dodge(self): + # maybe depend on the attack, if it is a gun shot it's quite impossible to dodge + rnd= _random.randint(0, 100) + if rnd < self.agility: return True + return False + + def accuses_damage(self, damage): + self.health -= damage + if self.health > 0: return self.get_health() + + self.health = 0 + print('Guarino, perdonami se sono morto x.x') + return damage + + def attack(self, target): + self._equip_weapon() + if target.dodge(): + print('Ehhhh voleviiii!!! sei lentoo!') + return 0 + target.accuses_damage(self.damage) + return self.damage + + def get_item(self, item): + if self.get_inventory_weight() + item.get_weight() >= self.get_max_weight(): + print('Sono sovraccarico, non posso prendere questo oggetto') + return False + self.inventory.append(item) + + def escape(self): + # TODO It can run away from the fighting + return + + def heal(self): + # TODO heal system + # if you have a wound and you have a medikit item, + # you can heal your wound or sickness + return diff --git a/main.py b/main.py index 71eda53..0226e2f 100644 --- a/main.py +++ b/main.py @@ -2,20 +2,20 @@ import random as _random from entities import weapon_syms as _wsyms from entities import arena as _arena -def init_arena(): - players= [{'name': 'Crystal'}, {'name': 'Andrea'}] - w= _wsyms.KNIFE - #weapons= [{_wsyms.WEAPONS[w]['name' ]: 1}] - weapons= [{w: 1}] +def init_arena(players, weapons): return _arena.BrSimArena(players, weapons) - def run_events(Arena): #A event for each player: pass def local_debug(): - Arena= init_arena() + players= [{'name': 'Crystal'}, {'name': 'Andrea'}] + w= _wsyms.KNIFE + #weapons= [{_wsyms.WEAPONS[w]['name' ]: 1}] + weapons= [{w: 1}] + + Arena= init_arena(players, weapons) print(f'Players: {Arena.get_players()}') print(f'Weapons: {Arena.get_weapons()}') while (len(Arena.get_alive_players()) > 1):