| | |
Text games are frustrating!
Thread Solved |
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:
Heres the code for the game:
Any ideas and help would be appreciated.
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:
python Syntax (Toggle Plain Text)
Traceback (most recent call last): File "/home/tom/Desktop/Python/Text game/main.py", line 109, in <module> main() File "/home/tom/Desktop/Python/Text game/main.py", line 105, in main print 'his attributes are:' + new_monster.getAttributesInfo() File "/home/tom/Desktop/Python/Text game/main.py", line 84, in getAttributesInfo Monster.getAttributesInfo() TypeError: unbound method getAttributesInfo() must be called with Monster instance as first argument (got nothing instead)
Heres the code for the game:
python Syntax (Toggle Plain Text)
#!/usr/bin/env python import random choice = 0 class weapon(object): def _init_(self, attack, defense): self.attack = self.attack defense = self.defense def getAttributesInfo(self): attributes = ['Attack:' + self.attack, 'Defense:' + self.defense] return ' '.join(attributes) class sword(weapon): def _init_(self): wepon._init_(self, random.randrange(1,3), random.randrange(1,3)) def getAttributesInfo(self): weapon.getAttributesInfo() class axe(weapon): def _init_(self): weapon._init_(self, random.randrange(1,5), random.randrange(1,5)) print 'This Axe\'s Attributes are:' print 'Attack:' + self.attack print 'Defense:' + self.defense def getAttributesInfo(self): weapon.getAttributesInfo() class player(): def _init_(self): self.health = 10 self.defense = 4 self.base_attack = 3 self.attacking = 1 def weapon(self): self.weapon = random.range(1,2) if self.weapon == 1: self.weapon = sword() print "you got a sword." elif self.weapon == 2: self.weapon = axe() print "you got an axe." def attack(Monster): if not self.attacking == 1: self.attacking = 1 if self.weapon.attack > monster.defense: hit = True monster.health = monster.health - self.weapon.damage print 'You hit him for ' + self.damage + 'damage!' for self.damage in axe() and sword(): if self.attack > troll.defense or orc.defense: self.damage = (player.attack +self.attack) - monster.defense elif self.attack < troll.defense or orc.defense: print 'You did\'nt do any damage...' class Monster(object): def __init__(self, attack, health, defense,): self.attack = attack self.health = health self.defense = defense def getAttributesInfo(): attributes = [ "Attack: " + self.attack, "Health: " +self.health, "Defense: " + self.defense] return " ".join(attributes) class orc(Monster): def _init_(self): Monster._init_(self, 4, 30, 5,) if player.attacking == 1: print "You are attacking an orc." if self.health <= 0: print 'You killed the Orc' def getAttributesInfo(self): Monster.getAttributesInfo() class troll(Monster): def _init_(self): Monster._init_(self, 7, 50, 7,) if player.attacking == 1: print "You are attacking a Troll." if self.health <= 0: print 'You killed the Troll' def getAttributesInfo(self): Monster.getAttributesInfo() def main(): monster_attacking = random.randrange(1,2) if monster_attacking == 1: new_monster = orc(4, 30, 5) elif monster_attacking == 2: new_monster = troll(7, 50, 7) choice = raw_input('Do you want to attack:') if choice == 'yes' or 'yer' or ' yes' or ' yer': print 'his attributes are:' + new_monster.getAttributesInfo() attack() if __name__ == '__main__': main()
Any ideas and help would be appreciated.
...
In your code you go:
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.
python Syntax (Toggle Plain Text)
Monster.getAttributesInfo() #and weapon.getAttributesInfo()
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
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
I fiddled with the code and here is what i got:
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:
python Syntax (Toggle Plain Text)
#!/usr/bin/env python import random choice = 0 class weapon(object): def _init_(self, attack, defense): self.attack = self.attack defense = self.defense def getAttributesInfo(self): attributes = ['Attack:' + self.attack, 'Defense:' + self.defense] return ' '.join(attributes) class sword(weapon): def _init_(self): wepon._init_(self, random.randrange(1,3), random.randrange(1,3)) def getAttributesInfo(self): weapon.getAttributesInfo() class axe(weapon): def _init_(self): weapon._init_(self, random.randrange(1,5), random.randrange(1,5)) print 'This Axe\'s Attributes are:' print 'Attack:' + self.attack print 'Defense:' + self.defense def getAttributesInfo(self): weapon.getAttributesInfo() class player(): def _init_(self): self.health = 10 self.defense = 4 self.base_attack = 3 self.attacking = 1 def weapon(self): self.weapon = random.range(1,2) if self.weapon == 1: self.weapon = sword() print "you got a sword." elif self.weapon == 2: self.weapon = axe() print "you got an axe." def attack(Monster): if not self.attacking == 1: self.attacking = 1 if self.weapon.attack > monster.defense: hit = True monster.health = monster.health - self.weapon.damage print 'You hit him for ' + self.damage + 'damage!' for self.damage in axe() and sword(): if self.attack > troll.defense or orc.defense: self.damage = (player.attack +self.attack) - monster.defense elif self.attack < troll.defense or orc.defense: print 'You did\'nt do any damage...' class Monster(object): def __init__(self, attack, health, defense,): self.attack = attack self.health = health self.defense = defense def getAttributesInfo(self): attributes = [ "Attack: "+ str( self.attack), "Health: "+str(self.health), "Defense: "+str(self.defense)] return " ".join(attributes) class orc(Monster): def _init_(self): Monster._init_(self, 4, 30, 5,) if player.attacking == 1: print "You are attacking an orc." if self.health <= 0: print 'You killed the Orc' def getInfo(self): self.getAttributesInfo() class troll(Monster): def _init_(self): Monster._init_(self, 7, 50, 7,) if player.attacking == 1: print "You are attacking a Troll." if self.health <= 0: print 'You killed the Troll' def getInfo(self): self.getAttributesInfo() def main(): monster_attacking = random.randrange(1,2) if monster_attacking == 1: new_monster = orc(4, 30, 5) elif monster_attacking == 2: new_monster = troll(7, 50, 7) choice = raw_input('Do you want to attack:') if choice == 'yes' or 'yer' or ' yes' or ' yer': print "FFFF" print 'his attributes are:' , new_monster.getInfo() attack() if __name__ == '__main__': 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
Check out my Site | and join us on IRC | Python Specific IRC
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.
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.
An alternative to changing the method's name is this
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.
python Syntax (Toggle Plain Text)
class Troll(Monster): def getAttributesInfo(self): Monster.getAttributesInfo(self)
•
•
•
•
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 callself.getAttributesInfoBut the problem with that is there are two methods calledself.getAttibutesInfoOne 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:
python Syntax (Toggle Plain Text)
#!/usr/bin/env python import random choice = 0 class weapon(object): def _init_(self, attack, defense): self.attack = self.attack defense = self.defense def getAttributesInfo(self): attributes = ['Attack:' + self.attack, 'Defense:' + self.defense] return ' '.join(attributes) class sword(weapon): def _init_(self): wepon._init_(self, random.randrange(1,3), random.randrange(1,3)) def getAttributesInfo(self): weapon.getAttributesInfo() class axe(weapon): def _init_(self): weapon._init_(self, random.randrange(1,5), random.randrange(1,5)) print 'This Axe\'s Attributes are:' print 'Attack:' + self.attack print 'Defense:' + self.defense def getAttributesInfo(self): weapon.getAttributesInfo() class player(): def _init_(self): self.health = 10 self.defense = 4 self.base_attack = 3 self.attacking = 1 def weapon(self): self.weapon = random.range(1,2) if self.weapon == 1: self.weapon = sword() print "you got a sword." elif self.weapon == 2: self.weapon = axe() print "you got an axe." def attack(Monster): if not self.attacking == 1: self.attacking = 1 if self.weapon.attack > monster.defense: hit = True monster.health = monster.health - self.weapon.damage print 'You hit him for ' + self.damage + 'damage!' for self.damage in axe() and sword(): if self.attack > troll.defense or orc.defense: self.damage = (player.attack +self.attack) - monster.defense elif self.attack < troll.defense or orc.defense: print 'You did\'nt do any damage...' class Monster(object): def __init__(self, attack, health, defense,): self.attack = attack self.health = health self.defense = defense def getAttributesInfo(self): attributes = [ "Attack: "+ str( self.attack), "Health: "+str(self.health), "Defense: "+str(self.defense)] return " ".join(attributes) class orc(Monster): def _init_(self): Monster._init_(self, 4, 30, 5,) if player.attacking == 1: print "You are attacking an orc." if self.health <= 0: print 'You killed the Orc' def getInfo(self): self.getAttributesInfo() class troll(Monster): def _init_(self): Monster._init_(self, 7, 50, 7,) if player.attacking == 1: print "You are attacking a Troll." if self.health <= 0: print 'You killed the Troll' def getInfo(self): self.getAttributesInfo() def main(): monster_attacking = random.randrange(1,2) if monster_attacking == 1: new_monster = orc(4, 30, 5) elif monster_attacking == 2: new_monster = troll(7, 50, 7) choice = raw_input('Do you want to attack:') if choice == 'yes' or 'yer' or ' yes' or ' yer': print "FFFF" print 'his attributes are:' , new_monster.getInfo() attack() if __name__ == '__main__': 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:
python Syntax (Toggle Plain Text)
Traceback (most recent call last): File "/home/tom/Desktop/Python/Text game/main.py", line 112, in <module> main() File "/home/tom/Desktop/Python/Text game/main.py", line 109, in main player.attack() TypeError: unbound method attack() must be called with player instance as first argument (got nothing instead)
Any ideas?
...
Again you need to create an instance of player so somewhere put this:
hope that helps.
python Syntax (Toggle Plain Text)
p = player() #then when you want to attack p.attack()
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
Thanks again, but now i get this error:
any more ideas?
python Syntax (Toggle Plain Text)
Traceback (most recent call last): File "/home/tom/Desktop/Python/Text game/main.py", line 115, in <module> main() File "/home/tom/Desktop/Python/Text game/main.py", line 112, in main new_player.attack() File "/home/tom/Desktop/Python/Text game/main.py", line 55, in attack if self.weapon.attack > monster.defense: AttributeError: 'function' object has no attribute 'attack'
any more ideas?
...
That is because when you go
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.
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
Check out my Site | and join us on IRC | Python Specific IRC
![]() |
Other Threads in the Python Forum
- Previous Thread: create interface with python
- Next Thread: python script works in pywin but not in command window
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






