patternpythonMinor
Python text-based fighting game
Viewed 0 times
fightingtextgamepythonbased
Problem
I wrote a fighting game. My code is rough. I would appreciate constructive and critical feedback please. The goal is to become a better programmer/developer.
(I wrote this using 2 Files - The Main Game File & Then a Character File):
pyfight_characters.py
```
import math
import random
class Berserker(object):
MAX_hp = 250
base_hp = 250
base_speed = 75
priority = 'Normal'
mindset_multiplier = 1.00
move = ''
def Jab(self,opponent):
""" Jab - A fast, light attack. Does 30 Basic Damage to Opponent"""
self.priority = 'Fast'
opponent.base_hp -= math.floor( 30*self.mindset_multiplier )
def Haymaker(self,opponent):
""" Haymaker - A Slow, strong attack. Does 65 Base to Opponent and 30 Base Damage to User"""
self.priority = 'Slow'
opponent.base_hp -= math.floor (65*self.mindset_multiplier)
self.base_hp -= math.floor(30*self.mindset_multiplier)
def PumpUp(self,opponent):
""" PumpUp - Normal speed. Improves the Mindset of the User ( x 1.25 )"""
self.priority = 'Normal'
self.mindset_multiplier *= 1.25
move_pool = {'1':Jab,'2':Haymaker,'3':PumpUp}
class Yogi(object):
MAX_hp = 220
base_hp = 220
base_speed = 80
priority = 'Normal'
mindset_multiplier = 1.00
move = ''
def Sukha(self,opponent):
""" Sukha - A Normal speed move. Restores up to a Base of 30 HP to User"""
self.priority = 'Normal'
if self.base_hp opponent.MAX_hp:
opponent.base_hp = opponent.MAX_hp
if self.base_hp self.MAX_hp:
self.base_hp = self.MAX_hp
def Tease(self,opponent):
""" Tease - A Normal-speed move. Reduces the Mindset of the Opponent (x 0.8)"""
self.priority = 'Normal'
opponent.mindset_multiplier *= 0.9
self.minds
(I wrote this using 2 Files - The Main Game File & Then a Character File):
pyfight_characters.py
```
import math
import random
class Berserker(object):
MAX_hp = 250
base_hp = 250
base_speed = 75
priority = 'Normal'
mindset_multiplier = 1.00
move = ''
def Jab(self,opponent):
""" Jab - A fast, light attack. Does 30 Basic Damage to Opponent"""
self.priority = 'Fast'
opponent.base_hp -= math.floor( 30*self.mindset_multiplier )
def Haymaker(self,opponent):
""" Haymaker - A Slow, strong attack. Does 65 Base to Opponent and 30 Base Damage to User"""
self.priority = 'Slow'
opponent.base_hp -= math.floor (65*self.mindset_multiplier)
self.base_hp -= math.floor(30*self.mindset_multiplier)
def PumpUp(self,opponent):
""" PumpUp - Normal speed. Improves the Mindset of the User ( x 1.25 )"""
self.priority = 'Normal'
self.mindset_multiplier *= 1.25
move_pool = {'1':Jab,'2':Haymaker,'3':PumpUp}
class Yogi(object):
MAX_hp = 220
base_hp = 220
base_speed = 80
priority = 'Normal'
mindset_multiplier = 1.00
move = ''
def Sukha(self,opponent):
""" Sukha - A Normal speed move. Restores up to a Base of 30 HP to User"""
self.priority = 'Normal'
if self.base_hp opponent.MAX_hp:
opponent.base_hp = opponent.MAX_hp
if self.base_hp self.MAX_hp:
self.base_hp = self.MAX_hp
def Tease(self,opponent):
""" Tease - A Normal-speed move. Reduces the Mindset of the Opponent (x 0.8)"""
self.priority = 'Normal'
opponent.mindset_multiplier *= 0.9
self.minds
Solution
I'm not sure what Sukha is supposed to do, but I'm finding it odd. I think what you meant to do was raise the HP, then reset it if it's higher than the max?
It looks like whenever health changes, it's a multiple of the
It looks like there are several fields getting initialized to the same thing. I would have a base
I had trouble figuring out why each character had an empty
I don't find the "Preallocation" comments useful.
You might have a bug in the character variables. Base HP etc don't have self, so that makes them static variables. I think odd things will happen if you have, for example, two Tricksters fight each other.
You can cut down on code re-use in
It looks like whenever health changes, it's a multiple of the
mindset_multiplier. I'd suggest having that in a new function such as:def adjust_health(self, amount):
self.base_hp += math.floor(amount * self.mindset_multiplier)It looks like there are several fields getting initialized to the same thing. I would have a base
Character class that the other three inherit from. The base class would set default stats and define the adjust_health method.I had trouble figuring out why each character had an empty
move variable. I'd recommend a comment. Actually, there's probably a better design for that, but I can't think of it atm.I don't find the "Preallocation" comments useful.
You might have a bug in the character variables. Base HP etc don't have self, so that makes them static variables. I think odd things will happen if you have, for example, two Tricksters fight each other.
You can cut down on code re-use in
move_collection() by having a choose_move function that can tell whether the character is human or computer and doing the right thing from there.Code Snippets
def adjust_health(self, amount):
self.base_hp += math.floor(amount * self.mindset_multiplier)Context
StackExchange Code Review Q#158675, answer score: 3
Revisions (0)
No revisions yet.