Text games are frustrating!

Thread Solved

Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Text games are frustrating!

 
0
  #1
Nov 4th, 2008
Hi all,
Im making a test text game and i got this error when i was trying to run it, after i had told it i wanted to attack:

  1. Traceback (most recent call last):
  2. File "/home/tom/Desktop/Python/Text game/main.py", line 109, in <module>
  3. main()
  4. File "/home/tom/Desktop/Python/Text game/main.py", line 105, in main
  5. print 'his attributes are:' + new_monster.getAttributesInfo()
  6. File "/home/tom/Desktop/Python/Text game/main.py", line 84, in getAttributesInfo
  7. Monster.getAttributesInfo()
  8. TypeError: unbound method getAttributesInfo() must be called with Monster instance as first argument (got nothing instead)

Heres the code for the game:

  1. #!/usr/bin/env python
  2.  
  3. import random
  4.  
  5. choice = 0
  6.  
  7. class weapon(object):
  8. def _init_(self, attack, defense):
  9. self.attack = self.attack
  10. defense = self.defense
  11.  
  12. def getAttributesInfo(self):
  13. attributes = ['Attack:' + self.attack,
  14. 'Defense:' + self.defense]
  15. return ' '.join(attributes)
  16.  
  17. class sword(weapon):
  18. def _init_(self):
  19. wepon._init_(self, random.randrange(1,3), random.randrange(1,3))
  20.  
  21. def getAttributesInfo(self):
  22. weapon.getAttributesInfo()
  23.  
  24. class axe(weapon):
  25. def _init_(self):
  26. weapon._init_(self, random.randrange(1,5), random.randrange(1,5))
  27. print 'This Axe\'s Attributes are:'
  28. print 'Attack:' + self.attack
  29. print 'Defense:' + self.defense
  30.  
  31. def getAttributesInfo(self):
  32. weapon.getAttributesInfo()
  33.  
  34. class player():
  35. def _init_(self):
  36. self.health = 10
  37. self.defense = 4
  38. self.base_attack = 3
  39. self.attacking = 1
  40.  
  41. def weapon(self):
  42. self.weapon = random.range(1,2)
  43. if self.weapon == 1:
  44. self.weapon = sword()
  45. print "you got a sword."
  46. elif self.weapon == 2:
  47. self.weapon = axe()
  48. print "you got an axe."
  49.  
  50. def attack(Monster):
  51. if not self.attacking == 1:
  52. self.attacking = 1
  53. if self.weapon.attack > monster.defense:
  54. hit = True
  55. monster.health = monster.health - self.weapon.damage
  56. print 'You hit him for ' + self.damage + 'damage!'
  57. for self.damage in axe() and sword():
  58. if self.attack > troll.defense or orc.defense:
  59. self.damage = (player.attack +self.attack) - monster.defense
  60. elif self.attack < troll.defense or orc.defense:
  61. print 'You did\'nt do any damage...'
  62.  
  63.  
  64. class Monster(object):
  65. def __init__(self, attack, health, defense,):
  66. self.attack = attack
  67. self.health = health
  68. self.defense = defense
  69. def getAttributesInfo():
  70. attributes = [ "Attack: " + self.attack,
  71. "Health: " +self.health,
  72. "Defense: " + self.defense]
  73. return " ".join(attributes)
  74.  
  75. class orc(Monster):
  76. def _init_(self):
  77. Monster._init_(self, 4, 30, 5,)
  78. if player.attacking == 1:
  79. print "You are attacking an orc."
  80. if self.health <= 0:
  81. print 'You killed the Orc'
  82.  
  83. def getAttributesInfo(self):
  84. Monster.getAttributesInfo()
  85.  
  86. class troll(Monster):
  87. def _init_(self):
  88. Monster._init_(self, 7, 50, 7,)
  89. if player.attacking == 1:
  90. print "You are attacking a Troll."
  91. if self.health <= 0:
  92. print 'You killed the Troll'
  93.  
  94. def getAttributesInfo(self):
  95. Monster.getAttributesInfo()
  96.  
  97. def main():
  98. monster_attacking = random.randrange(1,2)
  99. if monster_attacking == 1:
  100. new_monster = orc(4, 30, 5)
  101. elif monster_attacking == 2:
  102. new_monster = troll(7, 50, 7)
  103. choice = raw_input('Do you want to attack:')
  104. if choice == 'yes' or 'yer' or ' yes' or ' yer':
  105. print 'his attributes are:' + new_monster.getAttributesInfo()
  106. attack()
  107.  
  108. if __name__ == '__main__':
  109. main()

Any ideas and help would be appreciated.
...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 902
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is online now Online
previously paulthom12345

Re: Text games are frustrating!

 
0
  #2
Nov 4th, 2008
In your code you go:
  1. Monster.getAttributesInfo()
  2. #and
  3. weapon.getAttributesInfo()
The problem with this is that you are not instanciating your class. First creat an instance of the class and then you can call methods then. Otherwise you will keep getting this error.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 902
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is online now Online
previously paulthom12345

Re: Text games are frustrating!

 
0
  #3
Nov 4th, 2008
Sorry wait, that only solves half of the problem. The thing i only just spotted was that you go Monster.__init__. That then means that all of Monsters methods will now be stored in self. So you can call self.getAttributesInfo But the problem with that is there are two methods called self.getAttibutesInfo One is in the Orc and Troll classes and the other is in the Monster class so then we have to change the name of one of them to make sure that the program does not call the method over and over and over.

I fiddled with the code and here is what i got:
  1. #!/usr/bin/env python
  2.  
  3. import random
  4.  
  5. choice = 0
  6.  
  7. class weapon(object):
  8. def _init_(self, attack, defense):
  9. self.attack = self.attack
  10. defense = self.defense
  11.  
  12. def getAttributesInfo(self):
  13. attributes = ['Attack:' + self.attack,
  14. 'Defense:' + self.defense]
  15. return ' '.join(attributes)
  16.  
  17. class sword(weapon):
  18. def _init_(self):
  19. wepon._init_(self, random.randrange(1,3), random.randrange(1,3))
  20.  
  21. def getAttributesInfo(self):
  22. weapon.getAttributesInfo()
  23.  
  24. class axe(weapon):
  25. def _init_(self):
  26. weapon._init_(self, random.randrange(1,5), random.randrange(1,5))
  27. print 'This Axe\'s Attributes are:'
  28. print 'Attack:' + self.attack
  29. print 'Defense:' + self.defense
  30.  
  31. def getAttributesInfo(self):
  32. weapon.getAttributesInfo()
  33.  
  34. class player():
  35. def _init_(self):
  36. self.health = 10
  37. self.defense = 4
  38. self.base_attack = 3
  39. self.attacking = 1
  40.  
  41. def weapon(self):
  42. self.weapon = random.range(1,2)
  43. if self.weapon == 1:
  44. self.weapon = sword()
  45. print "you got a sword."
  46. elif self.weapon == 2:
  47. self.weapon = axe()
  48. print "you got an axe."
  49.  
  50. def attack(Monster):
  51. if not self.attacking == 1:
  52. self.attacking = 1
  53. if self.weapon.attack > monster.defense:
  54. hit = True
  55. monster.health = monster.health - self.weapon.damage
  56. print 'You hit him for ' + self.damage + 'damage!'
  57. for self.damage in axe() and sword():
  58. if self.attack > troll.defense or orc.defense:
  59. self.damage = (player.attack +self.attack) - monster.defense
  60. elif self.attack < troll.defense or orc.defense:
  61. print 'You did\'nt do any damage...'
  62.  
  63.  
  64. class Monster(object):
  65. def __init__(self, attack, health, defense,):
  66. self.attack = attack
  67. self.health = health
  68. self.defense = defense
  69. def getAttributesInfo(self):
  70. attributes = [ "Attack: "+ str( self.attack),
  71. "Health: "+str(self.health),
  72. "Defense: "+str(self.defense)]
  73. return " ".join(attributes)
  74.  
  75. class orc(Monster):
  76. def _init_(self):
  77. Monster._init_(self, 4, 30, 5,)
  78. if player.attacking == 1:
  79. print "You are attacking an orc."
  80. if self.health <= 0:
  81. print 'You killed the Orc'
  82.  
  83. def getInfo(self):
  84.  
  85. self.getAttributesInfo()
  86.  
  87. class troll(Monster):
  88. def _init_(self):
  89. Monster._init_(self, 7, 50, 7,)
  90. if player.attacking == 1:
  91. print "You are attacking a Troll."
  92. if self.health <= 0:
  93. print 'You killed the Troll'
  94.  
  95. def getInfo(self):
  96.  
  97. self.getAttributesInfo()
  98.  
  99. def main():
  100. monster_attacking = random.randrange(1,2)
  101. if monster_attacking == 1:
  102. new_monster = orc(4, 30, 5)
  103. elif monster_attacking == 2:
  104. new_monster = troll(7, 50, 7)
  105. choice = raw_input('Do you want to attack:')
  106. if choice == 'yes' or 'yer' or ' yes' or ' yer':
  107. print "FFFF"
  108. print 'his attributes are:' , new_monster.getInfo()
  109. attack()
  110.  
  111. if __name__ == '__main__':
  112. main()
Last edited by Paul Thompson; Nov 4th, 2008 at 6:51 am.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,276
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Text games are frustrating!

 
0
  #4
Nov 4th, 2008
Some very basic errors there. You failed to declare the instance of your class. One recommended way to avoid that is to start your class names with a capital letter and your instance name with lower case.

As your game gets larger, you may want to interest yourself in a thing called debugging.
Last edited by sneekula; Nov 4th, 2008 at 9:49 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 909
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 214
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Text games are frustrating!

 
0
  #5
Nov 4th, 2008
An alternative to changing the method's name is this
  1. class Troll(Monster):
  2. def getAttributesInfo(self):
  3. Monster.getAttributesInfo(self)
It's a standard way of overloading a method in python: the method in the derived class calls the method with the same name in the parent's class. This allows you to define troll-specific behaviour in the derived class method.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Text games are frustrating!

 
0
  #6
Nov 5th, 2008
Originally Posted by paulthom12345 View Post
Sorry wait, that only solves half of the problem. The thing i only just spotted was that you go Monster.__init__. That then means that all of Monsters methods will now be stored in self. So you can call self.getAttributesInfo But the problem with that is there are two methods called self.getAttibutesInfo One is in the Orc and Troll classes and the other is in the Monster class so then we have to change the name of one of them to make sure that the program does not call the method over and over and over.

I fiddled with the code and here is what i got:
  1. #!/usr/bin/env python
  2.  
  3. import random
  4.  
  5. choice = 0
  6.  
  7. class weapon(object):
  8. def _init_(self, attack, defense):
  9. self.attack = self.attack
  10. defense = self.defense
  11.  
  12. def getAttributesInfo(self):
  13. attributes = ['Attack:' + self.attack,
  14. 'Defense:' + self.defense]
  15. return ' '.join(attributes)
  16.  
  17. class sword(weapon):
  18. def _init_(self):
  19. wepon._init_(self, random.randrange(1,3), random.randrange(1,3))
  20.  
  21. def getAttributesInfo(self):
  22. weapon.getAttributesInfo()
  23.  
  24. class axe(weapon):
  25. def _init_(self):
  26. weapon._init_(self, random.randrange(1,5), random.randrange(1,5))
  27. print 'This Axe\'s Attributes are:'
  28. print 'Attack:' + self.attack
  29. print 'Defense:' + self.defense
  30.  
  31. def getAttributesInfo(self):
  32. weapon.getAttributesInfo()
  33.  
  34. class player():
  35. def _init_(self):
  36. self.health = 10
  37. self.defense = 4
  38. self.base_attack = 3
  39. self.attacking = 1
  40.  
  41. def weapon(self):
  42. self.weapon = random.range(1,2)
  43. if self.weapon == 1:
  44. self.weapon = sword()
  45. print "you got a sword."
  46. elif self.weapon == 2:
  47. self.weapon = axe()
  48. print "you got an axe."
  49.  
  50. def attack(Monster):
  51. if not self.attacking == 1:
  52. self.attacking = 1
  53. if self.weapon.attack > monster.defense:
  54. hit = True
  55. monster.health = monster.health - self.weapon.damage
  56. print 'You hit him for ' + self.damage + 'damage!'
  57. for self.damage in axe() and sword():
  58. if self.attack > troll.defense or orc.defense:
  59. self.damage = (player.attack +self.attack) - monster.defense
  60. elif self.attack < troll.defense or orc.defense:
  61. print 'You did\'nt do any damage...'
  62.  
  63.  
  64. class Monster(object):
  65. def __init__(self, attack, health, defense,):
  66. self.attack = attack
  67. self.health = health
  68. self.defense = defense
  69. def getAttributesInfo(self):
  70. attributes = [ "Attack: "+ str( self.attack),
  71. "Health: "+str(self.health),
  72. "Defense: "+str(self.defense)]
  73. return " ".join(attributes)
  74.  
  75. class orc(Monster):
  76. def _init_(self):
  77. Monster._init_(self, 4, 30, 5,)
  78. if player.attacking == 1:
  79. print "You are attacking an orc."
  80. if self.health <= 0:
  81. print 'You killed the Orc'
  82.  
  83. def getInfo(self):
  84.  
  85. self.getAttributesInfo()
  86.  
  87. class troll(Monster):
  88. def _init_(self):
  89. Monster._init_(self, 7, 50, 7,)
  90. if player.attacking == 1:
  91. print "You are attacking a Troll."
  92. if self.health <= 0:
  93. print 'You killed the Troll'
  94.  
  95. def getInfo(self):
  96.  
  97. self.getAttributesInfo()
  98.  
  99. def main():
  100. monster_attacking = random.randrange(1,2)
  101. if monster_attacking == 1:
  102. new_monster = orc(4, 30, 5)
  103. elif monster_attacking == 2:
  104. new_monster = troll(7, 50, 7)
  105. choice = raw_input('Do you want to attack:')
  106. if choice == 'yes' or 'yer' or ' yes' or ' yer':
  107. print "FFFF"
  108. print 'his attributes are:' , new_monster.getInfo()
  109. attack()
  110.  
  111. if __name__ == '__main__':
  112. main()

Thanks for all that, i only changed the attack() function in main() to be player.attack(), which solved another error that i got but after that i got this error:

  1. Traceback (most recent call last):
  2. File "/home/tom/Desktop/Python/Text game/main.py", line 112, in <module>
  3. main()
  4. File "/home/tom/Desktop/Python/Text game/main.py", line 109, in main
  5. player.attack()
  6. TypeError: unbound method attack() must be called with player instance as first argument (got nothing instead)

Any ideas?
...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 902
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is online now Online
previously paulthom12345

Re: Text games are frustrating!

 
0
  #7
Nov 5th, 2008
Again you need to create an instance of player so somewhere put this:
  1. p = player()
  2. #then when you want to attack
  3. p.attack()
hope that helps.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Text games are frustrating!

 
0
  #8
Nov 5th, 2008
Thanks again, but now i get this error:

  1. Traceback (most recent call last):
  2. File "/home/tom/Desktop/Python/Text game/main.py", line 115, in <module>
  3. main()
  4. File "/home/tom/Desktop/Python/Text game/main.py", line 112, in main
  5. new_player.attack()
  6. File "/home/tom/Desktop/Python/Text game/main.py", line 55, in attack
  7. if self.weapon.attack > monster.defense:
  8. AttributeError: 'function' object has no attribute 'attack'

any more ideas?
...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 902
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is online now Online
previously paulthom12345

Re: Text games are frustrating!

 
1
  #9
Nov 5th, 2008
That is because when you go self.weapon.attack it logically goes, well self.weapon is a function in the class and then it looks for the variable attack as part of the method weapon. This is a problem and you should change this.

I noticed a lot of the things that you are using in your code have not actually been declared. Make sure to declare all of your variables.
Last edited by Paul Thompson; Nov 5th, 2008 at 1:56 am.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 379
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Text games are frustrating!

 
0
  #10
Nov 5th, 2008
How do you think that i would do that?
...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC