| | |
Python Game: Norbert Quest
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
Norbert Quest:
python Syntax (Toggle Plain Text)
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.")
Last edited by Norbert X; Feb 27th, 2009 at 12:53 pm. Reason: new syntex
New syntex (it's a new post cuz I can only edit 2 times):
Python Syntax (Toggle Plain Text)
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.")
Last edited by Norbert X; Feb 27th, 2009 at 1:04 pm.
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:
V.3:
Python Syntax (Toggle Plain Text)
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.")
•
•
Join Date: May 2008
Posts: 600
Reputation:
Solved Threads: 95
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.
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.
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.
python Syntax (Toggle Plain Text)
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.
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.
•
•
Join Date: May 2008
Posts: 600
Reputation:
Solved Threads: 95
The problem in the store is the mixing of assignment and test operators. You must use
You have:
It should be:
Try that and see if it helps.
== for comparison and = for assignment.You have:
python Syntax (Toggle Plain Text)
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:
python Syntax (Toggle Plain Text)
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.
Okay thanks I figured it out. Now everything works right except for the HP resetting at the beginning of every battle. Here it is:
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.
Python Syntax (Toggle Plain Text)
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.
Last edited by Norbert X; Mar 1st, 2009 at 8:55 pm. Reason: 1. Wrong Syntex 2. Spotted a Mistake
![]() |
Other Threads in the Python Forum
- Previous Thread: Ejecting trays of CDs
- Next Thread: Looping through and removing parts of a list
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals dictionary directory dynamic error examples excel file float font format ftp function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython





