Skip to content

Commit cad592e

Browse files
committed
full game commit
1 parent 5dcbfd0 commit cad592e

20 files changed

+192
-0
lines changed
559 Bytes
Binary file not shown.
2.08 KB
Binary file not shown.
552 Bytes
Binary file not shown.
563 Bytes
Binary file not shown.
1.31 KB
Binary file not shown.

characters/archer.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .characters import Character
2+
3+
class Archer(Character):
4+
"""Archer character class"""
5+
def __init__(self, name = False):
6+
super().__init__(350, 80, 30, 50, name)

characters/archers.py

Whitespace-only changes.

characters/characters.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import random
2+
3+
class Character():
4+
"""DnD main class used as generic character definition
5+
attributs: name, life, attack, defense, agility
6+
methods: attacks, escape
7+
"""
8+
actions = {
9+
'a': 'attack',
10+
'f': 'flee'
11+
}
12+
13+
def __init__(self, life, attack, defense, agility, name = False):
14+
self.name = name
15+
self.life = life
16+
self.attack = attack
17+
self.defense = defense
18+
self.agility = agility
19+
20+
def fight(self, target):
21+
"""This function triggers an attack between two oppoents.
22+
As a result life and defense attributs are impacted
23+
"""
24+
print("{} is figthing against {}".format(self.name, target.name))
25+
# Give target the pportunity to avoid the attack
26+
# Exemple: 50% chance to dodge when agility set to 50
27+
if random.randrange(0,100) <= target.agility:
28+
print("{} has dodged opponent".format(target.name))
29+
return False
30+
# traget's life is reduced by opponent attack level less target's defense divided by 5
31+
target.life -= self.attack - (target.defense/5)
32+
# No negative life attribut allowed -> forced to 0
33+
if target.life < 0:
34+
target.life = 0
35+
print("{} life expectancy is now {}".format(target.name, target.life))
36+
return True
37+
38+
def dodge(self):
39+
"""This function trigger's character to dodge, in short leave the fight"""
40+
# Lower agility
41+
agi = round(self.agility/5)
42+
if random.randrange(0,100) <= agi:
43+
return True
44+
return False
45+
46+
def decision(self, action, enemy):
47+
"""This function implements decided action (attack/flee)"""
48+
if action not in self.actions:
49+
print('In your dream!')
50+
return False
51+
if action == 'a':
52+
self.fight(enemy)
53+
elif action == 'f':
54+
if self.dodge():
55+
# Exception raised on successul escape as fight comes to an end
56+
print("Lucky you, that was a close shave!!")
57+
raise Exception('Well dodged hero!')
58+
else:
59+
print('The enemy is right in your back!')
60+
elif action == 'h':
61+
self.get_cure()
62+
return True
63+
64+
def __repr__(self):
65+
return "{} : life = {}".format(self.name, self.life)
66+

characters/orc.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .characters import Character
2+
3+
class Orc(Character):
4+
"""Orc character class"""
5+
def __init__(self, name = ""):
6+
super().__init__(400, 40, 70, 20, name)

characters/warrior.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .characters import Character
2+
3+
class Warrior(Character):
4+
"""Warrior character lassc"""
5+
def __init__(self, name = False):
6+
super().__init__(500, 50, 80, 10, name)

characters/wizard.py

Whitespace-only changes.

characters/wizzard.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from .characters import Character
2+
3+
class Wizzard(Character):
4+
"""Wizzard character class.
5+
Attributs: mana
6+
methods = heal, __repr__"""
7+
actions: Character.actions.update({'h': 'heal itself'})
8+
9+
def __init__(self, name = False):
10+
super().__init__(600, 20, 50, 25, name)
11+
# Magical attribut
12+
self.mana = 200
13+
14+
def get_cure(self):
15+
"""This function brings additionnal amount of life to the wizzard thanks to his supernatural power (or mana)"""
16+
# Needs 50 pts of mana to heal itself
17+
if self.mana >= 50:
18+
print("{} uses some of his supernatural power".format(self.name))
19+
self.mana -= 50
20+
self.life += 20
21+
print("{} regains 20 pts of life, now : {}".format(self.name, self.life))
22+
else :
23+
print("{} has lost most of his mana, sorry".format(self.name))
24+
25+
def __repr__(self):
26+
return "{} : life = {}, mana = {}".format(self.name, self.life, self.mana)

characters/wolf.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .characters import Character
2+
3+
class Wolf(Character):
4+
""" Wolf character class """
5+
def __init__(self, name = False):
6+
super().__init__(300, 30, 15, 40, name)

characters/zombie.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .characters import Character
2+
3+
class Zombie(Character):
4+
""" Zombie character class """
5+
def __init__(self, name = False):
6+
super().__init__(600, 15, 15, 5, name)
7+

game/__pycache__/arena.cpython-36.pyc

1.5 KB
Binary file not shown.
977 Bytes
Binary file not shown.
1.95 KB
Binary file not shown.
822 Bytes
Binary file not shown.

game/arena.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from .story_agent import storyAgent
2+
3+
class Arena(storyAgent):
4+
"""Battlefield class nursing the fight
5+
attributs : player, enemy
6+
methods: fighters_enter, fighters_leave, battle
7+
"""
8+
9+
def __init__(self):
10+
# character object attributes
11+
self.player = False
12+
self.enemy = False
13+
14+
def fighters_enter(self, player, enemy):
15+
"""Store characters objects attributs"""
16+
self.player = player
17+
self.enemy = enemy
18+
19+
def fighters_leave(self):
20+
"""Cear character object attributs"""
21+
self.player = False
22+
self.enemy = False
23+
24+
def battle(self):
25+
"""This function processes fight player's decision"""
26+
self.pause(2)
27+
# As long as both characters are alive
28+
while self.player.life > 0 and self.enemy.life > 0:
29+
try:
30+
action_allowed = False
31+
# check if the character able to start action
32+
while not action_allowed:
33+
print("{} || {}".format(self.player, self.enemy))
34+
action = input('Wot have you decided {} ? : '.format(self.player.actions)).lower()
35+
action_allowed = self.player.decision(action, self.enemy)
36+
# Show character successfuly dodged
37+
except Exception as e:
38+
break
39+
# Enemie's turn, if alive
40+
if self.enemy.life > 0:
41+
self.enemy.fight(self.player)
42+
self.pause(4)
43+
# End the fight
44+
print('End of fight')
45+
self.fighters_leave()

game/factory.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Factory():
2+
"""Character parser class"""
3+
4+
@classmethod
5+
def get_character(self, class_name):
6+
"""This methode returns a character object from its class name"""
7+
if class_name == "warrior":
8+
from characters.warrior import Warrior
9+
return Warrior()
10+
elif class_name == "archer":
11+
from characters.archer import Archer
12+
return Archer()
13+
elif class_name == "wizzard":
14+
from characters.wizzard import Wizzard
15+
return Wizzard()
16+
elif class_name == "orc":
17+
from characters.orc import Orc
18+
return Orc()
19+
elif class_name == "wolf":
20+
from characters.wolf import Wolf
21+
return Wolf()
22+
elif class_name == "zombie":
23+
from characters.zombie import Zombie
24+
return Zombie()

0 commit comments

Comments
 (0)