Two questions about style and some simple math

Spoofy spoofy1 at gmx.net
Mon Jan 19 20:15:47 EST 2009


Hello everybody!

Though I'm a hobby programmer for years now (mainly small hackery 
things) I still have big problems getting "real" things to work.

I'm currently trying to write a simple RPG and have problems with 
the following:

1.

Characters have a "courage" attribute that basically determins who 
has the first attack in a fight. After some trying, I came up with 
this (sorry, not really working code, but what I made from 
interactive experimentation):

def first_attack(player1,  player2):
     diff = player1.attributes.courage - player2.attributes.courage
     players = (player,  player2)
     return players[diff + random.randint(-diff,  diff) < 0]

To make it more realistic, I randomized it a little bit and this 
seems to work for low courage values. But when the courage values 
are high (100 and such) it fails (the chance to have the first 
attack drops the higher the values are). My math is really bad and I 
have problems to understand what's happenning here. I suspect the 
greater range for randint() is the problem, but I don't really get why.

Any tips would be greatly appreciated.

2.

For maintaining the character attributes I creates a seperate class. 
I wonder weather this is an "overuse" of OO (instead of just making 
the attributes plain variables of the Char class) and if the way I 
wrote this is OK (somehow this looks cool to me but maybe too "showy"?)

class Attributes(object):
     ATTRIBUTES = {"attack": 0, "defence": 0, "ability": 0, 
"courage": 0, "condition": 0}
     def __init__(self, **kwargs):
         self.__dict__.update(self.ATTRIBUTES)
         for arg in kwargs:
             if arg not in self.ATTRIBUTES:
                 raise ValueError("Unkown character attribute '%s'" 
% arg)
             self.__dict__[arg] = kwargs[arg]


Again, I appreciate any tips. I you need more code (for the bigger 
picture or such), just ask.

Thanks in advance



More information about the Python-list mailing list