Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a code that I've been messing around with and the monster attack (monster.ma) isn't randomising after the first time it's called upon (elif statement). I've tried to make it a tuple since they are immutable but it doesn't randomise after the first time. I was told to make a class and that's what I have done but it still doesnt work right. My attack variable (att) works right, but monsters' attack gets stuck. I know I can code the monster's attack into the elif statement, but I want to be able to just call upon it when needed.

import random

class monster():
    hp=50
    ma=random.randint(5, 20)
    xp=random.randint(15, 30)
a=1
exp=0
while a==1:
    if monster.hp<=0:
        exp+=monster.xp
        print"you have killed the monster"
        print("you get, "+str(monster.xp)+" exp from monster"
              "you now have, "+str(exp)+" exp")
        break
    elif monster.hp>=0:
        print"swing your sword"
        att=random.randint(5, 15)
        monster.hp-=att
        print"you do, "+str(att)+" damage to monster"
        print"monster does, "+str(monster.ma)+" damage to you" #<----------------here
share|improve this question

closed as off-topic by Anko, bummzack, Sean Middleditch, Byte56 Sep 18 '13 at 23:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Anko, bummzack, Sean Middleditch, Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

It's only randomized the first time because you're only calling the random function one time (when you create the monster object). When you have ma=random.randmint(5, 20) in your class then you are creating that variable with a random amount at that time, but you are not altering it. In order to get a random value every time you need to reset the value every time you perform an attack. Notice how you do this when you set att; you need to do it the same way with monster.ma.

If you want to be able to generate a random attack, you need to create a method to do it. Something like this in your monster class:

def get_attack():
    return random.randomint(5, 20)

Should work whenever you call monster.get_attack(). Note however that it's generally a bad idea to use hard coded "magic numbers" like this. It would be a good idea to define a monsters attack stats as members of the monster class. It seems like you're making monster a class because you read somewhere that it should be a class and not because you're going for an object oriented structure. Read up on object oriented programming

share|improve this answer
    
Why not also encourage him to use minDmg and maxDmg constants instead of hardcoded 5's and 20's –  bobobobo Sep 17 '13 at 2:11
1  
Actually I did consider that but opted for the simplest answer since outlining how to make a properly constructed monster class seemed out of the scope of answering why random damage doesn't work right. Perhaps some suggestions would be good anyway, though. –  ssb Sep 17 '13 at 2:32

I'm very rusty with python, but usually what you want to do is create new monsters.

For example,

class monster(): def init(self): self.ma = random.randint(5,20)

or something. That way, you can create new monsters with m = monster() and then use their attack. I'm not sure this is perfectly correct, but it should help.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.