patternpythonMinor
Text-based turn-based fighter game
Viewed 0 times
turntextgamebasedfighter
Problem
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 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)
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 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)
Solution
-
General
-
PEP8 calls for
-
Returning values is much more explicit than state mutations. I strongly recommend passing players'
For the rest of review I'll use original style.
-
Move selection
belongs to the player, not the main loop. For example,
and similar for
-
and
General
-
PEP8 calls for
snake_case for methods, and reserves Capitalized names for classes. Using Player1 for variable is not right.-
Returning values is much more explicit than state mutations. I strongly recommend passing players'
Turns to play as variables rather then Player's properties.For the rest of review I'll use original style.
-
Move selection
belongs to the player, not the main loop. For example,
class Human(Characters.Fighter):
....
def chose(self):
follow_the_logic_here()
self.move = self.Options.__dict__[playerchoice]
self.Turn = self.move.priorityand similar for
Marco (who is really Robot). The main loop simplifies towhile (....):
situation_update()
Player.chose()
Marco.chose()
play(Player, Marco)-
play looks a bit convoluted, especially the recursive call. I recommend to factor out the rules of move order into a separate function, as indef play(Player1, Player2):
p1, p2 = order_of_move(Player1, Player2)
p1.move(p1, p2)
if p2.hp > 0:
p2.move(p2, p1)and
def order_of_move(p1, p2):
if p1.Turn p2.Turn:
return (p2, p1)
# Here p1.Turn == p2.Turn for sure
if p1.speed > p2.speed:
return (p1, p2)
if p1.speed < p2.speed:
return (p2, p1)
# Here we must randomize
....Code Snippets
class Human(Characters.Fighter):
....
def chose(self):
follow_the_logic_here()
self.move = self.Options.__dict__[playerchoice]
self.Turn = self.move.prioritywhile (....):
situation_update()
Player.chose()
Marco.chose()
play(Player, Marco)def play(Player1, Player2):
p1, p2 = order_of_move(Player1, Player2)
p1.move(p1, p2)
if p2.hp > 0:
p2.move(p2, p1)def order_of_move(p1, p2):
if p1.Turn < p2.turn:
return (p1, p2)
if p1.Turn > p2.Turn:
return (p2, p1)
# Here p1.Turn == p2.Turn for sure
if p1.speed > p2.speed:
return (p1, p2)
if p1.speed < p2.speed:
return (p2, p1)
# Here we must randomize
....Context
StackExchange Code Review Q#119562, answer score: 3
Revisions (0)
No revisions yet.