Battle Sequence help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Battle Sequence help

 
0
  #1
Sep 28th, 2008
I have decided that a text adventure would be better for me to learn from, as a visual game is confusing to me still. So, I was reading Chris O'leary's post about his text adventure game, Advent House, and got many ideas from there. I want to perfect his battle sequence though.
Here is the code he used with minor adjustments on my part in the random module. If someone could tell me where I am going wrong and give me an example I would greatly appreciate it!
  1. # adding to christ o'leary's fight function
  2. # to have integer returned use
  3. # x = random.choice([0, 1])
  4. # print x, type(x) # testing
  5.  
  6. # or x = random.randrange(0, 2)
  7. # print x, type(x)
  8.  
  9. def main():
  10. global monster_HP1
  11. monster_HP1 = 5
  12. global hp
  13. hp = 10
  14. print """you are in a fight!
  15. Type 'h' to fight"""
  16. x = raw_input(': ')
  17. if x == 'h':
  18. fight_unicorn()
  19. else:
  20. print "Error"
  21. main()
  22.  
  23. def fight_unicorn():
  24. global monster_HP1
  25. global hp
  26. hit_miss = random.randrange(0, 2)
  27. print x, type(x)
  28. if hit_miss == 1:
  29. print "You hit the monster!"
  30. monster_HP1 = monster_HP1 - 1
  31. if monster_HP1 == 0:
  32. print "You win!"
  33. master_bedroom()
  34. else:
  35. monster_turn1()
  36. else:
  37. print "You Missed"
  38. monster_turn1()
  39. def monster_turn1():
  40. monster_hit = random.randrange(0, 2)
  41. print x, type(x)
  42. if monster_hit == 1:
  43. print "You are hit!"
  44. hp = hp-1
  45. if hp == 0:
  46. print "Game Over"
  47. else:
  48. print "The monster missed"
  49. fight_unicorn()
  50. main()
Last edited by cscgal; Sep 28th, 2008 at 1:28 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Battle Sequence help

 
0
  #2
Sep 28th, 2008
  1. # adding to christ o'leary's fight function
  2. # to have integer returned use
  3. # x = random.choice([0, 1])
  4. # print x, type(x) # testing
  5.  
  6. # or x = random.randrange(0, 2)
  7. # print x, type(x)
  8. import random
  9.  
  10. def main():
  11. global monster_HP1
  12. monster_HP1 = 5
  13. global hp
  14. hp = 10
  15. print """you are in a fight!
  16. Type 'h' to fight"""
  17. x = raw_input(': ')
  18. if x == 'h':
  19. fight_unicorn()
  20. else:
  21. print "Error"
  22. main()
  23.  
  24. def fight_unicorn():
  25. global monster_HP1
  26. global hp
  27. hit_miss = random.randrange(0, 2)
  28. #print x, type(x)
  29. if hit_miss == 1:
  30. print "You hit the monster!"
  31. monster_HP1 = monster_HP1 - 1
  32. if monster_HP1 == 0:
  33. print "You win!"
  34. master_bedroom()
  35. else:
  36. monster_turn1()
  37. else:
  38. print "You Missed"
  39. monster_turn1()
  40. def monster_turn1():
  41. monster_hit = random.randrange(0, 2)
  42. #print x, type(x)
  43. global hp
  44. if monster_hit == 1:
  45. print "You are hit!"
  46. hp = hp-1
  47. if hp == 0:
  48. print "Game Over"
  49. else:
  50. fight_unicorn()
  51. else:
  52. print "The monster missed"
  53. fight_unicorn() # ADD IN
  54. main()

i simply took out the print x, type(x) and added in a call back to fight unicorn

Chris
Last edited by Freaky_Chris; Sep 28th, 2008 at 2:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Re: Battle Sequence help

 
0
  #3
Sep 30th, 2008
Well, that solved one problem, now I get a loop of you hit the monster/you missed the monster/ the monster hit/missed you. It goes on for a while and the ends the program. I checked my function calls and couldn't see the problem. It is probably so easy it's staring me right in the face without me knowing it.
here is the code now:
  1. # adding to christ o'leary's fight function
  2. # to have integer returned use
  3. # x = random.choice([0, 1])
  4. # print x, type(x) # testing
  5.  
  6. # or x = random.randrange(0, 2)
  7. # print x, type(x)
  8. import random
  9.  
  10. global monster_HP1
  11. monster_JP1 = 5
  12. global hp
  13. hp = 20
  14.  
  15. def main():
  16. global monster_HP1
  17. monster_HP1 = 5
  18. global hp
  19. hp = 10
  20. print """you are in a fight!
  21. Type 'h' to fight"""
  22. x = raw_input(': ')
  23. if x == 'h':
  24. fight_unicorn()
  25. else:
  26. print "Error"
  27. main()
  28.  
  29. def fight_unicorn():
  30. global monster_HP1
  31. global hp
  32. hit_miss = random.randrange(0, 2)
  33. # print x, type(x)
  34. if hit_miss == 1:
  35. print "You hit the monster!"
  36. monster_HP1 = monster_HP1 - 1
  37. if monster_HP1 == 0:
  38. print "You win!"
  39. else:
  40. monster_turn1()
  41. else:
  42. print "You Missed"
  43. monster_turn1()
  44. def monster_turn1():
  45. global monster_HP1
  46. monster_HP1 = 5
  47. global hp
  48. hp = 20
  49. monster_hit = random.randrange(0, 2)
  50. # print x, type(x)
  51. if monster_hit == 1:
  52. print "You are hit!"
  53. hp = hp-1
  54. if hp == 0:
  55. print "Game Over"
  56. else:
  57. fight_unicorn() # Chris added this for me
  58. print "The monster missed"
  59. fight_unicorn()
  60. main()
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 960
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 220
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Battle Sequence help

 
0
  #4
Oct 1st, 2008
I think this would be better written in a non recursive way like this
  1. # adding to christ o'leary's fight function
  2.  
  3. import random
  4. import sys
  5.  
  6. global monster_HP1
  7. monster_HP1 = 5
  8. global hp
  9. hp = 7
  10.  
  11. def main():
  12. global monster_HP1, hp
  13. print """you are in a fight!
  14. Type 'h' to fight"""
  15. x = raw_input(': ')
  16. if x == 'h':
  17. while True:
  18. if your_turn():
  19. break
  20. if monster_turn1():
  21. sys.exit(0)
  22. else:
  23. print "Error"
  24.  
  25. def your_turn():
  26. global monster_HP1, hp
  27. hit_miss = random.randrange(0, 2)
  28. # print x, type(x)
  29. if hit_miss == 1:
  30. print "You hit the monster!"
  31. monster_HP1 = monster_HP1 - 1
  32. if monster_HP1 == 0:
  33. print "You win!"
  34. return True
  35. else:
  36. print "You Missed"
  37. return False
  38.  
  39. def monster_turn1():
  40. global monster_HP1, hp
  41. monster_hit = random.randrange(0, 2)
  42. # print x, type(x)
  43. if monster_hit == 1:
  44. hp = hp-1
  45. if hp == 0:
  46. print "The monster killed you!"
  47. print "Game Over"
  48. return True
  49. else:
  50. print "You are hit!"
  51. else:
  52. print "The monster missed"
  53. return False
  54.  
  55. main()
There are many problems in your code, for example you should not set the global variables in monster_turn1 . I also chose an initial value of hp=7 so that the monster has a chance to win
Last edited by Gribouillis; Oct 1st, 2008 at 4:36 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Re: Battle Sequence help

 
0
  #5
Oct 2nd, 2008
Okay, it still does the same thing, only this time the error log reads:
Traceback (most recent call last):
File "C:/Python25/howtofight.py", line 49, in <module>
main()
File "C:/Python25/howtofight.py", line 19, in main
sys.exit(0)
SystemExit: 0

This is how the battle is supposed to go...

1 press h to fight
user presses h
they hit or miss
monsters turn
monster hits or misses
your turn
user presses h to fight
repeat until one of you is dead.


Should I create a seperate function for when it is your turn again and you must press h? And, what is wrong with my code that it exits at the end of the program?
sys.exit is set to 0, so it shouldn't exit, right?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Re: Battle Sequence help

 
0
  #6
Oct 2nd, 2008
Okay, I fixed it a little bit. But it seems like I always lose. I set the global hp to 10, and it still lost. Is it not registering when the monster gets hit? Also, it seems like it is taking more that one turn at a time. Usually, it prints:
You hit the monster
you miss

then the monster take like five turns.
here is the new code:

  1. import random
  2. import sys
  3.  
  4. global monster_hp1
  5. monster_hp1 = 5
  6. global hp
  7. hp = 10
  8.  
  9. def init_game():
  10. print """Welcome to NayNay's Quest"""
  11. main()
  12. def main():
  13. global monster_hp1, hp
  14. print """You are in a fight
  15. type 'h' to fight """
  16. x = raw_input(': ')
  17. if x == 'h':
  18. while True:
  19. if your_turn():
  20. break
  21. if monster_turn1():
  22. sys.exit(0)
  23. else:
  24. print "Error"
  25.  
  26. def your_turn():
  27. global monster_hp1, hp
  28. hit_miss = random.randrange(0, 2)
  29. if hit_miss == 1:
  30. print "You hit the monster!"
  31. monster_hp1= monster_hp1 = -1
  32. if monster_hp1 == 0:
  33. print "You win"
  34. return True
  35. else:
  36. print "You missed."
  37. return False
  38.  
  39. def monster_turn1():
  40. global monster_hp1, hp
  41. monster_hit= random.randrange(0, 2)
  42. if monster_hit == 1:
  43. hp = hp -1
  44. if hp == 0:
  45. print "The monster killed you!"
  46. print "game over"
  47. init_game()
  48. return True
  49. else:
  50. print "The monster missed"
  51. return False
  52. main()
  53.  
  54. init_game()

Now what is wrong. Also, any suggestions on how to improve my code? I just want to get the battle sequence right before I use it in my game.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 960
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 220
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Battle Sequence help

 
0
  #7
Oct 2nd, 2008
Here is how you could cleanly code this with classes. The idea is to follow closely the sequence of events that you described above and to write small functions with a clear meaning
  1. from random import randrange
  2.  
  3. class InvalidChoice(Exception):
  4. pass
  5.  
  6. class QuestGame(object):
  7. def __init__(self):
  8. print "Welcome to NayNay's Quest"
  9. self.player_hp = 7
  10.  
  11. def run(self):
  12. fight = FightSequence(self)
  13. fight.run_forever()
  14.  
  15. def player_is_dead(self):
  16. return self.player_hp <= 0
  17.  
  18. class FightSequence(object):
  19. def __init__(self, game):
  20. self.game = game
  21. self.init_monster()
  22.  
  23. def init_monster(self):
  24. self.monster_hp = 5
  25.  
  26. def run_forever(self):
  27. while True:
  28. self.game.player_hp = 7
  29. self.init_monster()
  30. self.run()
  31.  
  32. def run(self):
  33. try:
  34. while True:
  35. self.prompt_user()
  36. self.player_turn()
  37. if self.monster_is_dead():
  38. print "You win!"
  39. break
  40. self.monster_turn()
  41. if self.game.player_is_dead():
  42. print "The monster killed you!"
  43. break
  44. except InvalidChoice:
  45. pass
  46.  
  47. def prompt_user(self):
  48. print """
  49. You are in a fight
  50. Type 'h' to fight""",
  51. choice = raw_input(': ')
  52. if choice != 'h':
  53. print "Error: invalid choice '%s'" % choice
  54. raise InvalidChoice
  55.  
  56. def player_turn(self):
  57. hit_miss = randrange(0,2)
  58. if hit_miss:
  59. print "You hit the monster!"
  60. self.monster_hp -= 1
  61. else:
  62. print "You missed."
  63.  
  64. def monster_turn(self):
  65. hit_miss = randrange(0,2)
  66. if hit_miss:
  67. print "The monster hit you!"
  68. self.game.player_hp -= 1
  69. else:
  70. print "The monster missed."
  71.  
  72. def monster_is_dead(self):
  73. return self.monster_hp <= 0
  74.  
  75. game = QuestGame()
  76. game.run()
Last edited by Gribouillis; Oct 2nd, 2008 at 6:04 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC