-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from mattiasstjernstrom/Entity-working-branch
- Loading branch information
Showing
9 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"Fireball": { | ||
"name": "Fireball", | ||
"min_damage": 10, | ||
"max_damage": 20, | ||
"description": "A fiery ball that deals moderate damage to the enemy." | ||
}, | ||
"Heal": { | ||
"name": "Heal", | ||
"min_damage": -10, | ||
"max_damage": -20, | ||
"description": "Moderately heal yourself" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"Aggressive": { | ||
"action": "attack", | ||
"chance": 70, | ||
"description": "The enemy is more likely to attack." | ||
}, | ||
"Defensive": { | ||
"action": "defend", | ||
"chance": 30, | ||
"description": "The enemy is more likely to defend." | ||
}, | ||
"Ability": { | ||
"action": "ability", | ||
"chance": 80, | ||
"description": "The enemy is using an ability" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from .Entity import Entity | ||
import json | ||
import os | ||
|
||
class Character(Entity): | ||
def __init__(self, name, hp=100, attack_value=5, defence_value=1): | ||
super().__init__(name, hp, attack_value, defence_value) | ||
self.abilities = {} | ||
|
||
def load_abilities(self): | ||
file_path = os.path.join(os.path.dirname(__file__), 'Abilities.json') | ||
with open(file_path, 'r') as f: | ||
self.abilities.update(json.load(f)) | ||
|
||
|
||
|
||
def choose_action(self): | ||
print("Choose an action:\n1. Attack\n2. Defend\n3. Use Ability") | ||
choice = input("Enter the number of choice: ") | ||
return int(choice) | ||
|
||
def choose_ability(self): | ||
ability_names = list(self.abilities.keys()) | ||
print("Choose an ability: ") | ||
for i, ability in enumerate(ability_names, start=1): | ||
print(f"{i}. {ability}") | ||
choice = input("Enter the number of the ability: ") | ||
return int(choice) | ||
|
||
def player_use_ability(self, ability_index, target): | ||
ability_names = list(self.abilities.keys()) | ||
ability_name = ability_names[ability_index - 1] # Convert index to name | ||
super().use_ability(self.abilities, ability_name, target) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from .Characters import Character | ||
from .Enemies import Enemy | ||
|
||
class Combat: | ||
def __init__(self, player, enemy): | ||
""" | ||
Initialize the combat with a player and an enemy. | ||
""" | ||
self.player = player | ||
self.enemy = enemy | ||
|
||
def fight(self): | ||
""" | ||
Run the combat loop until the player or enemy has been defeated | ||
""" | ||
while self.player.is_alive and self.enemy.is_alive: | ||
self.player_turn() | ||
if not self.enemy.is_alive: | ||
print(f"{self.enemy.name} has been defeated!") | ||
break | ||
self.enemy_turn() | ||
if not self.player.is_alive: | ||
print(f"{self.player.name} has been defeated!") | ||
break | ||
|
||
def player_turn(self): | ||
""" | ||
The players turn to deal damage. | ||
""" | ||
print(f"{self.player.name}'s turn") | ||
choice = self.player.choose_action() | ||
if choice == 1: | ||
damage = self.player.attack(self.enemy) | ||
print(f"{self.player.name} attacked {self.enemy.name} for {damage} damage! {self.enemy.name} has {self.enemy.hp:.2f} HP left") | ||
elif choice == 2: | ||
self.player.defend() | ||
elif choice == 3: | ||
ability_choice = self.player.choose_ability() | ||
damage = self.player.player_use_ability(ability_choice, self.enemy) | ||
|
||
def enemy_turn(self): | ||
""" | ||
The enemy turn to deal damage. | ||
""" | ||
print(f"{self.enemy.name}'s turn!") | ||
self.enemy.choose_action(self.player) | ||
print(f"{self.player.name} has {self.player.hp} HP left.") | ||
|
||
def run_game(): | ||
player = Character("Daniel", 100, 10, 5) | ||
enemy = Enemy("Mattias", 80, 7, 3) | ||
|
||
player.load_abilities() | ||
enemy.load_abilities() | ||
enemy.load_behaviors() | ||
|
||
# Start combat | ||
combat = Combat(player, enemy) | ||
combat.fight() | ||
|
||
|
||
if __name__ == "__main__": | ||
run_game() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from .Entity import Entity | ||
import json | ||
import random | ||
import os | ||
|
||
|
||
class Enemy(Entity): | ||
def __init__(self, name, hp=100, attack_value=5, defence_value=1): | ||
super().__init__(name, hp, attack_value, defence_value) | ||
self.abilities = {} | ||
self.behaviors = {} | ||
|
||
def load_abilities(self): | ||
file_path = os.path.join(os.path.dirname(__file__), 'Enemy_abilities.json') | ||
with open(file_path, 'r') as f: | ||
self.abilities.update(json.load(f)) | ||
|
||
def load_behaviors(self): | ||
file_path = os.path.join(os.path.dirname(__file__), 'Behaviors.json') | ||
with open(file_path, 'r') as f: | ||
self.behaviors.update(json.load(f)) | ||
|
||
def choose_action(self, target): | ||
behavior_values = list(self.behaviors.values()) | ||
behavior = random.choices(behavior_values, weights=[b['chance'] for b in behavior_values],k=1)[0] | ||
if behavior['action'] == 'attack': | ||
self.attack(target) | ||
elif behavior['action'] == 'defend': | ||
self.defend() | ||
elif behavior['action'] == 'ability': | ||
ability = random.choice(list(self.abilities.keys())) | ||
self.enemy_use_ability(ability, target) | ||
else: | ||
#handle other behaviors | ||
pass | ||
|
||
def enemy_use_ability(self, ability, target): | ||
super().use_ability(self.abilities, ability, target) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"Dark Strike": { | ||
"name": "Dark Strike", | ||
"min_damage": 15, | ||
"max_damage": 30, | ||
"description": "A dark energy attack that deals significant damage to the enemy." | ||
}, | ||
"Curse": { | ||
"name": "Curse", | ||
"min_damage": 10, | ||
"max_damage": 20, | ||
"description": "A curse that deals damage and lowers the enemy's defense." | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import random | ||
|
||
class Entity: | ||
def __init__(self, name, hp=100, attack_value=5, defence_value=1): | ||
self._name = name | ||
self._hp = hp | ||
self._attack_value= attack_value | ||
self._defence_value= defence_value | ||
self.is_alive = True | ||
|
||
@property | ||
def name(self): | ||
return self._name | ||
@name.setter | ||
def name(self, new_name): | ||
self._name = new_name | ||
|
||
@property | ||
def hp(self): | ||
return self._hp | ||
@hp.setter | ||
def hp(self, new_hp): | ||
self._hp = new_hp | ||
|
||
@property | ||
def attack_value(self): | ||
return self._attack_value | ||
@attack_value.setter | ||
def attack_value(self, new_attack_value): | ||
self._attack_value = new_attack_value | ||
|
||
@property | ||
def defence_value(self): | ||
return self._defence_value | ||
@defence_value.setter | ||
def defence_value(self, new_defence_value): | ||
self._defence_value = new_defence_value | ||
|
||
def defend(self): | ||
self.defence_value *= 2 | ||
print(f"{self.name} is defending and has doubled their defence value!") | ||
|
||
def attack(self, target): #snor denna från förra arbetet | ||
base_damage = self.attack_value - target.defence_value | ||
damage = round(random.uniform( | ||
max(0.1 * self.attack_value, base_damage) * 0.8, | ||
max(0.1 * self.attack_value, base_damage) * 1.2)) | ||
target.receive_damage(damage) | ||
return damage | ||
|
||
def receive_damage(self, damage): | ||
self.hp -= damage | ||
if self.hp <= 0: | ||
self._hp = 0 | ||
self.is_alive = False | ||
|
||
def use_ability(self, abilities, ability, target): | ||
ability = abilities.get(ability) | ||
if ability is None: | ||
print(f"Ability {ability} not found.") | ||
return | ||
min_damage = ability.get('min_damage', 0) | ||
max_damage = ability.get('max_damage', 0) | ||
damage = round(random.uniform(min_damage, max_damage)) | ||
target.receive_damage(damage) | ||
print(f"{self.name} used {ability['name']} on {target.name} for {damage} damage!") | ||
return damage |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import os | ||
import sys | ||
current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
parent_dir = os.path.dirname(current_dir) | ||
sys.path.append(parent_dir) | ||
|
||
from Entities.common.Combat import run_game | ||
|
||
|
||
def run(): | ||
print('Running game!') | ||
run_game() | ||
|
||
if __name__ == '__main__': | ||
run() |