To get a better understanding of objects/methods, I wrote a text-based, turn-based fighting game.
I want to expand by adding new characters with new moves (one of which can alter the way other moves works - I'm thinking of using Decorators for this).
Anyways, I'm open to suggestions, advice, constructive criticism, etc.
import random
class Characters:
class Fighter:
def __init__(self,hp=100,atk=75,defen=50,speed=60):
self.hp = hp
self.atk = atk
self.defen = defen
self.speed = speed
class Options:
def punch(self,target,modifier=1.00):
base = 50/100.0
damage = (self.atk - target.defen)*base
target.hp = target.hp - damage
print(self.Name + " punched " + target.Name + " for %d HP" % damage)
punch.priority = 1
def heal(self,target,modifier=1.00):
self.hp += 15
if self.hp > 100:
self.hp = 100
print(self.Name + " healed 15 HP")
heal.priority = 2
def kick(self,target,modifier=1.00):
base = 100/100.0
damage = (self.atk-target.defen)*base
target.hp = target.hp - damage
self.hp = self.hp - 10
print(self.Name + " kicked " + target.Name + " for %d HP" % damage)
kick.priority = 2
# Declare Marco & Player
Player = Characters.Fighter()
Player.Name = "You"
Marco = Characters.Fighter()
Marco.Name = "Marco"
# Define Turn Action
def play(Player1,Player2):
# First - Check Options Priority
if Player1.Turn < Player2.Turn:
Player1.move(Player1,Player2)
if Player2.hp > 0:
Player2.move(Player2,Player1)
else:
Player2.hp = 0
return Player2.hp
elif Player1.Turn > Player2.Turn:
Player2.move(Player2,Player1)
if Player1.hp > 0:
Player1.move(Player1,Player2)
else:
Player1.hp = 0
return Player1.hp
# Second - Check Player's Speed
else:
if Player1.speed > Player2.speed:
Player1.move(Player1,Player2)
if Player2.hp > 0:
Player2.move(Player2,Player1)
else:
Player2.hp = 0
return Player2.hp
elif Player1.speed < Player2.speed:
Player1.move(Player1,Player2)
if Player2.hp > 0:
Player2.move(Player2,Player1)
else:
Player2.hp = 0
return Player2.hp
# Third - Ranomdly Choose
else:
random.choice(range(2))
if random.choice == 0:
Player1.Turn = Player2.Turn+1
play(Player1,Player2)
else:
Player1.Turn = Player2.Turn-1
play(Player1,Player2)
### Fight Sequence ###
# Start
print("\n 3...2...1...")
print("FIGHT!")
# Begin Fight/Conditions for Ending
while Player.hp > 0 and Marco.hp > 0:
# Situational Update
print("\n \t You have %d health points." %Player.hp)
print("\n \t Marco has %d health points." %Marco.hp)
print("\n \t It is your move!!!")
# Choosing the Options - Correctly
# Place Holder let's the whole loop start/error check.
playerchoice = 'place holder'
while playerchoice not in [1,2,3]:
playerchoice = input("\n Which move would you like to use: \
\n 1) Punch -- Base Damage of 50 (Weak) \
\n 2) Kick -- Base Damage of 100 (Moderate), but lose 10 HP\
\n 3) Heal -- Heal for 15 HP. \
\n -->")
# Convert Player Choice to String for Dict-Search
if playerchoice == 1:
playerchoice = "punch"
elif playerchoice == 2:
playerchoice = "kick"
else:
playerchoice = "heal"
# Randomly Choose Marco's move from list of Options Options
# Marco shouldn't be frequently healing at higher HP (Hence, if/else)
if Marco.hp < random.choice( range(80,100) ):
marcochoice = random.choice(Marco.Options.__dict__.keys()[1:4])
else:
marcochoice = random.choice(Marco.Options.__dict__.keys()[2:4])
Marco.move = Marco.Options.__dict__[marcochoice]
Player.move = Player.Options.__dict__[playerchoice]
print(Marco.move)
print (Player.move)
### Determine who goes first
Player.Turn = Player.move.priority
Marco.Turn = Marco.move.priority
print Marco.Turn
print Player.Turn
# First - Check Options Priority:
play(Player,Marco)