954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python Game: Norbert Quest

Yeah so I was bored in class cuz I didnt know the test wasn't today, so I made part of a Python game. The battling works so far, and I might need some help later on so if anyone could could help me I'd appreaciate it.

Norbert Quest:

import random

m = random.randrange(50)+1

#main
health = 150
ehealth = 50
money = 0

def Shop():
    global weapon
    weapon[0] = "Steel Knuckles"
    weapon[1] = "Knife"
    weapon[3] = "Sword"
    weapon[4] = "Gun"
    weapon[5] = "Rocket Launcher"
    print \
	 """
	 1 - Steel Knuckles 20$
	 2 - Knife 30$
	 3 - Sword 100$
 	 4 - Gun 300$
	 5 - Rocket Launcher 1000$
	 """
    z = input("Which weapon do you want?")
    if money >= cost:
        print"You have bought a",weapon[z]
        money = money - c
        mode = restart
    elif money < cost:
        print"You don't have enough gold to buy this."
        raw_input("Press [enter] to exit.")
        mode = restart

def eAttack():
    attacke = random.randrange(15)+1
    return attacke

def HP():
    e = eAttack()
    global health
    health = health - e
    print"Your HP is",health

def Gold():
    global money

def nAttack():
    attack = random.randrange(10)+1
    return attack

def EnemyHP():
    n = nAttack()
    global ehealth
    ehealth = ehealth - n
    print "Monster's HP is",ehealth

while True:
    print \
          """
          Welcome to NorbertQuest!
          Type:
          - [battle] to hunt monsters
          - [shop] to buy weapons
          - [exit] to quit the game.
          """
    mode = raw_input("What do you want to do?: ")

    while"battle"in mode:
        turn = raw_input("Do you want to Attack or Defend?")
        if"attack"in turn:
            print"You're attacking!"
            EnemyHP()
            HP()
        elif"defend"in turn:
            print"You're defending!"
            print"Monster's HP is",ehealth
            print"Your HP is",health
        else:
            print"Type attack or Defend!"

        if health <= 0:
            print"You are dead!"
            mode = restart
        if ehealth <= 0:
            print "You win the battle! You earn",m,"$!"
            money = money + m


    while"exit"in mode:
        exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
        if"yes"in exit:
            break
        elif"no"in exit:
            mode = restart


raw_input("Press [enter] to exit.")
Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

New syntex (it's a new post cuz I can only edit 2 times):

import random

m = random.randrange(50)+1

#main
health = 150
ehealth = 50
money = 0

def Shop():
    weapon1 = "Steel Knuckles"
    weapon2 = "Knife"
    weapon3 = "Sword"
    weapon4 = "Gun"
    weapon5 = "Rocket Launcher"
    print \
	 """
	 1 - Steel Knuckles 20$
	 2 - Knife 30$
	 3 - Sword 100$
 	 4 - Gun 300$
	 5 - Rocket Launcher 1000$
	 """
    z = input("Which weapon do you want?")
    if money >= cost:
        if z = 1:
	    c = 20
	    print"You have bought a",weapon[z]
            money = money - c
	if z = 2:
	    c = 30
	    print"You have bought a",weapon[z]
            money = money - c
	if z = 3:
	    c = 100
	    print"You have bought a",weapon[z]
            money = money - c
	if z = 4:
	    c = 300
	    print"You have bought a",weapon[z]
            money = money - c
	if z = 5:
	    c = 1000
	    print"You have bought a",weapon[z]
            money = money - c
        mode = restart
    elif money < cost:
        print"You don't have enough gold to buy this."
        raw_input("Press [enter] to exit.")
        mode = restart

def eAttack():
    attacke = random.randrange(15)+1
    return attacke

def HP():
    e = eAttack()
    global health
    health = health - e
    print"Your HP is",health

def Gold():
    global money

def nAttack():
    attack = random.randrange(10)+1
    return attack

def EnemyHP():
    n = nAttack()
    global ehealth
    ehealth = ehealth - n
    print "Monster's HP is",ehealth

while True:
    print \
          """
          Welcome to NorbertQuest!
          Type:
          - [battle] to hunt monsters
          - [shop] to buy weapons
          - [exit] to quit the game.
          """
    mode = raw_input("What do you want to do?: ")

    while"battle"in mode:
        turn = raw_input("Do you want to Attack or Defend?")
        if"attack"in turn:
            print"You're attacking!"
            EnemyHP()
            HP()
        elif"defend"in turn:
            print"You're defending!"
            print"Monster's HP is",ehealth
            print"Your HP is",health
        else:
            print"Type attack or Defend!"

        if health <= 0:
            print"You are dead!"
            mode = restart
        if ehealth <= 0:
            print "You win the battle! You earn",m,"$!"
            money = money + m


    while"exit"in mode:
        exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
        if"yes"in exit:
            break
        elif"no"in exit:
            mode = restart


raw_input("Press [enter] to exit.")
Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

Well here's version 2 of the text game. There's a lot of bugs around the shop, and also I can't figure out how to make the monster and you get full HP after the battle's over.

V.3:

import random
w = 0
m = random.randrange(50)+1
cost = 0

#main
health = 150
ehealth = 50
money = 0

def Shop():
    global money
    weapon1 = "Steel Knuckles"
    weapon2 = "Knife"
    weapon3 = "Sword"
    weapon4 = "Gun"
    weapon5 = "Rocket Launcher"
    print \
     """
     1 - Steel Knuckles 20$
     2 - Knife 30$
     3 - Sword 100$
     4 - Gun 300$
     5 - Rocket Launcher 1000$
     """
    z = input("Which weapon do you want?")
    if z == 1:
        cost == 20
        if money >= cost:
            print"You have bought",weapon1
            money = money - cost
            w == 1
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    if z == 2:
        cost == 30
        if money >= cost:
            print"You have bought",weapon2
            money = money - cost
            w == 2
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    if z == 3:
        cost == 100
    if money >= cost:
            print"You have bought",weapon3
            money = money - cost
            w == 3
            mode = "restart"
    elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    if z == 4:
        cost == 300
        if money >= cost:
            print"You have bought",weapon4
            money = money - cost
            w == 4
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    if z == 5:
        cost == 1000
        if money >= cost:
            print"You have bought",weapon5
            money = money - cost
            w == 5
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"

def eAttack():
    attacke = random.randrange(15)+1
    return attacke

def HP():
    e = eAttack()
    global health
    health = health - e
    print"Your HP is",health
    if mode == "restart":
        health = 150

def Gold():
    global money

def nAttack():
    attack = random.randrange(10)+1
    return attack
    if w == 1:
        attack = random.randrange(20)+1
    elif w == 2:
        attack = random.randrange(25)+1
    elif w == 3:
        attack = random.randrange(30)+1
    elif w == 4:
        attack = random.randrange(45)+1
    elif w == 5:
        attack = random.randrange(60)+1
    

def EnemyHP():
    n = nAttack()
    global ehealth
    ehealth = ehealth - n
    print "Monster's HP is",ehealth
    if mode == "restart":
        ehealth = 100

monster = 0

while True:
    print \
          """
          Welcome to NorbertQuest!
          Type:
          - [battle] to hunt monsters
          - [shop] to buy weapons
          - [exit] to quit the game.
          """
    mode = raw_input("What do you want to do?: ")
    while mode == "shop":
        Shop()
    while mode == "battle":
        fight = random.randrange(10)+1
        if fight == 1:
            monster = "Dragon!"
        elif fight == 2:
            monster = "Wild Cayote!"
        elif fight == 3:
            monster == "Jaguar!"
        elif fight == 4:
            monster = "Orc Lord!"
        elif fight == 5:
            monster = "Goblin!"
        elif fight == 6:
            monster = "Crazy Robot!"
        elif fight == 7:
            monster = "Werewolf!"
        elif fight == 8:
            monster = "Vampire!"
        elif fight == 9:
            monster = "Giant Spider!"
        elif fight == 10:
            monster = "Pirate King"
        turn = raw_input("Do you want to Attack or Defend?")
        
        if"attack"in turn:
            print"You're attacking!"
            EnemyHP()
            HP()
        elif"defend"in turn:
            print"You're defending!"
            print"Monster's HP is",ehealth
            print"Your HP is",health
        else:
            print"Type attack or Defend!"

        if health <= 0:
            print"You are dead!"
            mode = "restart"
        elif ehealth <= 0:
            print "You win the battle! You earn",m,"$!"
            money = money + m
            mode = "restart"


    while mode == "exit":
        exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
        if"yes"in exit:
            break
        elif"no"in exit:
            mode = "restart"


raw_input("Press [enter] to exit.")
Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

I would reset the player and monster health at the start of the battle.

The weapons would make a good class, the weapon has a name, a cost and an amount of damage. Then you could make a list of weapons that could be iterated for display at the shop. When the player buys a weapon, you could update their 'current' weapon and then use the 'current' weapon when calculating damage during the battle.

The following is some sample code of how the shop might work after you create the weapon class and a list of them. The sample code does not attempt to format the list in a 'pretty' form or to validate the user input, both would be beneficial updates.

for idx in range(len(weaponlist)):
    print idx + 1, weaponlist[idx].description, weaponlist[idx].cost
z = input("Which weapon do you want?")
if money > weaponlist[z].cost:
    playerweapon = weaponlist[z]
    print "You have bought", playerweapon.description
else:
    print "You can't afford the", weaponlist[z].description


The functions that display the health (of the player and the enemy) also calculate the damage applied and as such are poorly named. (The function name does not reflect what it does.) I would tend to separate them into a couple of functions, one to display the health and another to calculate the damage for an attack and apply it.

In the current program it is possible that the player might not have enough money to even buy the lowest weapon. If they can't buy a weapon, how much damage should they do?

The fight sequence currently does not take into account the weapon being used. (There is code that appears to want to support the concept, but the damage function returns before any of that code can be executed.)

Currently there is no difference between the monsters, they all have the same health and do the same damage. Was this how you intended it to work? (If you make some of the monsters harder, might the player want to 'run away' from something that is too tough?) Should there be better rewards from fighting harder monsters?

Right now, all damages are 'flat' random chances. If the damage is 1 to 10, all of the numbers from 1 to 10 are equally likely. A lot of 'combat' games have the concept of dice for damage. Where a weapon might use 2 six-sided dice commonly shown as (2d6). This example weapon will do from 2 to 12 points of damage, but not all numbers are as likely. The extremes (2 and 12) are rare and the most common damage will be 7. This would likely apply to both player and monster damage calculations.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

That's what 'w' is for. If you buy weapon1 it'll change the base damage (random number out of 10) to random number out of 20 or whatever that other number was. Any idea how I can fix the shop function because I tried it and I bought the most expensive weapon at the beginning of the game so something's definitely wrong.

Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

The problem in the store is the mixing of assignment and test operators. You must use == for comparison and = for assignment.

You have:

if z == 1:
        cost == 20
        if money >= cost:
            print"You have bought",weapon1
            money = money - cost
            w == 1
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"


It should be:

if z == 1:
        cost = 20
        if money >= cost:
            print"You have bought",weapon1
            money = money - cost
            w = 1
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"


Try that and see if it helps.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

I originally made it like that but it was marked as an invalid syntax

Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

If you have all of the indenting right, I think that should be ok. If not, post the code along with the compiler error and we'll figure it out.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

Okay thanks I figured it out. Now everything works right except for the HP resetting at the beginning of every battle. Here it is:

import random
w = 0
m = random.randrange(50)+1
cost = 0

#main
health = 150
ehealth = 50
money = 0

def Shop():
    global money
    weapon1 = "Steel Knuckles"
    weapon2 = "Knife"
    weapon3 = "Sword"
    weapon4 = "Gun"
    weapon5 = "Rocket Launcher"
    print \
     """
     What'll it be?
     1 - Steel Knuckles 20$
     2 - Knife 30$
     3 - Sword 100$
     4 - Gun 300$
     5 - Rocket Launcher 1000$
     Type exit to quit.
     """
    z = input("Which weapon do you want?")
    if z == 1:
        cost = 20
        if money >= cost:
            print"You have bought",weapon1
            money = money - cost
            w = 1
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 2:
        cost = 30
        if money >= cost:
            print"You have bought",weapon2
            money = money - cost
            w = 2
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 3:
        cost = 100
        if money >= cost:
            print"You have bought",weapon3
            money = money - cost
            w = 3
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 4:
        cost = 300
        if money >= cost:
            print"You have bought",weapon4
            money = money - cost
            w = 4
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 5:
        cost = 1000
        if money >= cost:
            print"You have bought",weapon5
            money = money - cost
            w = 5
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == "exit":
        mode = "restart"

def eAttack():
    attacke = random.randrange(15)+1
    return attacke
    if mode = "restart":
        health = 100

def HP():
    e = eAttack()
    global health
    health = health - e
    print"Your HP is",health
    if mode = "restart":
        health = 150

def Gold():
    global money

def nAttack():
    attack = random.randrange(10)+1
    return attack
    if w = 1:
        attack = random.randrange(20)+1
    elif w = 2:
        attack = random.randrange(25)+1
    elif w = 3:
        attack = random.randrange(30)+1
    elif w = 4:
        attack = random.randrange(45)+1
    elif w = 5:
        attack = random.randrange(60)+1
    

def EnemyHP():
    n = nAttack()
    global ehealth
    ehealth = ehealth - n
    print "Monster's HP is",ehealth
    if mode = "restart":
        ehealth = 100

monster = 0

while True:
    print \
          """
          Welcome to NorbertQuest!
          Type:
          - [battle] to hunt monsters
          - [shop] to buy weapons
          - [exit] to quit the game.
          """
    mode = raw_input("What do you want to do?: ")
    if mode == "shop":
        Shop()


    while mode == "battle":
        fight = random.randrange(10)+1
        if fight == 1:
            monster = "Dragon!"
        elif fight == 2:
            monster = "Wild Cayote!"
        elif fight == 3:
            monster == "Jaguar!"
        elif fight == 4:
            monster = "Orc Lord!"
        elif fight == 5:
            monster = "Goblin!"
        elif fight == 6:
            monster = "Crazy Robot!"
        elif fight == 7:
            monster = "Werewolf!"
        elif fight == 8:
            monster = "Vampire!"
        elif fight == 9:
            monster = "Giant Spider!"
        elif fight == 10:
            monster = "Pirate King"
        turn = raw_input("Do you want to Attack or Defend?")
        
        if"attack"in turn:
            print"You're attacking!"
            EnemyHP()
            HP()
        elif"defend"in turn:
            print"You're defending!"
            print"Monster's HP is",ehealth
            print"Your HP is",health
        else:
            print"Type attack or Defend!"

        if health <= 0:
            print"You are dead!"
            mode = "restart"
        elif ehealth <= 0:
            print "You win the battle! You earn",m,"$!"
            money = money + m
            mode = "restart"


    if mode == "exit":
        exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
        if"yes"in exit:
            break
        elif"no"in exit:
            mode = "restart"


raw_input("Press [enter] to exit.")


Also I need to know of I can add images linked to a site whenever the battle mode is activated. I've been making these quick pictures in paint while trying to figure out the coding and I was bored. There are pictures of the 6 weapons and 3/10 monsters so far.

Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

Sweet if you google "NorbertQuest" you get this page. Awsome.

Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

I think you should just make a reset heath function

while True:
    print \
          """
          Welcome to NorbertQuest!
          Type:
          - [battle] to hunt monsters
          - [shop] to buy weapons
          - [exit] to quit the game.
          """
    mode = raw_input("What do you want to do?: ")
    if mode == "shop":
        Shop()


    while mode == "battle":
        fight = random.randrange(10)+1
        if fight == 1:
            monster = "Dragon!"
        elif fight == 2:
            monster = "Wild Cayote!"
        elif fight == 3:
            monster == "Jaguar!"
        elif fight == 4:
            monster = "Orc Lord!"
        elif fight == 5:
            monster = "Goblin!"
        elif fight == 6:
            monster = "Crazy Robot!"
        elif fight == 7:
            monster = "Werewolf!"
        elif fight == 8:
            monster = "Vampire!"
        elif fight == 9:
            monster = "Giant Spider!"
        elif fight == 10:
            monster = "Pirate King"
        turn = raw_input("Do you want to Attack or Defend?")
        
        if"attack"in turn:
            print"You're attacking!"
            EnemyHP()
            HP()
        elif"defend"in turn:
            print"You're defending!"
            print"Monster's HP is",ehealth
            print"Your HP is",health
        else:
            print"Type attack or Defend!"

        if health <= 0:
            print"You are dead!"
            mode = "restart"
        elif ehealth <= 0:
            print "You win the battle! You earn",m,"$!"
            money = money + m
            resetHP()

def resetHP():
    global health
    heath = 150


Also this code is dying to be put into a class.

Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

My python isn't working at the moment and I gotta get to school so I can't check if the resetHP function is, well, functioning right. Here it is:

import random
w = 0
m = random.randrange(50)+1
cost = 0

#main
health = 150
ehealth = 50
money = 0

def Shop():
    global money
    weapon1 = "Steel Knuckles"
    weapon2 = "Knife"
    weapon3 = "Sword"
    weapon4 = "Gun"
    weapon5 = "Rocket Launcher"
    print \
     """
     What'll it be?
     1 - Steel Knuckles 20$
     2 - Knife 30$
     3 - Sword 100$
     4 - Gun 300$
     5 - Rocket Launcher 1000$
     Type exit to quit.
     """
    z = input("Which weapon do you want?")
    if z == 1:
        cost = 20
        if money >= cost:
            print"You have bought",weapon1
            money = money - cost
            w = 1
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 2:
        cost = 30
        if money >= cost:
            print"You have bought",weapon2
            money = money - cost
            w = 2
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 3:
        cost = 100
        if money >= cost:
            print"You have bought",weapon3
            money = money - cost
            w = 3
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 4:
        cost = 300
        if money >= cost:
            print"You have bought",weapon4
            money = money - cost
            w = 4
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 5:
        cost = 1000
        if money >= cost:
            print"You have bought",weapon5
            money = money - cost
            w = 5
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == "exit":
        mode = "restart"

def eAttack():
    attacke = random.randrange(15)+1
    return attacke
    if mode == "restart":
        health = 100

def HP():
    e = eAttack()
    global health
    health = health - e
    print"Your HP is",health
    if mode == "restart":
        health = 150

def Gold():
    global money

def nAttack():
    attack = random.randrange(10)+1
    return attack
    if w == 1:
        attack = random.randrange(20)+1
    elif w == 2:
        attack = random.randrange(25)+1
    elif w == 3:
        attack = random.randrange(30)+1
    elif w == 4:
        attack = random.randrange(45)+1
    elif w == 5:
        attack = random.randrange(60)+1
    

def EnemyHP():
    n = nAttack()
    global ehealth
    ehealth = ehealth - n
    print "Monster's HP is",ehealth
    if mode == "restart":
        ehealth = 100

def resetHP():
    global health
    health = 150

def resetEnemyHP():
    global health
    health = 150

monster = 0

while True:
    print \
          """
          Welcome to NorbertQuest!
          Type:
          - [battle] to hunt monsters
          - [shop] to buy weapons
          - [exit] to quit the game.
          """
    mode = raw_input("What do you want to do?: ")
    if mode == "shop":
        Shop()

    if mode == "battle":
        print"You have encountered a,"monster,"What will you do?"
    while mode == "battle":
        fight = random.randrange(10)+1
        if fight == 1:
            monster = "Dragon!"
        elif fight == 2:
            monster = "Wild Cayote!"
        elif fight == 3:
            monster == "Jaguar!"
        elif fight == 4:
            monster = "Orc Lord!"
        elif fight == 5:
            monster = "Goblin!"
        elif fight == 6:
            monster = "Crazy Robot!"
        elif fight == 7:
            monster = "Werewolf!"
        elif fight == 8:
            monster = "Vampire!"
        elif fight == 9:
            monster = "Giant Spider!"
        elif fight == 10:
            monster = "Pirate King!"
        turn = raw_input("Do you want to Attack or Defend?")
        
        if"attack"in turn:
            print"You're attacking!"
            EnemyHP()
            HP()
        elif"defend"in turn:
            print"You're defending!"
            print"Monster's HP is",ehealth
            print"Your HP is",health
        else:
            print"Type attack or Defend!"

        if health <= 0:
            print"You are dead!"
            mode = "restart"
            resetHP()
            resetEnemyHP()
        elif ehealth <= 0:
            print "You win the battle! You earn",m,"$!"
            money = money + m
            mode = "restart"
            resetHP()
            resetEnemyHP()
            


    if mode == "exit":
        exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
        if"yes"in exit:
            break
        elif"no"in exit:
            mode = "restart"


raw_input("Press [enter] to exit.")

Also I've been thinking... would a Levels and Experience-Gaining system function work or would that be too hard?

Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

Well the game's coming along pretty nicely but I need a way to fix that HP resetting function, and I need to add pictures and the save option. Looks like it'll be done in about a couple weeks if I use my weekend time.

Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

You can put the health reset in the main loop, just before the battle loop starts. You'll reset it more than you need to, but everyone will be full health when the battle starts.

while True:
    print \
          """
          Welcome to NorbertQuest!
          Type:
          - [battle] to hunt monsters
          - [shop] to buy weapons
          - [exit] to quit the game.
          """
    mode = raw_input("What do you want to do?: ")
    if mode == "shop":
        Shop()

    # reset health here

    while mode == "battle":
        fight = random.randrange(10)+1


In order to get pictures on the screen, you're going to have to change the program a lot. You need to use one of the python gui models to run your application and they run a lot differently than you have been so far. (I saw your other gui thread. I have used both wxPython and tkInter to fairly good success, you'll have to pick one.)

The 'save' feature is something you'll have to work on as well. First step is to decide what it is that you want to save. We don't have one yet, but does the player have a name? They have gold and a weapon, is that what we save? Do we want to keep some form of battle statistics and save them?

The basic file concepts (from your 'how do I save' thread) are how you get data into and out of the file. How your application interfaces to the files is up to you, but I recommend something like this:Set variables to the default value
Check for the save file
If the save file exists, update the variables from the file

To get the save file created, the application either needs to do saves from time to time (oh, like whenever they complete a battle or shop at the store) or just save when the program exits.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

So then if someone's playing this game they can save it on their hard drives? But first before that, I need to work on a Levels function so there would actually be something to save, and the Images will probably have to wait for now because I need to learn wxPython first. Thanks for the tip on adding the HP reset to the main loop.

Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

Can someone help me with the levels function? The xp gaining thing doesn't seem to work properly. Here it is:

import random
w = 0
level = 1
m = random.randrange(50)+1+level*5
cost = 0
xp = 0


def LVL():
    global level
    global xp
    if level == 1:
        tnl = 100
        if tnl <= xp:
            level = 2
            xp = 0
            print"Your new Level is",level,"! Congratulations!"

    if level == 2:
        tnl = 250
        if tnl <= xp:
            level = 3
            xp = 0
            print"Your new Level is",level,"! Congratulations!"
            
            if level == 3:
                tnl = 450
                if tnl <= xp:
                    level = 4
                    xp = 0
                    print"Your new Level is",level,"! Congratulations!"

                    if level == 4:
                        tnl = 650
                        if tnl <= xp:
                            level = 5
                            xp = 0
                            print"Your new Level is",level,"! Congratulations!"

                            if level == 5:
                                tnl = 900
                                if tnl <= xp:
                                    level = 6
                                    xp = 0
                                    print"Your new Level is",level,"! Congratulations!"

                                    if level == 6:
                                        tnl = 1200
                                        if tnl <= xp:
                                            level = 7
                                            xp = 0
                                            print"Your new Level is",level,"! Congratulations!"

                                            if level == 7:
                                                tnl = 1550
                                                if tnl <= xp:
                                                    level = 8
                                                    xp = 0
                                                    print"Your new Level is",level,"! Congratulations!"

                                                    if level == 8:
                                                        tnl = 1950
                                                        if tnl <= xp:
                                                            level = 9
                                                            xp = 0
                                                            print"Your new Level is",level,"! Congratulations!"

                                                            if level == 9:
                                                                tnl = 2400
                                                                if tnl <= xp:
                                                                    level = 10
                                                                    xp = 0
                                                                    print"Your new Level is",level,"! Congratulations!"

                                                                    if level == 10:
                                                                        tnl = 2900
                                                                        if tnl <= xp:
                                                                            level = 100
                                                                            xp = 0
                                                                            print"Your new Level is",level,"! Congratulations!"
                                                                            print"Congratulations, you beat the Demo!"
    



#main
health = 150
ehealth = 50
money = 0
level = 1

def Shop():
    global money
    weapon1 = "Steel Knuckles"
    weapon2 = "Knife"
    weapon3 = "Sword"
    weapon4 = "Gun"
    weapon5 = "Rocket Launcher"
    print \
     """
     What'll it be?
     1 - Steel Knuckles 20$
     2 - Knife 30$
     3 - Sword 100$
     4 - Gun 300$
     5 - Rocket Launcher 1000$
     Type exit to quit.
     """
    z = input("Which weapon do you want?")
    if z == 1:
        cost = 20
        if money >= cost:
            print"You have bought",weapon1
            money = money - cost
            w = 1
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 2:
        cost = 30
        if money >= cost:
            print"You have bought",weapon2
            money = money - cost
            w = 2
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 3:
        cost = 100
        if money >= cost:
            print"You have bought",weapon3
            money = money - cost
            w = 3
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 4:
        cost = 300
        if money >= cost:
            print"You have bought",weapon4
            money = money - cost
            w = 4
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == 5:
        cost = 1000
        if money >= cost:
            print"You have bought",weapon5
            money = money - cost
            w = 5
            mode = "restart"
        elif money < cost:
            print"You don't have enough gold to buy this."
            raw_input("Press [enter] to exit.")
            mode = "restart"
    elif z == "exit":
        mode = "restart"

def eAttack():
    attacke = random.randrange(10)+level
    return attacke
    if mode == "restart":
        health = 100

def HP():
    e = eAttack()
    global health
    health = health - e
    print"Your HP is",health
    if mode == "restart":
        health = 150

def Gold():
    global money

def nAttack():
    attack = random.randrange(10)+level*5
    return attack
    if w == 1:
        attack = random.randrange(10)+20+level*5
    elif w == 2:
        attack = random.randrange(10)+35+level*5
    elif w == 3:
        attack = random.randrange(10)+55+level*5
    elif w == 4:
        attack = random.randrange(10)+80+level*5
    elif w == 5:
        attack = random.randrange(10)+110+level*5


def EnemyHP():
    n = nAttack()
    global ehealth
    ehealth = ehealth - n
    print "Monster's HP is",ehealth
    if mode == "restart":
        ehealth = 100

def resetHP():
    global health
    health = 150
    global ehealth
    health = 100

monster = "Giant Python!"

while True:
    print \
          """
          Welcome to NorbertQuest!
          Type:
          - [battle] to hunt monsters
          - [shop] to buy weapons
          - [exit] to quit the game.
          """
    mode = raw_input("What do you want to do?: ")
    if mode == "shop":
        Shop()
        
    if mode == "battle":
        print"You have encountered a",monster,"What will you do?"
        health = 150+level*100
        ehealth = 100+level*125
        m = random.randrange(50)+1+level*5
        xpGain = random.randrange(25)+level+25
        xp = xpGain + xp
        LVL()
    while mode == "battle":
        fight = random.randrange(10)+1
        if fight == 1:
            monster = "Dragon!"
        elif fight == 2:
            monster = "Wild Cayote!"
        elif fight == 3:
            monster == "Jaguar!"
        elif fight == 4:
            monster = "Orc Lord!"
        elif fight == 5:
            monster = "Goblin!"
        elif fight == 6:
            monster = "Crazy Robot!"
        elif fight == 7:
            monster = "Werewolf!"
        elif fight == 8:
            monster = "Vampire!"
        elif fight == 9:
            monster = "Giant Spider!"
        elif fight == 10:
            monster = "Pirate King!"
        turn = raw_input("Do you want to Attack or Defend?")
        
        if"attack"in turn:
            print"You're attacking!"
            EnemyHP()
            HP()
        elif"defend"in turn:
            print"You're defending!"
            print"Monster's HP is",ehealth
            print"Your HP is",health
        else:
            print"Type attack or Defend!"

        if health <= 0:
            print"You are dead!"
            mode = "restart"
            resetHP()
        elif ehealth <= 0:
            print "You win the battle! You earn",m,"$!"
            money = money + m
            mode = "restart"
            resetHP()
            m = random.randrange(50)+1+level*5
            xpGain = random.randrange(25)+level+25
            xp = xpGain + xp
            LVL()
            print"You gain",xpGain,"Experience Points!"
            


    if mode == "exit":
        exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
        if"yes"in exit:
            break
        elif"no"in exit:
            mode = "restart"


raw_input("Press [enter] to exit.")
Norbert X
Junior Poster in Training
88 posts since Feb 2009
Reputation Points: 52
Solved Threads: 0
 

I would suggest this

tnls = [100, 250, 450, 650, 900, 1200, 1550, 1950, 2400, 2900]

def LVL():
    global level, xp
    if 1 <= level <= len(tnls):
        tnl = tnls[level-1]
        if tnl <= xp:
            level += 1
            if level > len(tnls):
                level = 100
            xp = 0
            print("Your new level is %d! Congratulations!" % level)
    if level == 100:
        print("Congratulations, you beat the Demo!")
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

you didn't want to keep indenting each new 'if level' test

They should all be at the same indent level, like if level == 1 and if level == 2 are.

(I had typed the following and thought I had posted it, but apparently not...here it is anyway)

Have you ever heard of an array or a list?

If you had a data structure that contained (or was indexed by) the level and the 'tnl' value, the LVL() function could have been implemented with a loop instead of 10 levels or if's with almost identical code that follows each of them. What do you plan to do in the 'official' version when there are more than 10 levels?

I tend to think about XP as always increasing. In my world, XP from 0 to 99 is level 1. XP from 100 to 349 would be level 2, XP 350-799 is level 3...etc.

So if I was at 97 XP and killed a monster worth 20 XP, I could be at 117 XP in level 2 instead of level 2 with zero XP. (Granted that even in your model, instead of setting XP back to zero, you just subtracted the old tnl value you would get the same result.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

PS- your player attack function:

def nAttack():
    attack = random.randrange(10)+level*5
    return attack
    if w == 1:
        attack = random.randrange(10)+20+level*5
    elif w == 2:
        attack = random.randrange(10)+35+level*5
    elif w == 3:
        attack = random.randrange(10)+55+level*5
    elif w == 4:
        attack = random.randrange(10)+80+level*5
    elif w == 5:
        attack = random.randrange(10)+110+level*5


Is still not using any new weapon calculations. (The return on line 3 causes the remaining code to be ignored.) The return should be at the end.

def nAttack():
    attack = random.randrange(10)+level*5
    if w == 1:
        attack = random.randrange(10)+20+level*5
    elif w == 2:
        attack = random.randrange(10)+35+level*5
    elif w == 3:
        attack = random.randrange(10)+55+level*5
    elif w == 4:
        attack = random.randrange(10)+80+level*5
    elif w == 5:
        attack = random.randrange(10)+110+level*5
    return attack
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

Also, the whole shopping sequence could be shortened using a list of shop items like this for example

shop_items = [
    ("Steel Knuckles", 20),
    ("Knife", 30),         
    ("Sword", 100),        
    ("Gun", 300),          
    ("Rocket Launcher", 1000),
    ]                         

def shop_menu_lines():
    yield ''          
    yield "What'll it be ?"
    for index, pair in enumerate(shop_items):
        yield "%d - %s %d$" % (index+1, pair[0], pair[1])
    yield "Type exit to quit"                            

def shop_menu():
    return "\n    ".join(shop_menu_lines())

def Shop():
    global money, mode, w
    print(shop_menu())   
    z = raw_input("Which weapon do you want ?")
    if z == "exit":
        mode = "restart"
    else:
        try:
            z = int(z)
        except ValueError:
            z = -1
        if 1 <= z <= len(shop_items)
            weapon, cost = shop_items[z-1]
            if money >= cost:
                print("You have bought a %s" % weapon.lower())
                money -= cost
                w = z
            else:
                print("You don't have enough gold to buy a %s." % weapon.lower())
                raw_input("press [enter] to exit.")
                mode = "restart"
        else:
            z = -1
        if z < 0:
            print("Invalid choice")
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You