943,553 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 395942
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 27th, 2009
0

Python Game: Norbert Quest

Expand Post »
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:
python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. m = random.randrange(50)+1
  4.  
  5. #main
  6. health = 150
  7. ehealth = 50
  8. money = 0
  9.  
  10. def Shop():
  11. global weapon
  12. weapon[0] = "Steel Knuckles"
  13. weapon[1] = "Knife"
  14. weapon[3] = "Sword"
  15. weapon[4] = "Gun"
  16. weapon[5] = "Rocket Launcher"
  17. print \
  18. """
  19. 1 - Steel Knuckles 20$
  20. 2 - Knife 30$
  21. 3 - Sword 100$
  22. 4 - Gun 300$
  23. 5 - Rocket Launcher 1000$
  24. """
  25. z = input("Which weapon do you want?")
  26. if money >= cost:
  27. print"You have bought a",weapon[z]
  28. money = money - c
  29. mode = restart
  30. elif money < cost:
  31. print"You don't have enough gold to buy this."
  32. raw_input("Press [enter] to exit.")
  33. mode = restart
  34.  
  35. def eAttack():
  36. attacke = random.randrange(15)+1
  37. return attacke
  38.  
  39. def HP():
  40. e = eAttack()
  41. global health
  42. health = health - e
  43. print"Your HP is",health
  44.  
  45. def Gold():
  46. global money
  47.  
  48. def nAttack():
  49. attack = random.randrange(10)+1
  50. return attack
  51.  
  52. def EnemyHP():
  53. n = nAttack()
  54. global ehealth
  55. ehealth = ehealth - n
  56. print "Monster's HP is",ehealth
  57.  
  58. while True:
  59. print \
  60. """
  61. Welcome to NorbertQuest!
  62. Type:
  63. - [battle] to hunt monsters
  64. - [shop] to buy weapons
  65. - [exit] to quit the game.
  66. """
  67. mode = raw_input("What do you want to do?: ")
  68.  
  69. while"battle"in mode:
  70. turn = raw_input("Do you want to Attack or Defend?")
  71. if"attack"in turn:
  72. print"You're attacking!"
  73. EnemyHP()
  74. HP()
  75. elif"defend"in turn:
  76. print"You're defending!"
  77. print"Monster's HP is",ehealth
  78. print"Your HP is",health
  79. else:
  80. print"Type attack or Defend!"
  81.  
  82. if health <= 0:
  83. print"You are dead!"
  84. mode = restart
  85. if ehealth <= 0:
  86. print "You win the battle! You earn",m,"$!"
  87. money = money + m
  88.  
  89.  
  90. while"exit"in mode:
  91. exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
  92. if"yes"in exit:
  93. break
  94. elif"no"in exit:
  95. mode = restart
  96.  
  97.  
  98. raw_input("Press [enter] to exit.")
Last edited by Norbert X; Feb 27th, 2009 at 12:53 pm. Reason: new syntex
Reputation Points: 52
Solved Threads: 0
Junior Poster in Training
Norbert X is offline Offline
87 posts
since Feb 2009
Feb 27th, 2009
0

Re: Python Game: Norbert Quest

New syntex (it's a new post cuz I can only edit 2 times):
Python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. m = random.randrange(50)+1
  4.  
  5. #main
  6. health = 150
  7. ehealth = 50
  8. money = 0
  9.  
  10. def Shop():
  11. weapon1 = "Steel Knuckles"
  12. weapon2 = "Knife"
  13. weapon3 = "Sword"
  14. weapon4 = "Gun"
  15. weapon5 = "Rocket Launcher"
  16. print \
  17. """
  18. 1 - Steel Knuckles 20$
  19. 2 - Knife 30$
  20. 3 - Sword 100$
  21. 4 - Gun 300$
  22. 5 - Rocket Launcher 1000$
  23. """
  24. z = input("Which weapon do you want?")
  25. if money >= cost:
  26. if z = 1:
  27. c = 20
  28. print"You have bought a",weapon[z]
  29. money = money - c
  30. if z = 2:
  31. c = 30
  32. print"You have bought a",weapon[z]
  33. money = money - c
  34. if z = 3:
  35. c = 100
  36. print"You have bought a",weapon[z]
  37. money = money - c
  38. if z = 4:
  39. c = 300
  40. print"You have bought a",weapon[z]
  41. money = money - c
  42. if z = 5:
  43. c = 1000
  44. print"You have bought a",weapon[z]
  45. money = money - c
  46. mode = restart
  47. elif money < cost:
  48. print"You don't have enough gold to buy this."
  49. raw_input("Press [enter] to exit.")
  50. mode = restart
  51.  
  52. def eAttack():
  53. attacke = random.randrange(15)+1
  54. return attacke
  55.  
  56. def HP():
  57. e = eAttack()
  58. global health
  59. health = health - e
  60. print"Your HP is",health
  61.  
  62. def Gold():
  63. global money
  64.  
  65. def nAttack():
  66. attack = random.randrange(10)+1
  67. return attack
  68.  
  69. def EnemyHP():
  70. n = nAttack()
  71. global ehealth
  72. ehealth = ehealth - n
  73. print "Monster's HP is",ehealth
  74.  
  75. while True:
  76. print \
  77. """
  78. Welcome to NorbertQuest!
  79. Type:
  80. - [battle] to hunt monsters
  81. - [shop] to buy weapons
  82. - [exit] to quit the game.
  83. """
  84. mode = raw_input("What do you want to do?: ")
  85.  
  86. while"battle"in mode:
  87. turn = raw_input("Do you want to Attack or Defend?")
  88. if"attack"in turn:
  89. print"You're attacking!"
  90. EnemyHP()
  91. HP()
  92. elif"defend"in turn:
  93. print"You're defending!"
  94. print"Monster's HP is",ehealth
  95. print"Your HP is",health
  96. else:
  97. print"Type attack or Defend!"
  98.  
  99. if health <= 0:
  100. print"You are dead!"
  101. mode = restart
  102. if ehealth <= 0:
  103. print "You win the battle! You earn",m,"$!"
  104. money = money + m
  105.  
  106.  
  107. while"exit"in mode:
  108. exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
  109. if"yes"in exit:
  110. break
  111. elif"no"in exit:
  112. mode = restart
  113.  
  114.  
  115. raw_input("Press [enter] to exit.")
Last edited by Norbert X; Feb 27th, 2009 at 1:04 pm.
Reputation Points: 52
Solved Threads: 0
Junior Poster in Training
Norbert X is offline Offline
87 posts
since Feb 2009
Feb 27th, 2009
0

Re: Python Game: Norbert Quest

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:
Python Syntax (Toggle Plain Text)
  1. import random
  2. w = 0
  3. m = random.randrange(50)+1
  4. cost = 0
  5.  
  6. #main
  7. health = 150
  8. ehealth = 50
  9. money = 0
  10.  
  11. def Shop():
  12. global money
  13. weapon1 = "Steel Knuckles"
  14. weapon2 = "Knife"
  15. weapon3 = "Sword"
  16. weapon4 = "Gun"
  17. weapon5 = "Rocket Launcher"
  18. print \
  19. """
  20. 1 - Steel Knuckles 20$
  21. 2 - Knife 30$
  22. 3 - Sword 100$
  23. 4 - Gun 300$
  24. 5 - Rocket Launcher 1000$
  25. """
  26. z = input("Which weapon do you want?")
  27. if z == 1:
  28. cost == 20
  29. if money >= cost:
  30. print"You have bought",weapon1
  31. money = money - cost
  32. w == 1
  33. mode = "restart"
  34. elif money < cost:
  35. print"You don't have enough gold to buy this."
  36. raw_input("Press [enter] to exit.")
  37. mode = "restart"
  38. if z == 2:
  39. cost == 30
  40. if money >= cost:
  41. print"You have bought",weapon2
  42. money = money - cost
  43. w == 2
  44. mode = "restart"
  45. elif money < cost:
  46. print"You don't have enough gold to buy this."
  47. raw_input("Press [enter] to exit.")
  48. mode = "restart"
  49. if z == 3:
  50. cost == 100
  51. if money >= cost:
  52. print"You have bought",weapon3
  53. money = money - cost
  54. w == 3
  55. mode = "restart"
  56. elif money < cost:
  57. print"You don't have enough gold to buy this."
  58. raw_input("Press [enter] to exit.")
  59. mode = "restart"
  60. if z == 4:
  61. cost == 300
  62. if money >= cost:
  63. print"You have bought",weapon4
  64. money = money - cost
  65. w == 4
  66. mode = "restart"
  67. elif money < cost:
  68. print"You don't have enough gold to buy this."
  69. raw_input("Press [enter] to exit.")
  70. mode = "restart"
  71. if z == 5:
  72. cost == 1000
  73. if money >= cost:
  74. print"You have bought",weapon5
  75. money = money - cost
  76. w == 5
  77. mode = "restart"
  78. elif money < cost:
  79. print"You don't have enough gold to buy this."
  80. raw_input("Press [enter] to exit.")
  81. mode = "restart"
  82.  
  83. def eAttack():
  84. attacke = random.randrange(15)+1
  85. return attacke
  86.  
  87. def HP():
  88. e = eAttack()
  89. global health
  90. health = health - e
  91. print"Your HP is",health
  92. if mode == "restart":
  93. health = 150
  94.  
  95. def Gold():
  96. global money
  97.  
  98. def nAttack():
  99. attack = random.randrange(10)+1
  100. return attack
  101. if w == 1:
  102. attack = random.randrange(20)+1
  103. elif w == 2:
  104. attack = random.randrange(25)+1
  105. elif w == 3:
  106. attack = random.randrange(30)+1
  107. elif w == 4:
  108. attack = random.randrange(45)+1
  109. elif w == 5:
  110. attack = random.randrange(60)+1
  111.  
  112.  
  113. def EnemyHP():
  114. n = nAttack()
  115. global ehealth
  116. ehealth = ehealth - n
  117. print "Monster's HP is",ehealth
  118. if mode == "restart":
  119. ehealth = 100
  120.  
  121. monster = 0
  122.  
  123. while True:
  124. print \
  125. """
  126. Welcome to NorbertQuest!
  127. Type:
  128. - [battle] to hunt monsters
  129. - [shop] to buy weapons
  130. - [exit] to quit the game.
  131. """
  132. mode = raw_input("What do you want to do?: ")
  133. while mode == "shop":
  134. Shop()
  135. while mode == "battle":
  136. fight = random.randrange(10)+1
  137. if fight == 1:
  138. monster = "Dragon!"
  139. elif fight == 2:
  140. monster = "Wild Cayote!"
  141. elif fight == 3:
  142. monster == "Jaguar!"
  143. elif fight == 4:
  144. monster = "Orc Lord!"
  145. elif fight == 5:
  146. monster = "Goblin!"
  147. elif fight == 6:
  148. monster = "Crazy Robot!"
  149. elif fight == 7:
  150. monster = "Werewolf!"
  151. elif fight == 8:
  152. monster = "Vampire!"
  153. elif fight == 9:
  154. monster = "Giant Spider!"
  155. elif fight == 10:
  156. monster = "Pirate King"
  157. turn = raw_input("Do you want to Attack or Defend?")
  158.  
  159. if"attack"in turn:
  160. print"You're attacking!"
  161. EnemyHP()
  162. HP()
  163. elif"defend"in turn:
  164. print"You're defending!"
  165. print"Monster's HP is",ehealth
  166. print"Your HP is",health
  167. else:
  168. print"Type attack or Defend!"
  169.  
  170. if health <= 0:
  171. print"You are dead!"
  172. mode = "restart"
  173. elif ehealth <= 0:
  174. print "You win the battle! You earn",m,"$!"
  175. money = money + m
  176. mode = "restart"
  177.  
  178.  
  179. while mode == "exit":
  180. exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
  181. if"yes"in exit:
  182. break
  183. elif"no"in exit:
  184. mode = "restart"
  185.  
  186.  
  187. raw_input("Press [enter] to exit.")
Reputation Points: 52
Solved Threads: 0
Junior Poster in Training
Norbert X is offline Offline
87 posts
since Feb 2009
Feb 28th, 2009
1

Re: Python Game: Norbert Quest

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.
python Syntax (Toggle Plain Text)
  1. for idx in range(len(weaponlist)):
  2. print idx + 1, weaponlist[idx].description, weaponlist[idx].cost
  3. z = input("Which weapon do you want?")
  4. if money > weaponlist[z].cost:
  5. playerweapon = weaponlist[z]
  6. print "You have bought", playerweapon.description
  7. else:
  8. 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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Feb 28th, 2009
0

Re: Python Game: Norbert Quest

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.
Reputation Points: 52
Solved Threads: 0
Junior Poster in Training
Norbert X is offline Offline
87 posts
since Feb 2009
Mar 1st, 2009
0

Re: Python Game: Norbert Quest

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

You have:
python Syntax (Toggle Plain Text)
  1. if z == 1:
  2. cost == 20
  3. if money >= cost:
  4. print"You have bought",weapon1
  5. money = money - cost
  6. w == 1
  7. mode = "restart"
  8. elif money < cost:
  9. print"You don't have enough gold to buy this."
  10. raw_input("Press [enter] to exit.")
  11. mode = "restart"

It should be:
python Syntax (Toggle Plain Text)
  1. if z == 1:
  2. cost = 20
  3. if money >= cost:
  4. print"You have bought",weapon1
  5. money = money - cost
  6. w = 1
  7. mode = "restart"
  8. elif money < cost:
  9. print"You don't have enough gold to buy this."
  10. raw_input("Press [enter] to exit.")
  11. mode = "restart"

Try that and see if it helps.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Mar 1st, 2009
0

Re: Python Game: Norbert Quest

I originally made it like that but it was marked as an invalid syntax
Reputation Points: 52
Solved Threads: 0
Junior Poster in Training
Norbert X is offline Offline
87 posts
since Feb 2009
Mar 1st, 2009
0

Re: Python Game: Norbert Quest

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Mar 1st, 2009
0

Re: Python Game: Norbert Quest

Okay thanks I figured it out. Now everything works right except for the HP resetting at the beginning of every battle. Here it is:
Python Syntax (Toggle Plain Text)
  1. import random
  2. w = 0
  3. m = random.randrange(50)+1
  4. cost = 0
  5.  
  6. #main
  7. health = 150
  8. ehealth = 50
  9. money = 0
  10.  
  11. def Shop():
  12. global money
  13. weapon1 = "Steel Knuckles"
  14. weapon2 = "Knife"
  15. weapon3 = "Sword"
  16. weapon4 = "Gun"
  17. weapon5 = "Rocket Launcher"
  18. print \
  19. """
  20. What'll it be?
  21. 1 - Steel Knuckles 20$
  22. 2 - Knife 30$
  23. 3 - Sword 100$
  24. 4 - Gun 300$
  25. 5 - Rocket Launcher 1000$
  26. Type exit to quit.
  27. """
  28. z = input("Which weapon do you want?")
  29. if z == 1:
  30. cost = 20
  31. if money >= cost:
  32. print"You have bought",weapon1
  33. money = money - cost
  34. w = 1
  35. mode = "restart"
  36. elif money < cost:
  37. print"You don't have enough gold to buy this."
  38. raw_input("Press [enter] to exit.")
  39. mode = "restart"
  40. elif z == 2:
  41. cost = 30
  42. if money >= cost:
  43. print"You have bought",weapon2
  44. money = money - cost
  45. w = 2
  46. mode = "restart"
  47. elif money < cost:
  48. print"You don't have enough gold to buy this."
  49. raw_input("Press [enter] to exit.")
  50. mode = "restart"
  51. elif z == 3:
  52. cost = 100
  53. if money >= cost:
  54. print"You have bought",weapon3
  55. money = money - cost
  56. w = 3
  57. mode = "restart"
  58. elif money < cost:
  59. print"You don't have enough gold to buy this."
  60. raw_input("Press [enter] to exit.")
  61. mode = "restart"
  62. elif z == 4:
  63. cost = 300
  64. if money >= cost:
  65. print"You have bought",weapon4
  66. money = money - cost
  67. w = 4
  68. mode = "restart"
  69. elif money < cost:
  70. print"You don't have enough gold to buy this."
  71. raw_input("Press [enter] to exit.")
  72. mode = "restart"
  73. elif z == 5:
  74. cost = 1000
  75. if money >= cost:
  76. print"You have bought",weapon5
  77. money = money - cost
  78. w = 5
  79. mode = "restart"
  80. elif money < cost:
  81. print"You don't have enough gold to buy this."
  82. raw_input("Press [enter] to exit.")
  83. mode = "restart"
  84. elif z == "exit":
  85. mode = "restart"
  86.  
  87. def eAttack():
  88. attacke = random.randrange(15)+1
  89. return attacke
  90. if mode = "restart":
  91. health = 100
  92.  
  93. def HP():
  94. e = eAttack()
  95. global health
  96. health = health - e
  97. print"Your HP is",health
  98. if mode = "restart":
  99. health = 150
  100.  
  101. def Gold():
  102. global money
  103.  
  104. def nAttack():
  105. attack = random.randrange(10)+1
  106. return attack
  107. if w = 1:
  108. attack = random.randrange(20)+1
  109. elif w = 2:
  110. attack = random.randrange(25)+1
  111. elif w = 3:
  112. attack = random.randrange(30)+1
  113. elif w = 4:
  114. attack = random.randrange(45)+1
  115. elif w = 5:
  116. attack = random.randrange(60)+1
  117.  
  118.  
  119. def EnemyHP():
  120. n = nAttack()
  121. global ehealth
  122. ehealth = ehealth - n
  123. print "Monster's HP is",ehealth
  124. if mode = "restart":
  125. ehealth = 100
  126.  
  127. monster = 0
  128.  
  129. while True:
  130. print \
  131. """
  132. Welcome to NorbertQuest!
  133. Type:
  134. - [battle] to hunt monsters
  135. - [shop] to buy weapons
  136. - [exit] to quit the game.
  137. """
  138. mode = raw_input("What do you want to do?: ")
  139. if mode == "shop":
  140. Shop()
  141.  
  142.  
  143. while mode == "battle":
  144. fight = random.randrange(10)+1
  145. if fight == 1:
  146. monster = "Dragon!"
  147. elif fight == 2:
  148. monster = "Wild Cayote!"
  149. elif fight == 3:
  150. monster == "Jaguar!"
  151. elif fight == 4:
  152. monster = "Orc Lord!"
  153. elif fight == 5:
  154. monster = "Goblin!"
  155. elif fight == 6:
  156. monster = "Crazy Robot!"
  157. elif fight == 7:
  158. monster = "Werewolf!"
  159. elif fight == 8:
  160. monster = "Vampire!"
  161. elif fight == 9:
  162. monster = "Giant Spider!"
  163. elif fight == 10:
  164. monster = "Pirate King"
  165. turn = raw_input("Do you want to Attack or Defend?")
  166.  
  167. if"attack"in turn:
  168. print"You're attacking!"
  169. EnemyHP()
  170. HP()
  171. elif"defend"in turn:
  172. print"You're defending!"
  173. print"Monster's HP is",ehealth
  174. print"Your HP is",health
  175. else:
  176. print"Type attack or Defend!"
  177.  
  178. if health <= 0:
  179. print"You are dead!"
  180. mode = "restart"
  181. elif ehealth <= 0:
  182. print "You win the battle! You earn",m,"$!"
  183. money = money + m
  184. mode = "restart"
  185.  
  186.  
  187. if mode == "exit":
  188. exit = raw_input("Are you sure you want to quit? All progress will be lost. Yes or no: ")
  189. if"yes"in exit:
  190. break
  191. elif"no"in exit:
  192. mode = "restart"
  193.  
  194.  
  195. 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
Reputation Points: 52
Solved Threads: 0
Junior Poster in Training
Norbert X is offline Offline
87 posts
since Feb 2009
Mar 1st, 2009
0

Re: Python Game: Norbert Quest

Sweet if you google "NorbertQuest" you get this page. Awsome.
Reputation Points: 52
Solved Threads: 0
Junior Poster in Training
Norbert X is offline Offline
87 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Ejecting trays of CDs
Next Thread in Python Forum Timeline: Looping through and removing parts of a list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC