| | |
Variable Question maybe a function..
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Yep, and there you have the start of it! Now you can just add the methods you want, like maybe a doAttack function,
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
python Syntax (Toggle Plain Text)
from random import randint class Character_1: def __init__(self,char): self.char = char self.gold = 0 self.level = 1 self.attack = 7 def doAttack(self): # get a damage amount ranging from half # the attack value, up to the attack value # note that 'minimum' is integer division, so it'll truncate, # i.e. 7 / 2 = 3 (it cuts off the decimal part) minimum = self.attack / 2 maximum = self.attack damage = randint(minimum, maximum) return damage
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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
•
•
Join Date: Jul 2009
Posts: 76
Reputation:
Solved Threads: 1
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)
from random import randint class Character_1: def __init__(self,char): self.char = char self.gold = 150 self.level = 1 self.attack = 4 self.defend = 6 def Level_Up(self,x): player.level = player.level + 1 player.gold = player.gold + 125 player.attack = player.attack + 2 player.defend = player.defend + 1 if player.Level_Up >= 1: print 'Level:', print player.level print 'Gold:', print player.gold print 'Attack', print player.attack print 'Defend:', print player.defend def doAttack(self): player.attack player = Character_1('Serf') player.level print 'Serf Character:' print 'Gold:', print player.gold print 'Level:', print player.level print 'Attack:', print player.attack print 'Defence:', print player.defend
Last edited by Clueless86; Jul 21st, 2009 at 5:54 pm.
I see an error. Why do you have
And you can also simplify those additions you make by using the += operator. So instead of
-=
*=
/=
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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
•
•
Join Date: Jul 2009
Posts: 76
Reputation:
Solved Threads: 1
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..
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.
Oh! How could I miss it! You're using things like
That's what has been messing it up
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)
def Level_Up(self, x): self.level += 1 self.gold += 125 self.attack += 2 self.defend += 1 if self.level >= 1: print 'Level:', print self.level print 'Gold:', print self.gold print 'Attack', print self.attack print 'Defend:', print self.defend
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
•
•
Join Date: Jul 2009
Posts: 76
Reputation:
Solved Threads: 1
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?
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.
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:
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!
Python Syntax (Toggle Plain Text)
class MainMenu(object): def __init__(self): # do your stuff here def askWhichClass(self): choice = raw_input("Which class? ") return choice # define your player classes here... mm = MainMenu() choice = mm.askWhichClass() if choice == 'serf': player = PlayerSerf() elif choice == 'baron': player = PlayerBaron() # etc...
Hope this clears up your confusion!
Last edited by shadwickman; Jul 21st, 2009 at 9:34 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
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)
class MainMenu(object): def __init__(self): # do your stuff here def askWhichClass(self): # available options in a list options = ['serf', 'baron'] while True: choice = raw_input("Which class? ") # check if the choice is in the options list # (convert to all lowercase first to allow for case-insensitive input) # if so, return the choice (which also breaks the while loop) if choice.lower() in options: return choice # if not, don't break out of the loop, # so that it regathers input until it gets a valid choice else: print 'Invalid choice!'
Last edited by shadwickman; Jul 21st, 2009 at 9:42 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
![]() |
Similar Threads
- using variable passed to function as enw variable name (PHP)
- Totaling up a variable in repeating function (Python)
- passing directory name as a variable in include function (PHP)
- regarding variable arguments (C++)
- Question: Which is the best function to count the running time an algorithm? (Java)
- Pass a javascript variable to PHP in the same function (JavaScript / DHTML / AJAX)
- variable arguments in C (C)
- Bad Code question (C++)
- Problem with string variable in void prnt function (C++)
Other Threads in the Python Forum
- Previous Thread: Emailing problem... Please Help !
- Next Thread: 2ND Console Window
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory drive dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo verify webservice wikipedia windows write wxpython xlib





