[Tutor] Flat is better than Nested
Steven D'Aprano
steve at pearwood.info
Fri Sep 9 06:28:46 CEST 2011
Ryan Strunk wrote:
> Hello everyone,
> I am still learning to program by writing this boxing game. I'm running into
> a problem with how to organize data. My current setup looks something like
> this:
> """This feels incredibly disgusting to me."""
You might find it a little less disgusting with the application of some
helper classes and a slightly different data format.
Combos probably should be treated as a sequence of moves. For
simplicity, I'm going to assume each move takes the same time, and once
a combo is entered, the opponent can't interrupt it. You may want to
make it more complicated/realistic.
The hard part is deciding what fields you need. Here's my guess:
class Move:
def __init__(self, speed, strength, side, purpose, kind, foul=False):
self.speed = speed
self.strength = strength
self.side = side
self.purpose = purpose
self.kind = kind
self.foul = foul
CENTRE = NEUTRAL = ''
LEFT, RIGHT = 'left right'.split()
ATTACK, DEFENSE = 'attack defense'.split()
right_cross = Move(2, 3, RIGHT, ATTACK, 'punch')
left_lead = Move(3, 4, LEFT, ATTACK, 'punch')
left_jab = Move(1, 1, LEFT, ATTACK, 'punch')
headbutt = Move(1, 3, CENTRE, ATTACK, 'headbutt', True)
step_in = Move(2, 0, NEUTRAL, NEUTRAL, 'footwork')
weave = Move(1, 0, NEUTRAL, DEFENSE, 'footwork')
rabbit_punch = Move(1, 2, CENTRE, ATTACK, 'punch', True)
class Combo:
def __init__(self, *moves):
self.moves = moves
old_one_two = Combo(left_lead, right_cross)
liverpool_kiss = Combo(step_in, headbutt)
combos = {
1: old_one_two,
2: liverpool_kiss,
3: Combo(weave, weave, step_in, rabbit_punch),
4: Combo(left_jab, left_jab, left_jab, step_in, uppercut),
}
Does that help?
--
Steven
More information about the Tutor
mailing list