User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,475 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,756 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 329 | Replies: 6
Reply
Join Date: Oct 2006
Location: Ohio
Posts: 55
Reputation: mruane is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mruane mruane is offline Offline
Junior Poster in Training

Battle Sequence help

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 229
Reputation: Freaky_Chris is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 25
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Posting Whiz in Training

Re: Battle Sequence help

  #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  
Join Date: Oct 2006
Location: Ohio
Posts: 55
Reputation: mruane is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mruane mruane is offline Offline
Junior Poster in Training

Re: Battle Sequence help

  #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:
# adding to christ o'leary's fight function
# to have integer returned use
# x = random.choice([0, 1])
# print x, type(x) # testing

# or x = random.randrange(0, 2)
# print x, type(x)
import random

global monster_HP1
monster_JP1 = 5
global hp
hp = 20

def main():
    global monster_HP1
    monster_HP1 = 5
    global hp
    hp = 10
    print """you are in a fight!
    Type 'h' to fight"""
    x = raw_input(': ')
    if x == 'h':
        fight_unicorn()
    else:
        print "Error"
        main()
        
def fight_unicorn():
    global monster_HP1
    global hp
    hit_miss = random.randrange(0, 2)
    # print x, type(x)
    if hit_miss == 1:
        print "You hit the monster!"
        monster_HP1 = monster_HP1 - 1
        if monster_HP1 == 0:
            print "You win!"
        else:
            monster_turn1()
    else:
        print "You Missed"
        monster_turn1()
def monster_turn1():
    global monster_HP1
    monster_HP1 = 5
    global hp
    hp = 20
    monster_hit = random.randrange(0, 2)
    # print x, type(x)
    if monster_hit == 1:
        print "You are hit!"
        hp = hp-1
        if hp == 0:
            print "Game Over"
    else:
        fight_unicorn() # Chris added this for me
        print "The monster missed"
        fight_unicorn()
main()
Reply With Quote  
Join Date: Jul 2008
Posts: 284
Reputation: Gribouillis is on a distinguished road 
Rep Power: 1
Solved Threads: 38
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Whiz in Training

Re: Battle Sequence help

  #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  
Join Date: Oct 2006
Location: Ohio
Posts: 55
Reputation: mruane is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mruane mruane is offline Offline
Junior Poster in Training

Re: Battle Sequence help

  #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  
Join Date: Oct 2006
Location: Ohio
Posts: 55
Reputation: mruane is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mruane mruane is offline Offline
Junior Poster in Training

Re: Battle Sequence help

  #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:

import random
import sys

global monster_hp1
monster_hp1 = 5
global hp
hp = 10

def init_game():
    print """Welcome to NayNay's Quest"""
    main()
def main():
    global monster_hp1, hp
    print """You are in a fight
    type 'h' to fight """
    x = raw_input(': ')
    if x == 'h':
        while True:
            if your_turn():
                break
            if monster_turn1():
                sys.exit(0)
        else:
            print "Error"

def your_turn():
    global monster_hp1, hp
    hit_miss = random.randrange(0, 2)
    if hit_miss == 1:
        print "You hit the monster!"
        monster_hp1= monster_hp1 = -1
        if monster_hp1 == 0:
            print "You win"
            return True
        else:
            print "You missed."
        return False

def monster_turn1():
    global monster_hp1, hp
    monster_hit= random.randrange(0, 2)
    if monster_hit == 1:
        hp = hp -1
        if hp == 0:
            print "The monster killed you!"
            print "game over"
            init_game()
            return True
        else:
            print "The monster missed"
        return False
    main()

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  
Join Date: Jul 2008
Posts: 284
Reputation: Gribouillis is on a distinguished road 
Rep Power: 1
Solved Threads: 38
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Whiz in Training

Re: Battle Sequence help

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 2:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC