New to Python, looking a little guidance

This is the place for queries that don't fit in any of the other categories.

New to Python, looking a little guidance

Postby vaoe » Thu May 02, 2013 9:01 pm

Hi there all. I just started Python today and been having a lot of fun with it. Never learned any languages so I'm new to programming in general.

I decided to make text-rpg game to help make learning more fun for myself. Been going smoothly until I noticed I was repeating a lot of code and I'm thinking that when I'm repeating a lot of the same thing, there must be something I'm missing.

so, playing with classes (which are awesome) and set one up so my little game could have multiple enemies like so:

Code: Select all
class Monster:

    def __init__(self, name, hp, mp, ac, res, exp, gold):
        self.name = name #monster name
        self.hp = hp     #monster health
        self.mp = mp     #monster mana
        self.ac = ac     #monster armor class
        self.res = res   #monster resistance
        self.exp = exp   #experiance reward
        self.gold = gold #gold reward


Then made a few monsters using this class:

Code: Select all
goblin = Monster("Goblin", 50, 0, 5, 0, 75, 40)
goblinKing = Monster("Goblin King", 600, 50, 25, 25, 900, 1100)
rat = Monster("Rat", 25, 0, 0, 40, 8


Now I wanted to make a 'battle' scenario which could be started and in which one of the monsters could be loaded into the battle by loading their stats into variables to be used in calculating damage, health changes and so on.

Code: Select all
battle = 1
monsterID = "goblin"
if battle == 1:
    if monsterID == "goblin":
        monsterMaxHP = goblin.hp
        monsterCurHP = goblin.hp
        monsterName  = goblin.name
        monsterMaxMP = goblin.mp
        monsterCurMP = goblin.mp
        monsterAC    = goblin.ac
        monsterRes   = goblin.res
        monsterExp   = goblin.exp
        monsterGold  = goblin.gold

    elif monsterID == "goblinKing":
        monsterMaxHP = goblinKing.hp
        monsterCurHP = goblinKing.hp
        monsterName  = goblinKing.name
        monsterMaxMP = goblinKing.mp
        monsterCurMP = goblinKing.mp
        monsterAC    = goblinKing.ac
        monsterRes   = goblinKing.res
        monsterExp   = goblinKing.exp
        monsterGold  = goblinKing.gold

    elif monsterID == "rat":
        monsterMaxHP = rat.hp
        monsterCurHP = rat.hp
        monsterName  = rat.name
        monsterMaxMP = rat.mp
        monsterCurMP = rat.mp
        monsterAC    = rat.ac
        monsterRes   = rat.res
        monsterExp   = rat.exp
        monsterGold  = rat.gold


Can't see what I'm missing but I'm thinking I can somehow link the monsterID variable and the class variables to simplify this. I tried using
Code: Select all
monsterID.hp
but I guess you can't use a string like this.

Would appreciate a little help, maybe just a point in the right direction as apposed to a total solution, it's kinda fun to apply a principle, I just don't know which one I'm looking for.
vaoe
 
Posts: 2
Joined: Thu May 02, 2013 8:42 pm

Re: New to Python, looking a little guidance

Postby Yoriz » Thu May 02, 2013 9:45 pm

Pass in the monster to a func and then just call its methods/attributes.
Code: Select all
class Monster:

    def __init__(self, name, hp, mp, ac, res, exp, gold):
        self.name = name  # monster name
        self.hp = hp  # monster health
        self.mp = mp  # monster mana
        self.ac = ac  # monster armour class
        self.res = res  # monster resistance
        self.exp = exp  # experience reward
        self.gold = gold  # gold reward

goblin = Monster("Goblin", 50, 0, 5, 0, 75, 40)
goblinKing = Monster("Goblin King", 600, 50, 25, 25, 900, 1100)
rat = Monster("Rat", 25, 0, 0, 40, 8, 20)


def battle(battle_no, monster):
    print battle_no
    print monster.name
    print monster.hp

battle(1, goblin)
battle(2, goblinKing)
battle(3, rat)

Code: Select all
Goblin
50
2
Goblin King
600
3
Rat
25
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1049
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: New to Python, looking a little guidance

Postby vaoe » Thu May 02, 2013 9:47 pm

Ah thats so simple now that I see it. Thanks a lot for pointing that out.
vaoe
 
Posts: 2
Joined: Thu May 02, 2013 8:42 pm

Re: New to Python, looking a little guidance

Postby Mekire » Fri May 03, 2013 1:25 pm

I just want to say, it is nice to see a new programmer that actually wants to use classes, let alone sees the value of them. Most new coders seem to fight tooth and nail not to use them. A good start.

-Mek
User avatar
Mekire
 
Posts: 1025
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests