mardi 26 septembre 2017

Python: Trying to make random values during program run or indexable class

using Python 3.6.2

In this code I am making an Enemy class and calling a function that displays random values for it's variable Name, LVL, and health. This code works, but the random module will only work once when the program initializes. After that first time, every subsequent function call has the same values as the first.

How would I make it random every time while the program runs?

I will be calling this class from another code by importing it. It does not currently change every time I call it from the other code.

-

If I can't make this happen, how can I make my class indexable so I make it pseudo random by making new variables?

I've read that __ getitem __ and __ setitem __ would help me index but i don't know how to implement it as I really don't know much about it or what it does. I've tried reading about it but I don't seem to grasp the concept.

Here is my code:

import random as rnd

class enemy():

    def __init__(self, name, level, health):

        self.name = name
        self.level = level
        self.health = 20+(5*self.level)

Forest = ['Wolf', 'Bear', 'Bandit', 'Hunter']

Plains = ['Bandit', 'Large Bird', 'Lion']

City = ['Thiefs', 'Rats', 'Guards']



EnemyStart = enemy(rnd.choice(Forest), rnd.randint(1,2), [])
EnemyLow = enemy(rnd.choice(Plains), rnd.randint(3,5), [])
EnemyCh1 = enemy(rnd.choice(City), rnd.randint(6,8), [])

Count=0

def ResetCond():
    Count+=1
    EnemyStart = enemy(rnd.choice(Forest), rnd.randint(1,2), [])
    EnemyLow = enemy(rnd.choice(Plains), rnd.randint(3,5), [])
    EnemyCh1 = enemy(rnd.choice(City), rnd.randint(6,8), [])
    return Count

##if using index I would have this instead in the 
##ResetCond----EnemyStart[Count]=----
##----print(EnemyStart[count].name)----
## repeat for all similar variables
##But this gives me error object enemy is not indexable.

def DisSE():
    print (EnemyStart.name)
    print ('LVL:', EnemyStart.level)
    print ('HP:', EnemyStart.health)

def DisLE():
    print (EnemyLow.name)
    print ('LVL:', EnemyLow.level)
    print ('HP:', EnemyLow.health)

def DisC1E():   
    print (EnemyCh1.name)
    print ('LVL:', EnemyCh1.level)
    print ('HP:', EnemyCh1.health)


DisSe()
DisLe()
DisC1E()

print()

DisSe()
DisLe()
DisC1E()

print()

DisSe()
DisLe()
DisC1E()




Aucun commentaire:

Enregistrer un commentaire