i am not going to give you a bunch of code and say don't ask what it does, trust me, it works I can give an example showcasing stuff, but it's up to you to absorb it and see what it can be used for.
Is there a reason you got separate class for each dragon? Imo you should have only 1 class, surprisingly enough called Dragon , as it should be enough to describe all the common things your dragons share, and use it for all your majestic creatures.
If you need to specialize your dragons further you can use inheritance to add functionality, eg
Code:
class BlackDragon( Dragon ):
<some black dragon stuff>
In case you want to have other types of creatures too you may want to create an abstract Creature class (eg having hp,ba,sp) and derive the Dragon class(es) from it
Another issue i see is that at least some of your printed descriptions are not tied to the object they are related to (even though there seems to be a code that prints dragon info) and maintainability will suffer. Should any values change due to balance reasons or whatever, you would have to track and update manually in multiple places to maintain coherence. Good design would be based on a single set of master data and everything would query it when needed, so you never have to deal with contradictory duplicates.
Quick and dirty example
Code:
#!/usr/bin/env python
def walk( obj ):
print '- {0} walks'.format( obj.name )
def fly( obj ):
print '- {0} flies'.format( obj.name )
def breathe_fire( obj ):
print '- {0} breathes fire'.format( obj.name )
def resist_magic( obj ):
print '- {0} resists magic'.format( obj.name )
def stone_skin( obj ):
print '- {0} has stone skin'.format( obj.name )
def fit_in_pokeball( obj ):
print '- {0} fits in a poke ball'.format( obj.name )
def look_cute( obj ):
print '- {0} looks cute'.format( obj.name )
class Creature:
class_actions = [ walk ]
def __init__( self, _name, _hp, _ba, _sp, _actions, _desc ):
self.name = _name
self.hp = _hp
self.ba = _ba
self.sp = _sp
self.desc = _desc
self.actions = self.class_actions[:]
for a in _actions:
self.actions.append(a)
def info( self ):
print '{0}: {1}\nHP: {2}\nBA: {3}\nSP: {4}'.format( self.name, self.desc, self.hp, self.ba, self.sp )
for a in self.actions:
a( self )
class Pokemon( Creature ):
class_actions = [ walk, fit_in_pokeball ]
class Dragon( Creature ):
class_actions = [ walk, fly, resist_magic ]
beasts = { 'Pikachu': Pokemon( 'Pikachu', 30, 20, 20, [ look_cute ],
'A cute, little pokemon of yellow color' ),
'Coal': Dragon( 'Coal', 100, 25, 30, [ breathe_fire ],
'A fiery dragon, with medium health, and medium attack' ),
'Hydros': Dragon( 'Hydros', 100, 20, 40, [],
'A cool water dragon, with medium health, a lower attack, but a high special attack' ),
'Geon': Dragon( 'Geon', 200, 10, 15, [ stone_skin ],
'A hard, stony dragon, with a very high health, and a low special and regular attack' ) }
if __name__ == '__main__':
print 'Available beasts:'
for name in beasts:
beasts[name].info()
Code:
$ ./creatures.py
Available beasts:
Coal: A fiery dragon, with medium health, and medium attack
HP: 100
BA: 25
SP: 30
- Coal walks
- Coal flies
- Coal resists magic
- Coal breathes fire
Pikachu: A cute, little pokemon of yellow color
HP: 30
BA: 20
SP: 20
- Pikachu walks
- Pikachu fits in a poke ball
- Pikachu looks cute
Hydros: A cool water dragon, with medium health, a lower attack, but a high special attack
HP: 100
BA: 20
SP: 40
- Hydros walks
- Hydros flies
- Hydros resists magic
Geon: A hard, stony dragon, with a very high health, and a low special and regular attack
HP: 200
BA: 10
SP: 15
- Geon walks
- Geon flies
- Geon resists magic
- Geon has stone skin
there is None keyword in python, that is used to describe emptiness
Code:
$ python
Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x={ 1: None, None: 1 }
>>> for k in x:
... if k is None:
... print 'key is empty, value =', x[k]
... elif x[k] is None:
... print 'value is empty, key =', k
...
value is empty, key = 1
key is empty, value = 1
Bookmarks