jeudi 26 mars 2020

Using random.choice() with classes

As a predisposition to this post, I'm very new to programming. I've been a sound engineer for the last 5+ years but due to lack of work in my area I've decided to go back to school for software development. Classes start in September but while I have downtime due to the state of the world right now (Coronavirus), I figured I'd use my time to my advantage and get a head start. I've decided to start with python because the syntax is pretty simple and I have dabbled in it years ago when I was a kid.

Anyway, I was following along with youtube tutorials and just copying peoples code and felt like i wasn't learning anything. So once I had a basic concept of enough fundamentals to get started I figured I'd just started coding something and learn by trial and error, and documentation reading. I started to program a simple text based RPG game.

The problem I have encountered is when using the random.choice() function along with a list of objects representing monsters, when a monster is chosen with a certain set of stats and level etc, it locks them in as that initial choice. for example.

I have a list of two monster objects like so :

monster = [Monster_Blob("Blob", 0, 0, 0, 0, 0), Monster_Skeleton("Skeleton", 0, 0, 0, 0, 0)]
pick = random.choice(monster)

Their stats inside the class such as Level are determined by random integers. Then along with that the player has a search function:

def monster_search():
    global pick
    pick = random.choice(monster)
    print()
    print("You found a " + pick.Name)
    print()
    print("Name: " + str(pick.Name), "Level: " + str(pick.Lvl))
    print("HP: " + str(pick.BaseHP), "MP: " + str(pick.BaseMP))
    print("Str: " + str(pick.BaseStr), "Dex: " + str(pick.BaseDex))
    battle()

When the monsters HP is reduced to 0 and the battle function ends, when you call the search function again it repeats the same 2 monsters with the same stats that were locked in during the initial search. Is there something else I should be doing other than just defining pick as a random choice of monster during the search function?

I know this is pretty basic stuff but its had me stuck long enough that I took the time to write this out.

Thanks for reading.




Aucun commentaire:

Enregistrer un commentaire