-1

While coding my game, I've ran into problems when running it. For example, the zombie attack doesn't come out correctly, when using the store the gold value and health value do not update. When running the battle part, the first time the zombie has less health, then more health, and more damage. I don't know what I've messed up on. Any help/tips?

import time

import sys

import random

cls = ("\n"*100)


class Mage:
    def __init__(self):
        self.maxhp = 50
        self.attack = 3.33
        self.name = "Mage"


class Warrior:
    def __init__(self):
        self.maxhp = 70
        self.attack = 2.5
        self.name = "Warrior"


class Thief:
    def __init__(self):
        self.maxhp = 35
        self.attack = 5
        self.name = "Thief"


class Zombie:
    def __init__(self):
        self.maxhp = 10
        self.attack = 1
        self.name = "Zombie"


def heal(character_health):

    if character_health < character_health:
        character_health += 5
        print("Healed. Health is now " + character_health + " +5.")
        time.sleep(2)
    else:
        print("No healing available.")
        time.sleep(2)


def battle(character_health, character_attack, monster_health, monster_attack, gold):
    while True:

        character_health_max = character_health

        monster_name = "Zombie"

        choice1 = input("\nPress 1 to Attack: ")

        if choice1 == "1":
            monster_health -= character_attack
            print("\n" + str(monster_name) + "'s health is now " + str(monster_health))
            time.sleep(1)
            character_health -= monster_attack
            print("\nThe hero's health is now " + str(character_health))
            time.sleep(1)

        if character_health <= 0:
            print("\nThe hero is dead.")
            sys.exit("\nThe End")

        if monster_health <= 0:
            print("\nThe monster is dead.")
            time.sleep(2)
            print("Your gold has increased by: 5")
            gold += 5
            monster_health = 10
            character_health = character_health_max
            time.sleep(2)
            menu_list(character_health, character_attack, monster_health, monster_attack, gold)


def store(gold, character_health):

        print("\nWelcome to my shop of wonders! My name is Hanz, what can I aid you with today? We have...\nPotions: [1.] EEK")
        buy = input("\nWhat will it be? ")

        if gold < 5:
            print("Sorry, you don't have any gold!")
            time.sleep(2)

        if buy == "1" and gold >= 5:
            print("\nYou now own the Potion EEK! Health increased by 5!")
            character_health += 5
            gold -= 5
            time.sleep(2)


def menu_list(character_health, character_attack, monster_health, monster_attack, gold):

    while True:
        print(cls)

        menu = input("---> Fight [1.] \n---> Heal [2.] \n---> Store [3.] \n---> Quit [4.] \n---> Gold: " + str(gold) + " \n---> ")

        if menu == "4":
            sys.exit()

        if menu == "2":
            heal(character_health)

        if menu == "1":
            battle(character_health, character_attack, monster_attack, monster_health, gold)

        if menu == "3":
            store(gold, character_attack)

        if menu == "Gold":
            print("\nNot valid hackerman.")
            time.sleep(1)


class Main:

    print(cls)

    name = input("What is your name: ")

    character = input("\nChoose your class: \n----------------- \nMage [1.] \nWarrior [2.] \nThief [3.] \n---> ")

    if character == "1":
        character_health = Mage().maxhp
        print("\nHealth " + str(character_health))
        character_attack = Mage().attack
        print("\nAttack " + str(character_attack))
        character_name = Mage().name
        print("\nClass " + str(character_name))
        time.sleep(3)
        monster_health = Zombie().maxhp
        monster_attack = Zombie().attack
        gold = 0
        menu_list(character_health, character_attack, monster_health, monster_attack, gold)

    if character == "2":
        character_health = Warrior().maxhp
        print("\nHealth " + str(character_health))
        character_attack = Warrior().attack
        print("\nAttack " + str(character_attack))
        character_name = Warrior().name
        print("\nClass " + str(character_name))
        time.sleep(3)
        monster_health = Zombie().maxhp
        monster_attack = Zombie().attack
        gold = 0
        menu_list(character_health, character_attack, monster_health, monster_attack, gold)

    if character == "3":
        character_health = Thief().maxhp
        print("\nHealth " + str(character_health))
        character_attack = Thief().attack
        print("\nAttack " + str(character_attack))
        character_name = Thief().name
        print("\nClass " + str(character_name))
        time.sleep(3)
        monster_health = Zombie().maxhp
        monster_attack = Zombie().attack
        gold = 0
        menu_list(character_health, character_attack, monster_health, monster_attack, gold)


if __name__ == '__main__':
    Main()
rapidDev
  • 109
  • 2
  • 13
  • I think your problem is that you don't have one object representing your player. You should make a `Player` superclass that has the instance variables `maxhp`, `attack`, and `name`, and the methods `take_damage`, `heal`, and `attack`. This way you can have each subclass inherit these methods. Currently, when you're setting `character_health = Warrior().maxhp`, you're creating a `Warrior` object, getting its health, but then immediately discarding the object. You should store it somehow. – Aviv Shai Sep 26 '18 at 01:50
  • [Here](https://pastebin.com/QwhVJxhR)'s a Pastebin showing what I mean with the classes. – Aviv Shai Sep 26 '18 at 02:25

1 Answers1

0

I think this is a good time to go over what happens when you pass variables to functions in Python.

Firstly, everything in Python is an object! Primitives too (numbers are wrapped as an object in Python). Every class of objects inherit from the object class in Python.

Now, what happens when you pass a primitive to a function and change the value?

To illustrate:

def foo(a):
    a = 5
b = 1
foo(b)
print(b) # b is still 1! Some objects in Python are immutable including integers, strings, and booleans.

Now I suspect your gold and health values aren't changing because you're passing in immutable objects which cannot be changed!

How to resolve? You want to pass in a mutable object! Instead of passing an integer object (which is immutable) for a character's health, pass in the character object (which is mutable). You can set the new health of that character object.

To illustrate:

class Warrior:
def __init__(self):
    self.maxhp = 70
    self.attack = 2.5
    self.name = "Warrior"
    self.currenthp = 55 # arbitrary number but you may want to have something like this

def heal(warrior):
    warrior.currenthp += 5

# somewhere in your Main function
warrior1 = Warrior()
heal(warrior1) # currenthp of this warrior should be 60!

It's important that you implement OOP correctly when you are creating your game. Similarly try debugging your other issues paying attention to how you implement OOP.

rapidDev
  • 109
  • 2
  • 13
  • Ok, thank you so much! This cleared a lot up, and your explanation was great too. Thanks again! – Kal Set Sep 27 '18 at 22:12