[Tutor] Design Question... from a newbie

nik my.mailing.lists at noos.fr
Wed Oct 20 21:08:11 CEST 2004


Another person on the list is asking about good examples for using 
object orientated programming. This could be a good chance to try 
something like that.

A base class could be person (with basic attributes like 'first name'), 
and then you create derived classes being mage, warrior etc each with 
attributes appropriate to that type of character. Another base class 
could be item, with derived classes weapon, armor, food etc, and then 
derived from that further specifics (ie sword, staff  from the weapon 
class). This makes it easier when it comes to expanding into new classes 
(ie if you wanted to add another character type, ie dwarf, you wouldn't 
have to re-do all the basic attributes that apply to all characters, 
like health points etc, just the stuff that is unique to a dwarf). Each 
level in the class heirachy will introduce it's own relevent attributes. 
Then, when someone decides to be a dwarf, a lot of the attributes will 
be automatically set just because they're using the dwarf class.

That doesn't answer your question though, but it might help look at 
things a bit differently.

nik

Rene Lopez wrote:

>For the last couple weeks I have been teaching myself python and
>trying to get a better grasp in programming in general. I'm beginning
>to realize that there is more to it then learning a programming
>language.  I may know how a lot of English words, and how to write a
>sentence but I won't be able to write a novel right a way... the same
>goes with programming I suppose.
>
>I'm trying to write a small RPG game similar to D&D or whatever.  So
>I'm trying to figure out how to best keep track of a character and all
>of the attributes/variables for that character.  I think the key to
>this is going to be using a class to create a customer data type, but
>I'm not sure if there is a better way.  For example I'm using
>something like this:
>
>class Character:
>    def __init__(self, name=' ', job = 1, race = 1):
>        self.name = name
>        # assign job
>        if job == 1:
>             self.job = job
>             self.jobdesc = "Fighter"
>        elif job == 2:
>             self.job = class 
>             self.job = "Mage"
>        # assign race
>        if race == 1:
>             self.race = race
>             self.racedesc =  "Human"
>        if race == 2:
>             self.race = race 
>             self.racedesc = "Elf"
>
>player = Character(name= 'Bob', job=2, race=2)
>
>Would it make more sense to have the class just handle the attributes
>and do all of the assignment via a separate function?  The example I
>gave is very simple.. I imagine as the character information gets more
>in depth the list of attributes will grow and grow...so I'm concerned
>that it might start to get confusing.  What do you gurus think?
>
>PS: I apologize for any programming errors I made...as made the code
>up as i went.. and didn't test it... but i figured you'd get the idea.
>
>  
>



More information about the Tutor mailing list