May i just say well don on the new code. It is a lot better then the old code and it is great you learnt from your old code and this new stuff is a lot better.
Well Done!

Thanks, thom, it works now and i'm slaying orcs like a pro. but is there a way to get random.randrange() to only have a maximum number of repeats? when i try to attack a monster, sometimes it says 'You did no damage.....' tooo much and i would like to limit the number of repeats that it does, if possible ,any help would be apreciated.

Maybe instead of randrange you could have a list of possible hit vaules. This way you could have zero in there only a few time and higher numbers in more.

Im glad to hear that it is working.

Oh also maybe another way to do it would to have a variable that is set at the max number of hits with no value and then every time there is a hit of zero the counter goes down, and then if the counter is at zero and the randrange gets zero then you could make it re-do it.

I was trying to run it again thismorning and then it started saying invalid syntax on the word 'class' but i cant seem to find anything wrong with it? hrers the code:

#!/usr/bin/env python

import random

class monster(object):
    def __init__(self, attack, health, defense, name):
        self.attack = attack
        self.health = health
        self.defense = defense
        self.name = name

    def GetAttributesInfo(self):
        attributes = ['Name: %s' % self.name,
        'Health: %d' % self.health,
        'Attack: %d' % self.attack,
        'Defense: %d' % self.defense]
        return ', '.join(attributes)

class spider(monster):
    def __init__(self):
        monster.__init__(self, 1, 3, 1, 'Spider')

    def GetAttributesInfo(self):
        return monster.GetAttributesInfo(self)
    
class goblin(monster):
    def __init__(self):
        monster.__init__(self, 2, 5, 2, 'Goblin')

    def GetAttributesInfo(self):
        return monster.GetAttributesInfo(self)

class orc(monster):
    def __init__(self):
        monster.__init__(self, 3, 10, 3, 'Orc')

    def GetAttributesInfo(self):
        return monster.GetAttributesInfo(self)

class troll(monster):
    def __init__(self):
        monster.__init__(self, 5, 20, 5, 'Troll')

    def GetAttributesInfo(self):
        return monster.GetAttributesInfo(self)

class dragon(monster):
    def __init__(self):
        monster.__init__(self, 10, 50, 6, 'Dragon')

    def GetAttributesInfo(self):
        return monster.GetAttributesInfo(self)

class demon(monster):
    def __init__(self):
        monster.__init__(self, 

class weapon(object):
    def __init__(self, md, name):
        self.max_damage = md
        self.name = name
    def GetDamage(self):
        return random.randrange(1, self.max_damage)
        
    def GetAttributesInfo(self):
        attributes2 = ['Max Damage: %d' %  self.max_damage,
        'Name: %s' % self.name]
        return ', '.join(attributes2)

class sword(weapon):
    def __init__(self):
        weapon.__init__(self, 5, 'Sword')

    def GetAttributesInfo(self):
        return weapon.GetAttributesInfo(self)

class axe(weapon):
    def __init__(self):
        weapon.__init__(self, 7, 'Axe')

    def GetAttributesInfo(self):
        return weapon.GetAttributesInfo(self)

class spear(weapon):
    def __init__(self):
        weapon.__init__(self, 10, 'Spear')

    def GetAttributesInfo(self):
        return weapon.GetAttributesInfo(self)

class player():
    def __init__(self):
        self.alive = True
        self.health = 30
        self.damage = 5
        choice = random.randrange(1,2)
        if choice == 1:
            self.weapon = sword()
            print 'The weapon that you are using\'s attributes are:'
            print self.weapon.GetAttributesInfo()
        else:
            self.weapon = axe()
            print 'The weapon that you are using\'s attributes are:'
            print self.weapon.GetAttributesInfo()
    
    def attack(self, monster):
        dam = self.weapon.GetDamage()
        if dam > monster.defense:
            monster.health = monster.health - dam
            print 'You hit him for %d damage!' %dam
        else:
            print 'You did\'nt do any damage...'

def main():
    new_monster = random.randrange(1, 5)
    if new_monster == 1:
        new_monster = orc()
    elif new_monster == 2:
        new_monster = troll()
    elif new_monster == 3:
        new_monster = dragon()
    elif new_monster == 4:
        new_monster = goblin()
    elif new_monster == 5:
        new_monster = spider()
    P = player()
    print 'The attacking monster\'s attributes are:'
    print new_monster.GetAttributesInfo()
    while True:
        P.attack(new_monster)
        if new_monster.health <= 0:
            print 'You killed the %n!' %new_monster.name
            loop = 0
        elif new_monster.health > 0:
            choice = raw_input('Do you want to attack again:')
            if choice == 'yes' or 'y':
                print 'The monster\'s health is now:', new_monster.health
            
if __name__ == '__main__':
    main()

Ah thats an easy one. Look at the class demon. Okay, notice in the __init__ method you call monster.__init__? Well remember to close those brackets.

That should fix it.

Lol, i remember that last night i was working on this, and i was going to my friends birthday party and i must have just saved it and left, thanks.

Another problem, i seperated the code into 3 files, 1 for the main, one for the monsters and another 1 for the weapons, and now it is telling me that my monsters arnt defined, and i have imported them properly, and everything, mabey i have to declare them as modules or sokmsething like that? any ideas?

How are you doing it?
I mean have you changed your code now so it goes:

module.monster
#rather then before
monster

Because unless you go

from module import *

You have to tell the program what module it is in. The second method imports all that is inside the module so you wouldn't have to change a thing.

Thanks for all your help with this! it works now, killin things, fixed to random problem and everything, i will be making a fully fledged game soon thanks!. i'v attached a pic of it working properly.

Congradulations, you have done very well.
:)

Sorry if you cant see it verry well

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.