943,920 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1538
  • Python RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Jul 21st, 2009
0

Re: Variable Question maybe a function..

Yep, and there you have the start of it! Now you can just add the methods you want, like maybe a doAttack function,
python Syntax (Toggle Plain Text)
  1. from random import randint
  2.  
  3. class Character_1:
  4. def __init__(self,char):
  5. self.char = char
  6. self.gold = 0
  7. self.level = 1
  8. self.attack = 7
  9.  
  10. def doAttack(self):
  11. # get a damage amount ranging from half
  12. # the attack value, up to the attack value
  13. # note that 'minimum' is integer division, so it'll truncate,
  14. # i.e. 7 / 2 = 3 (it cuts off the decimal part)
  15. minimum = self.attack / 2
  16. maximum = self.attack
  17. damage = randint(minimum, maximum)
  18. return damage
Note that minimum is integer division, so it's essentially rounding down by cutting off the decimal point. And the randint function chooses a random integer in the range from minimum up to and including the maximum.

EDIT:
In response to you wanting to add 200 gold to the player upon initialization, then just change the self.gold = 0 inside __init__ to self.gold = 200 .
Last edited by shadwickman; Jul 21st, 2009 at 3:25 pm.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 21st, 2009
0

Re: Variable Question maybe a function..

Nice, that will come in handy, I will change that self.gold to 200, that way I can cut out the middle man.. I will work on this some more today and post some code up ..
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Clueless86 is offline Offline
76 posts
since Jul 2009
Jul 21st, 2009
0

Re: Variable Question maybe a function..

Ok, so here is what I have now. I basicly tied everything into Leveling up..So in order to get and do stuff you have to level up. You might know a better way, but I thought this would work well.. pay no mind to that variable 'x' that is just a place holder. And for now the 'Attack' function is just setting there..
Python Syntax (Toggle Plain Text)
  1. from random import randint
  2.  
  3.  
  4. class Character_1:
  5.  
  6. def __init__(self,char):
  7. self.char = char
  8. self.gold = 150
  9. self.level = 1
  10. self.attack = 4
  11. self.defend = 6
  12.  
  13. def Level_Up(self,x):
  14. player.level = player.level + 1
  15. player.gold = player.gold + 125
  16. player.attack = player.attack + 2
  17. player.defend = player.defend + 1
  18. if player.Level_Up >= 1:
  19. print 'Level:',
  20. print player.level
  21. print 'Gold:',
  22. print player.gold
  23. print 'Attack',
  24. print player.attack
  25. print 'Defend:',
  26. print player.defend
  27.  
  28.  
  29. def doAttack(self):
  30. player.attack
  31.  
  32.  
  33. player = Character_1('Serf')
  34.  
  35. player.level
  36. print 'Serf Character:'
  37. print 'Gold:',
  38. print player.gold
  39. print 'Level:',
  40. print player.level
  41. print 'Attack:',
  42. print player.attack
  43. print 'Defence:',
  44. print player.defend
Last edited by Clueless86; Jul 21st, 2009 at 5:54 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Clueless86 is offline Offline
76 posts
since Jul 2009
Jul 21st, 2009
0

Re: Variable Question maybe a function..

I see an error. Why do you have if player.Level_Up >= 1 ? Shouldn't that be if player.level >= 1 ? The value of player.Level_Up (which is the function) will be something like <bound method Character_1.Level_Up of <__main__.Character_1 object at 0x00D89110>>
And you can also simplify those additions you make by using the += operator. So instead of player.gold = player.gold + 125 you can write player.gold += 125 . Similar operators are:
-=
*=
/=
Last edited by shadwickman; Jul 21st, 2009 at 6:03 pm.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 21st, 2009
0

Re: Variable Question maybe a function..

I tried changing that to .level but when I do I get an error, when I use the Level_Up I put in 0 for x and get what I asked for..I dont know.. I may have something screwie going on, I will figure it out im sure.

I will change the operators to -= and += that will make things a little cleaner..Hey at least now I am asking good questions instead of stupid ones, or just having you help me make things cleaner or neater. yay im learning lol XD

EDIT: Well maybe I do have a stupid question..How would I build a menu option that pops up before the Class Character_1 pops up? I know how to build it, but what can I do do delay the Character class from poping up? I have a thought that is is the __init__ but don't I still need that?

What I am thinking is building that class for Character_1 and haveing a class Main_Menu that pops up first and lets you choose a character that will start the next class..
Last edited by Clueless86; Jul 21st, 2009 at 6:41 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Clueless86 is offline Offline
76 posts
since Jul 2009
Jul 21st, 2009
0

Re: Variable Question maybe a function..

Oh! How could I miss it! You're using things like player.gold and player.level , etc. That's wrong, because player is undefined within that scope. When you're changing a class' properties and such from within itself, you use self. Your Level_Up function should read:
python Syntax (Toggle Plain Text)
  1. def Level_Up(self, x):
  2. self.level += 1
  3. self.gold += 125
  4. self.attack += 2
  5. self.defend += 1
  6. if self.level >= 1:
  7. print 'Level:',
  8. print self.level
  9. print 'Gold:',
  10. print self.gold
  11. print 'Attack',
  12. print self.attack
  13. print 'Defend:',
  14. print self.defend
That's what has been messing it up
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 21st, 2009
0

Re: Variable Question maybe a function..

did you see the rest of my question? I noticed you posted about the time I was editing my post above^.

Also now when I change everything from player to self. I get the error 'self' is not defined when I call the function Level_Up

EDIT: never mind helps to change 'player = Character_1 to 'self'
major on me..

Now what about this question I had above?
EDIT: Well maybe I do have a stupid question..How would I build a menu option that pops up before the Class Character_1 pops up? I know how to build it, but what can I do do delay the Character class from poping up? I have a thought that is is the __init__ but don't I still need that?
Last edited by Clueless86; Jul 21st, 2009 at 8:44 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Clueless86 is offline Offline
76 posts
since Jul 2009
Jul 21st, 2009
1

Re: Variable Question maybe a function..

Remember that the script executes downwards; it doesn't all happen at once. Things like raw_input also stall until input is given, then the script continues. So this is fairly easy to do; here's an outline of what you'll want:
Python Syntax (Toggle Plain Text)
  1. class MainMenu(object):
  2. def __init__(self):
  3. # do your stuff here
  4. def askWhichClass(self):
  5. choice = raw_input("Which class? ")
  6. return choice
  7.  
  8. # define your player classes here...
  9.  
  10. mm = MainMenu()
  11. choice = mm.askWhichClass()
  12. if choice == 'serf':
  13. player = PlayerSerf()
  14. elif choice == 'baron':
  15. player = PlayerBaron()
  16. # etc...
This first initializes an instance MainMenu class (as mm). Then we call its method for getting the user's choice (via the raw_input statement) and then we check it with if statements to make player an instance of the appropriate class.
Hope this clears up your confusion!
Last edited by shadwickman; Jul 21st, 2009 at 9:34 pm.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 21st, 2009
0

Re: Variable Question maybe a function..

Perfect! If I was not so sure I would say you are a Python genius! Making my life alot easier I tend to second guess about stuff, even if I think it may work..
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Clueless86 is offline Offline
76 posts
since Jul 2009
Jul 21st, 2009
0

Re: Variable Question maybe a function..

Thanks! Just note that with the above code you'd still need to take into account incorrect input, like a user entering an invalid option for the class, etc. You could fix the MainMenu's getWhichClass method to handle this accordingly:
python Syntax (Toggle Plain Text)
  1. class MainMenu(object):
  2. def __init__(self):
  3. # do your stuff here
  4. def askWhichClass(self):
  5. # available options in a list
  6. options = ['serf', 'baron']
  7. while True:
  8. choice = raw_input("Which class? ")
  9. # check if the choice is in the options list
  10. # (convert to all lowercase first to allow for case-insensitive input)
  11.  
  12. # if so, return the choice (which also breaks the while loop)
  13. if choice.lower() in options:
  14. return choice
  15. # if not, don't break out of the loop,
  16. # so that it regathers input until it gets a valid choice
  17. else:
  18. print 'Invalid choice!'
Last edited by shadwickman; Jul 21st, 2009 at 9:42 pm.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007

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: Emailing problem... Please Help !
Next Thread in Python Forum Timeline: 2ND Console Window





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


Follow us on Twitter


© 2011 DaniWeb® LLC