Help me learn


#1


I've just finished the python course not sure how to put together the things I've learned .
below is a little game I started.what should I study to learn how to make
this better?I want player classes an inventory .spells and weapons.
do I have to make a list for every item,weapon and spell? would a dictionary work?
how would I implement classes?

import random
def bat():
    health = char[1]W
    enemy = char[1]
    while health >= 1 or enemy >= 1:


        print "you have encountered an enemy"
        a = int(raw_input ("to attack enter 1"))
        if a == 1:
            enemy -= random.randint(1,11)
            health -= random.randint(1,5)
        print enemy
        print health

points = 120
char =[]
while points > 0:
        name = raw_input("What would you like to name you character?")
        name = char.append(name)
        print char[0]
        print "You have 120 points to spend on health,mana,streangth and stamina"
        health = int(raw_input("How many points would you like to add to health?"))
        points -= health
        health = char.insert(1,health)
        print "Your health is " + str(char[1])
        print points,
        print "points remaining"
        if points < 0:
            print "You can't do that"
            break
        mana = int(raw_input("how many points would you like to add to mana"))
        points -= mana
        mana = char.insert(2,mana)
        print char
        strength = int(raw_input("How many points would you like to add to strength?"))
        points -= strength
        strength = char.insert(3, strength)
        print "Your strength is " + str(char[3])
        print points,
        print "points remaining"
        if points > 0:
            print "you still have %s points left" %points
        print "Your name is " + str(char[0])
        print "Your health is " + str(char[1])
        print "Your mana is " + str(char[2])
        print "Your strength is " + str(char[3])
        print "and so we begin or journey"
        print "You see a castle in the distance"
        ans= raw_input("shall we go there?").lower()
        if ans == "yes":
                print "you approach the castle as you near the drawbrige you hear a voice shout Halt! "
        ans=raw_input("Do you stop or attack").lower()
        if ans == "attack":
            bat()


#2

yea, you could use classes.

The difficult bit is to actually have a good design. you could do something like:

class Characters(object):
    def __init__(self, health, strength, mana):
         self.health = health
         self.strength = strength
         self.mana = mana

now you can make your characters and enemies classes by inheriting character class

you can also go to sololearn, once you get to OOP there, they make a small game, you could use it for inspiration

i would do the spells/weapons as dictionaries, given you can have the weapon as key and the weapon damage as value


#3

Thank you sololearn looks cool. I went back to redo the classes chapter still not sure how to use them or how to manipulate lists in dictionarys but leaning.
heres tonight's make from what i learned today.
Having trouble getting the stats to be correct.

from random import randint


def status():
    print ("your name is %s") % char[0]
    print ("Health is :%s ")  %char[1]
    print ("Mana is : %s") % char[2]
    print ("and you have % exp") % char[3]
    menu()

def level_up():
    if exp >= 5:
        char[1] += 5
        char[2] += 5
        print "+ five to health and mana"
    menu()


def move():
    print("Test.")
    command = raw_input("> ")
    func_dict[command]()


def run():
    print "Running !"
    command = raw_input("> ")
    func_dict[command]()


def bat():

    health = char[1]
enemy = char[2]
while health >= 1 or enemy >= 1:
    enemy -= randint(1,10)
    health -= randint(1,5)
    char[1] -= enemy
    char[2] -= health
    print char[1]
    print char[2]
    if health <= 0:
        print "you have died!"
        menu()
    elif enemy <= 0:
        print "you killed the enemy!"
        x = randint(1, 25)
        char[3] += x
        menu()
    print enemy
    print health
def menu():
    print "Main menu please choose from 'run' 'move''bat' 'status' or lvl_up"
    command = raw_input("> ")
    if command not in func_dict:
        print "sorry %s I don't understand the question" % name
        menu()
    else:
        func_dict[command]()

func_dict = {'move': move, 'run': run, 'bat': bat
    , 'status': status, 'lvl_up': level_up, }
if __name__ == "__main__":
    raw_input("Press enter to begin.")
    exp = 40
    char = []
    mana = 0
    name = str(raw_input("Name your character"))
    print("you have %s exp to spend on Health and Mana") % exp
    health = int(raw_input("How many would you like to add to health"))
    exp -= health
    print("You have %s xp left adding remainder to mana")% exp
    mana += exp
    exp-= mana
    while health == 0 or health >= 41:
        health = int(raw_input("Please pick a value from 1 - 40"))
    char = [name, health, mana, exp]
    menu()

#4

its difficult using classes, i build something:

from random import randint

class Characters(object):
  def __init__(self, name, health, mana, exp):
    self.name = name
    self.health = health 
    self.mana = mana
    self.exp = exp

class Player(Characters):
  #func_dict = {'move': move, 'run': run, 'bat': bat, 'status': status, 'lvl_up': level_up}
  def actions(self):
    self.func_dict = {'status': self.status, 'move': self.move, 'run': self.run, 'bat': self.bat}
  def menu(self):
    print "Main menu please choose from 'run' 'move''bat' 'status' or lvl_up"
    command = raw_input("> ")
    if command not in self.func_dict:
        print "sorry {} I don't understand the question".format(name)
        menu()
    else:
        self.func_dict[command]()
  def status(self):
    print "your name is: {}\nHealth is: {}\nMana is: {}\nAnd you have: {} xp".format(self.name, self.health, self.mana, self.exp)
    self.menu()
  def level_up(self):
    if self.exp >= 5:
      self.health += 5 
      self.mana += 5 
      self.exp -= 5 
      print "Your mana and health where increased by 5.\nhealth: {}\nmana: {}".format(self.health, self.mana)
  def move(self):
    print("Test.")
    command = raw_input("> ")
    self.func_dict[command]()
  def run(self):
    print "Running !"
    command = raw_input("> ")
    self.func_dict[command]()
  def bat(self):
    pass


if True:#__name__ == "__main__":
    raw_input("Press enter to begin.")
    exp = 40
    mana = 0
    name = str(raw_input("Name your character"))
    print("you have {} exp to spend on Health and Mana").format(exp)
    while True:
      try:
        health = int(raw_input("How many would you like to add to health"))
      except ValueError:
        print "not a number"
      else:
        break
    exp -= health
    print("You have %s xp left adding remainder to mana")% exp
    mana += exp
    exp-= mana
    while health == 0 or health >= 41:
        health = int(raw_input("Please pick a value from 1 - 40"))
    player = Player(name, health, mana, exp)
    player.actions()
    player.menu()

see if you can use it


#5

Thank you I think I understand alot better but what am I doing wrong here? I've tried it so many diff ways.

from random import randint


fire_ball = [10]
fire_beam = [15]
Fira = [50]
gun = [15]
class Characters(object):
    def __init__(self, name, health, mana, exp):
        self.name = name
        self.health = health
        self.mana = mana
        self.exp = exp




class Enemy(Characters):
    def attrib(self):
        print "enemy name: {} health: {} mana: {} xp{}".format(self.name, self.health, self.mana, self.exp)


slime = Enemy("slime", 15, 0, 0)
muck = Enemy("muck", 20, 15, 0)


class Player(Characters):

    backpack = []

# func_dict = {'move': move, 'run': run, 'bat': bat, 'status': status, 'lvl_up': level_up}
def actions(self):
    self.func_dict = {'status': self.status, 'move': self.move, 'run': self.run, 'bat': self.bat, 'lvl' : self.lvl, }

def menu(self):
    print "Main menu please choose from 'run' 'move''bat' 'status' or lvl_up"
    command = raw_input("> ")
    if command not in self.func_dict:
        print "sorry {} I don't understand the question".format(name)
        self.menu()
    else:
        self.func_dict[command]()

def status(self):
    print "your name is: {}\nHealth is: {}\nMana is: {}\nAnd you have: {} xp".format(self.name, self.health,
                                                                                     self.mana, self.exp)
    self.menu()

def lvl(self):
    if self.exp >= 5:
        self.health += 5
        self.mana += 5
        self.exp -= 5
        print "Your mana and health where increased by 5.\nhealth: {}\nmana: {}".format(self.health, self.mana)

def move(self):
    print("Test.")
    command = raw_input("> ")
    self.func_dict[command]()

def run(self):
    print "Running !"
    command = raw_input("> ")
    self.func_dict[command]()

def bat(self):
    while self.health >= 0 or slime.health >= 0:
        self.health -= randint(1,4)
        slime.health -= randint(1, 8)
        print  "you have {} left".format(self.health)
        print "your opponent has {} left ".format(slime.health)
        if self.health <= 0:
            print "you have died!"
            self.menu()
        elif slime.health <= 0:
            print "you killed the enemy!"
            x = randint(1, 10)
            self.exp += x
            self.menu()
        print self.health
        print self.mana
mage = Player("gandolf", 100, 100, 0)
Knight = Player("lancelot", 100, 100, 0)
warrior = Player("Zena", 100, 100, 100)


if True:  # __name__ == "__main__":
    raw_input("Press enter to begin.")
    x = str(raw_input("Please choose from knight,mage or warrior"))
    ## How do I get this to be the mage ins
    choice = x
    player = choice
    player.actions()
    player.menu()
 


#6

mostly with this part:

if True:  # __name__ == "__main__":
    raw_input("Press enter to begin.")
    choice = str(raw_input("Please choose from knight,mage or warrior"))
    ## How do I get this to be the mage ins
    player = Player("{}{}{}{}{}").format(choice.name,choice.health,choice.mana,choice.exp)
    player.actions()
    player.menu()

#7

don't use a string and format here, just use the variables containing the values:

player = Player(name, health, mana, exp)

#8

but I want to use the premade character instances have under the Player class.


#9

What do you mean? Here we made player, which can be premade. Just supply values, don't use format(), that is for printing a string


#10

mage = Player("gandolf", 100, 100, 0)
Knight = Player("lancelot", 100, 100, 0)
warrior = Player("Zena", 100, 100, 100)

I have these 3 and i wanted the player to chose between them.


#11

you can make a list, that should make it easier? Then you can just ask if the user wants gandolf, lancelot or zena

mage = Player("gandolf", 100, 100, 0)
knight = Player("lancelot", 100, 100, 0)
warrior = Player("Zena", 100, 100, 100)

character = [mage, knight, warrior]

#12

This is what im trying now but i get x is not defined

if True:  # __name__ == "__main__":
    raw_input("Press enter to begin.")
    choice = str(raw_input("Please choose from knight,mage or warrior"))
    if choice == mage:
        x = mage
    elif choice == warrior:
        x = Player("Zena", 100, 100, 100)
    elif choice == knight:
        x = Player("lancelot",100,100,100,)
    else:
        print "not a choice"

    player = x
    player.actions()
    player.menu()

#13

here:

elif choice == warrior:

why did you make warrior a variable? Where did you define it? I think it should be a string


#14

Wow that fixes a lot of frustration I've been been having. I was thinking since input was a string and it was after a == that it wasn't necessary .


#15

Is there a way to get the fireball to hit whatever enemy instead of listing everyone?

from random import randint


fire_ball = [100]
fire_beam = [15]
Fira = [50]
gun = [15]
class Characters(object):
    def __init__(self, name, health, mana, exp,lvl):
        self.name = name
        self.health = lvl * 50
        self.mana = mana
        self.exp = exp
        self.lvl = lvl



class Enemy(Characters):
    def attrib(self):
        print "enemy name: {} health: {} mana: {} xp{}".format(self.name, self.health, self.mana, self.exp)


slime = Enemy("slime", 15, 0, 0,1)
muck = Enemy("muck", 20, 15, 0,1)

class Player(Characters):

    backpack = []

    # func_dict = {'move': move, 'run': run, 'bat': bat, 'status': status, 'lvl_up': level_up}
    def actions(self):
        self.func_dict = {'status': self.status, 'move': self.move, 'run': self.run, "bat": self.bat,'lvl_up' : self.lvl_up,'fireball' : self.fireball }

    def menu(self):
       # {'status': self.status, 'move': self.move, 'run': self.run, 'bat': self.bat,: self.lvl, }
        print "Main menu please choose from 'run' 'move''bat' 'status' or lvl_up"
        command = raw_input("> ")
        if command not in self.func_dict:
            print "sorry {} I don't understand the question".format(self.name)
            self.menu()
        else:
            self.func_dict[command]()
    def bat_menu(self):
        print "The fight has started attack, magic or item"
    def status(self):
        print "your name is: {}\nHealth is: {}\nMana is: {}\nAnd you have: {} xp".format(self.name, self.health,self.mana, self.exp)
        self.menu()



    def move(self):
        print("Test.")
        command = raw_input("> ")
        self.func_dict[command]()

    def run(self):
        print "Running !"
        command = raw_input("> ")
        self.func_dict[command]()

    def lvl_up(self):
        if self.exp >= 5:
            self.health += 5
            self.mana += 5
            self.exp -= 5
            print "Your mana and health where increased by 5.\nhealth: {}\nmana: {}".format(self.health, self.mana)
            self.menu()
    def bat(self,enemy):
        """

        :type enemy: object
        """
        while self.health >= 0 or  enemy.health >= 0:
            player.actions()
            player.menu()
            self.health -= randint(1,4)
            enemy.health -= randint(1, 8)
            print  "you have {} left".format(self.health)
            print "your opponent has {} left ".format(enemy.health)
            if self.health <= 0:
                print "you have died!"
                print "Game Over"
                quit("Game over")
            elif enemy.health <= 0:
                print "you killed the enemy!"
                x = randint(1, 10)
                self.exp += x
                self.menu()
            print self.health
            print enemy.health

    def fireball(self):       ###how do I get this to target any enemy enemy.health says enemy not defined
        slime.health -= fire_ball[0]
        muck.health -= fire_ball[0]

        print self.health



mage = Player("gandolf", 100, 100, 0,1)
knight = Player("lancelot", 100, 100, 0,1)
warrior = Player("Zena", 100, 100, 100,1)

if True:  # __name__ == "__main__":
    raw_input("Press enter to begin.")
    finish = 0
    choice = ("")
    while True:
        finish == 0
        choice != "mage" or choice != "warrior" or choice != "knight"
        choice = str(raw_input("Please choose from knight,mage or warrior"))
        if choice == ('mage'):
            x = mage
            player = x
            finish += 1
        elif choice == ("warrior"):
            x = warrior
            player = x
        elif choice == ('knight'):
            x = knight
            player = x
        else:
            print "not an option"
        if finish == 1:
            print "you've encountered a slime"
            player.bat(slime)
            player.actions()
            player.menu()
            player.bat(slime)
            player.actions()
            player.menu()

#16

i get an error:

Traceback (most recent call last):
File "python", line 126, in
File "python", line 75, in bat
File "python", line 42, in menu
TypeError: bat() takes exactly 2 arguments (1 given)

and i don't see it.


#17

I get that to if I just call bat() from the menu but if i use bat(slime) or any other enemy it works i haven't changed it on the menu.
I'm stuck here

def fireball(self): ###how do I get this to target any enemy enemy.health says enemy not defined
slime.health -= fire_ball[0]
muck.health -= fire_ball[0]

    print self.health

am I going to have to do a line for each possible enemy?
I've tried using fire_ball(self,bat) ,fire_ball(self,enemy)
or enemy.health -= #this one says enemy not defined.

Thank you for your help.


#18

i modified your code a bit:

from random import randint


fire_ball = [100]
fire_beam = [15]
Fira = [50]
gun = [15]
class Characters(object):
    def __init__(self, name, health, mana, exp,lvl):
        self.name = name
        self.health = lvl * 50
        self.mana = mana
        self.exp = exp
        self.lvl = lvl



class Enemy(Characters):
    def attrib(self):
        print "enemy name: {} health: {} mana: {} xp{}".format(self.name, self.health, self.mana, self.exp)


slime = Enemy("slime", 15, 0, 0,1)
muck = Enemy("muck", 20, 15, 0,1)

class Player(Characters):

    backpack = []

    # func_dict = {'move': move, 'run': run, 'bat': bat, 'status': status, 'lvl_up': level_up}
    def actions(self):
        self.func_dict = {'status': self.status, 'move': self.move, 'run': self.run, "bat": self.bat,'lvl_up' : self.lvl_up,'fireball' : self.fireball }

    def menu(self):
       # {'status': self.status, 'move': self.move, 'run': self.run, 'bat': self.bat,: self.lvl, }
        print "Main menu please choose from 'run' 'move''bat' 'status' or lvl_up"
        command = raw_input("> ")
        if command not in self.func_dict:
            print "sorry {} I don't understand the question".format(self.name)
            self.menu()
        else:
            self.func_dict[command]()
    def bat_menu(self):
        print "The fight has started attack, magic or item"
    def status(self):
        print "your name is: {}\nHealth is: {}\nMana is: {}\nAnd you have: {} xp".format(self.name, self.health,self.mana, self.exp)
        self.menu()



    def move(self):
        print("Test.")
        command = raw_input("> ")
        self.func_dict[command]()

    def run(self):
        print "Running !"
        command = raw_input("> ")
        self.func_dict[command]()

    def lvl_up(self):
        if self.exp >= 5:
            self.health += 5
            self.mana += 5
            self.exp -= 5
            print "Your mana and health where increased by 5.\nhealth: {}\nmana: {}".format(self.health, self.mana)
            self.menu()
    def bat(self,enemy):
        """
        :type enemy: object
        """
        while self.health >= 0 or  enemy.health >= 0:
            player.actions()
            player.menu()
            self.health -= randint(1,4)
            enemy.health -= randint(1, 8)
            print  "you have {} left".format(self.health)
            print "your opponent has {} left ".format(enemy.health)
            if self.health <= 0:
                print "you have died!"
                print "Game Over"
                quit("Game over")
            elif enemy.health <= 0:
                print "you killed the enemy!"
                x = randint(1, 10)
                self.exp += x
                self.menu()
            print self.health
            print enemy.health

    def fireball(self):       ###how do I get this to target any enemy enemy.health says enemy not defined
        slime.health -= fire_ball[0]
        muck.health -= fire_ball[0]

        print self.health



mage = Player("gandolf", 100, 100, 0,1)
knight = Player("lancelot", 100, 100, 0,1)
warrior = Player("Zena", 100, 100, 100,1)


raw_input("Press enter to begin.")
finish = 0
choice = ("")
while True:
    finish == 0
    choice != "mage" or choice != "warrior" or choice != "knight"
    choice = str(raw_input("Please choose from knight,mage or warrior"))
    if choice == ('mage'):
        player = mage
        break
    elif choice == ("warrior"):
        x = warrior
        player = x
    elif choice == ('knight'):
        x = knight
        player = x

print "you've encountered a slime"
player.bat(slime)
player.actions()
player.menu()
player.bat(slime)
player.actions()
player.menu()

If i enter fireball, something happens.

I can't find the mistakes, today programming is just working for me