I need some help debugging this game. I've tried everything I know, but nothing works. I use Python 3.1.4. Thanks guys!
import random
#Setting global variables and such items
loop = 1
level = 1
win = 0
move = ''' '''
#Setting variables and such items for Bulbasaur (player):
hp = 200
armr = 10
sparmr = 10
atk = 5
spatk = 10
speed = 0
pp1 = 20
pp2 = 40
pp3 = 10
pp4 = 15
gold = 0
#Setting variables and such items for Dark Bulbasaur (enemy):
ehp = (190 + level)
earmr = 10
esparmr = 5
eatk = 10
espatk = 10
espeed = 0
def setVar():
#Setting global variables and such items:
loop = 1
level = 1
win = 0
move = ''' '''
#Setting variables and such items for Bulbasaur (player):
hp = 200
armr = 10
sparmr = 10
atk = 5
spatk = 10
speed = 0
pp1 = 20
pp2 = 40
pp3 = 10
pp4 = 15
gold = 0
#Setting variables and such items for Dark Bulbasaur (enemy):
ehp = (190 + level)
earmr = 10
esparmr = 5
eatk = 10
espatk = 10
espeed = 0
global speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
#The function that runs the game:
def playerTurn():
global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
move = input('''What will Bulbasaur do?\n\n>>> ''')
move = move.lower()
playerMove(move)
#The function that processes the player's chosen move:
def playerMove(move):
global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
if move == '''tackle''':
if pp1 > 0:
print('''Bulbasaur has used Tackle!''')
atk = random.randint(0, 100)
if atk < (90 + speed):
ehp -= (20 + atk - earmr)
print('''Dark Bulbasaur was hurt!\n''')
else:
print('''Bulbasaur missed!''')
pp1 -= 1
else:
print('''Bulbasaur does not have enough PP to perform Tackle.\n''')
playerTurn()
elif move == '''growl''':
if pp2 > 0:
print('''Bulbasaur has used Growl!''')
atk = random.randint(0, 100)
if atk < (100 + speed):
eatk -= 5
print('''Dark Bulbasaur was weakened\n''')
else:
print('''Bulbasaur missed!\n''')
pp2 -= 1
else:
print('''Bulbasaur does not have enough PP to perform Growl.\n''')
playerTurn()
elif move == '''leech seed''':
if pp3 > 0:
print('''Bulbasaur used Leech Seed!''')
atk = random.randint(0, 100)
if atk > (90 + speed):
ehp -= (10 + spatk - esparmr)
hp += (10 + spatk - esparmr)
print('''Dark Bulbasaur was drained!\n''')
else:
print('''Bulbasaur missed!\n''')
pp3 -= 1
else:
print('''Bulbasaur does not have enough PP to perform Leech Seed.\n''')
playerTurn()
elif move == '''vine whip''':
if pp4 > 0:
print('''Bulbasaur used Vine Whip!''')
atk = random.randint(0, 100)
if atk > (100 + speed):
ehp -= (20 + spatk - esparmr)
print('''Dark Bulbasaur was hurt!\n''')
else:
print('''Bulbasaur missed!\n''')
else:
print('''Bulbasaur does not have enough PP to perform Vine Whip.\n''')
playerTurn()
pp4 -= 1
else:
print('''That is not a recognized move. Please chose from the following moves:\n\t
Tackle\n\t
Growl\n\t
Leech Seed\n\t
Vine Whip\n
If these commands do not work, check the spelling.\n''')
playerTurn()
#The function that controls the Dark Bulbasaur's move:
def enemyMove():
global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
atk = random.randint(0, 100)
if atk <= 25:
print('''Dark Bulbasaur used Tackle!''')
atk = random.randint(0, 100)
if atk <= (20 + espeed):
hp -= (35 + eatk - armr)
print('''Bulbasaur was hurt!\n''')
else:
print('''Dark Bulbasaur missed!\n''')
elif atk > 25 and atk <= 50:
print('''Dark Bulbasaur used Growl!''')
atk = random.randint(0, 100)
if atk <= (100 + espeed):
atk -= 5
print('''Bulbasaur was weakened!\n''')
else:
print('''Dark Bulbasaur missed!\n''')
elif atk > 50 and atk <= 75:
print('''Dark Bulbasaur used Leech Seed!''')
atk = random.randint(0, 100)
if atk <= (90 + espeed):
hp -= (10 + espatk - sparmr)
print('''Bulbasaur was drained!\n''')
ehp += (10 + espatk)
else:
print('''Dark Bulbasaur missed!\n''')
elif atk > 75:
print('''Dark Bulbasaur used Vine Whip!''')
atk = random.randint(0, 100)
if atk <= (100 + speed):
hp -= (20 + espatk - sparmr)
print('''Bulbasaur was hurt!\n''')
else:
print('''Dark Bulbasaur missed!\n''')
else:
print('''There is a glitch in the program. Please report the event to *name removed*.''')
loop = 0
#The function that reports the HP and PP of the Bulbasaur and Dark Bulbasaur:
def reportStats():
global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
print('''Bulbasaur has ''', hp, ''' HP!\n\nBulbasaur has ''', pp1, ''' PP for Tackle!\nBulbasaur has ''', pp2, ''' for PP for Growl!\nBulbasaur has ''', pp3, ''' PP for Leech Seed!\nBulbasaur has ''', pp4, ''' PP for Vine Whip!\n''')
print('''Dark Bulbasaur has ''', ehp, ''' HP!\n''')
#The function that checks to see whether someone is KO'ed:
def reportDeath():
global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
if hp <= 0:
print('''Bulbasaur has been KO'ed!''')
loop = 0
elif ehp <= 0:
print('''Dark Bulbasaur has been KO'ed!''')
atk = random.randint(0, 11)
gold += atk
win = 1
#The function that runs the shop feature:
def shop():
global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
print('''Welcome to the shop! Feel free to look around!''')
print('''\n
\tAttack Kit 1: +1 to ATK, 25 Gold\n
\tAttack Kit 2: +5 to ATK, 125 Gold\n
\tAttack Kit 3: +10 to ATK, 250 Gold\n
\tSp Attack Kit: +1 to Sp. ATK, 25 Gold\n
\tSp Attack Kit: +5 to Sp. ATK, 125 Gold\n
\tSp Attack Kit: +10 to Sp. ATK, 250 Gold\n
\tDefense Kit 1: +1 to DEF, 25 Gold\n
\tDefense Kit 2: +5 to DEF, 125 Gold\n
\tDefense Kit 3: +10 to DEF, 250 Gold\n
\tSp Defense Kit 1: +1 to Sp. DEF, 25 Gold\n
\tSp Defense Kit 2: +5 to Sp. DEF, 125 Gold\n
\tSp Defense Kit 3: +10 to Sp. DEF, 250 Gold\n
\tSpeed Kit 1: +1 to Speed, 25 Gold\n
\tSpeed Kit 2: +5 to Speed, 125 Gold\n
\tSpeed Kit 3: +10 to Speed, 250\n
\tQuit: Exits the shop\n
\nWhat will Bulbasaur buy?''')
atk = input('''>>> ''')
atk = atk.lower()
if atk == '''attack kit 1''':
if gold >= 25:
atk += 1
gold -= 25
print('''Bulbasaur bought the Attack Kit 1.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''attack kit 2''':
if gold >= 125:
atk += 5
gold -= 125
print('''Bulbasaur bought the Attack Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''attack kit 3''':
if gold >= 250:
atk += 10
gold -= 250
print('''Bulbasaur bought the Attack Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''sp attack kit 1''':
if gold >= 25:
spatk += 1
gold -= 25
print('''Bulbasaur bought the Sp Attack Kit 1.\nBulbasaur now has ''', gold, ''' Gold''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''sp attack kit 2''':
if gold >= 125:
spatk += 5
gold -= 125
print('''Bulbasaur bought the Sp Attack Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''sp attack kit 3''':
if gold >= 250:
spatk += 10
gold -= 250
print('''Bulbasaur bought the Sp Attack Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''defense kit 1''':
if gold >= 25:
armr += 1
gold -= 25
print('''Bulbasaur bought the Defense Kit 1.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''defense kit 2''':
if gold >= 125:
armr += 5
gold -= 125
print('''Bulbasaur bought the Defense Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''defense kit 3''':
if gold >= 250:
armr += 10
gold -= 250
print('''Bulbasaur bought the Defense Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''sp defense kit 1''':
if gold >= 25:
sparmr += 1
gold -= 25
print('''Bulbasaur bought the Sp Defense Kit 1.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''sp defense kit 2''':
if gold >= 125:
sparmr += 5
gold -= 125
print('''Bulbasaur bought the Sp Defense Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''sp defense kit 3''':
if gold >= 250:
sparmr += 10
gold -= 250
print('''Bulbasaur bought the Sp Defense Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''speed kit 1''':
if gold >= 25:
speed += 1
gold -= 25
print('''Bulbasaur bought the Speed Kit 1.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''speed kit 2''':
if gold >= 125:
speed += 5
gold -= 125
print('''Bulbasaur bought the Speed Kit 2.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''speed kit 3''':
if gold >= 250:
speed += 10
gold -= 250
print('''Bulbasaur bought the Speed Kit 3.\nBulbasaur now has ''', gold, ''' Gold.''')
shop()
else:
print('''Bulbasaur does not have enough Gold to buy that.''')
shop()
elif atk == '''quit''':
win = 0
level += 10
print('''You have reached Level ''', level / 10, '''.\n''')
setVar()
#The function that runs the entire game- funny how simple you can make code with 'def' functions:
def runGame():
global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
if win == 0:
playerTurn()
enemyMove()
reportStats()
elif win == 1:
shop()
#The part which causes the whole game to go into effect- funny how it's so short and yet so important:
while loop == 1:
global hp, speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
runGame()
Hm. Now when I run this, when someone gets KO'd, nothing happens. It just keeps looping through and saying that someone has 0 HP. Any help?
Last edited by maxskywalker (2011-12-19 10:19:20)
Offline
Harakou wrote:
What exactly doesn't work?
When you try to type a move, it gives you an error. Try it.
Offline
Well, I just found something very baffling. The error I'm getting is that the pp variables are called but not defined, which I thought was odd because you define those in the beginning of the script. So, I was running it in the debugger, and everything goes along fine with variables and functions being declared until you actually call the runGame() function. At that point, all the variables inexplicably disappear. I really have no idea why it's doing this, but let me look into it a bit further.
Offline
I have not done any python programming in a couple of years . . . but don't you need to pass variables when you call the function. Such as runGame(variable,variable) etc.?
Offline
Ah yes demosthenes, that's right. I just thought of that a few minutes before you said that. XD I feel stupid now for overlooking that. You need to pass all the variables that the methods are going to need as arguments. (Likewise, you can't set variables that the whole program is going to use in a method, because they're local. You have to return them.)
Alternatively, instead of passing a bazillion arguments back and forth between functions, you can define them as globals.
Last edited by Harakou (2011-07-22 09:55:13)
Offline
Harakou wrote:
Ah yes demosthenes, that's right. I just thought of that a few minutes before you said that. XD I feel stupid now for overlooking that. You need to pass all the variables that the methods are going to need as arguments. (Likewise, you can't set variables that the whole program is going to use in a method, because they're local. You have to return them.)
Alternatively, instead of passing a bazillion arguments back and forth between functions, you can define them as globals.
How can I define them as Globals? I'm not familiar in the field of local/global variables. I thought that they were all global unless they were in a
def function(variable):
thing.
Offline
maxskywalker wrote:
Harakou wrote:
Ah yes demosthenes, that's right. I just thought of that a few minutes before you said that. XD I feel stupid now for overlooking that. You need to pass all the variables that the methods are going to need as arguments. (Likewise, you can't set variables that the whole program is going to use in a method, because they're local. You have to return them.)
Alternatively, instead of passing a bazillion arguments back and forth between functions, you can define them as globals.How can I define them as Globals? I'm not familiar in the field of local/global variables. I thought that they were all global unless they were in a
Code:
def function(variable):thing.
Variables defined outside of a function (i.e. in the main body) can be used as globals, but by default when you call a function it will create a new local variable even if the name is the same, so you have to declare that the variable you're going to use is an existing global before the function can use and change it. For example:
var1 = 42 def change(num): var1 = 42 var1 += num change(1337) print(var1)
While you might expect this to print 1379, it actually prints 42 because the global variable var1 is never changed. Instead, the change(num) function creates its own local var1 variable that never leaves that function, and is never passed to the print function. What you need to do is tell the change function to use the global "var1" rather than create a new local variable with the same name.
var1 = 42 def change(num): global var1 var1 = 42 var1 += num change(1337) print(var1)
In this script, the only addition is that var1 is defined as global in the change function, telling it to work with the global variable. Now, 1379 is printed out onscreen as expected when you run it.
Offline
Harakou wrote:
maxskywalker wrote:
Harakou wrote:
Ah yes demosthenes, that's right. I just thought of that a few minutes before you said that. XD I feel stupid now for overlooking that. You need to pass all the variables that the methods are going to need as arguments. (Likewise, you can't set variables that the whole program is going to use in a method, because they're local. You have to return them.)
Alternatively, instead of passing a bazillion arguments back and forth between functions, you can define them as globals.How can I define them as Globals? I'm not familiar in the field of local/global variables. I thought that they were all global unless they were in a
Code:
def function(variable):thing.
Variables defined outside of a function (i.e. in the main body) can be used as globals, but by default when you call a function it will create a new local variable even if the name is the same, so you have to declare that the variable you're going to use is an existing global before the function can use and change it. For example:
Code:
var1 = 42 def change(num): var1 = 42 var1 += num change(1337) print(var1)While you might expect this to print 1379, it actually prints 42 because the global variable var1 is never changed. Instead, the change(num) function creates its own local var1 variable that never leaves that function, and is never passed to the print function. What you need to do is tell the change function to use the global "var1" rather than create a new local variable with the same name.
Code:
var1 = 42 def change(num): global var1 var1 = 42 var1 += num change(1337) print(var1)In this script, the only addition is that var1 is defined as global in the change function, telling it to work with the global variable. Now, 1379 is printed out onscreen as expected when you run it.
So does that mean that I can just say
global pp1 = * global pp2 = * global pp3 = * global pp4 = *
?
Offline
maxskywalker wrote:
Harakou wrote:
maxskywalker wrote:
How can I define them as Globals? I'm not familiar in the field of local/global variables. I thought that they were all global unless they were in aCode:
def function(variable):thing.
Variables defined outside of a function (i.e. in the main body) can be used as globals, but by default when you call a function it will create a new local variable even if the name is the same, so you have to declare that the variable you're going to use is an existing global before the function can use and change it. For example:
Code:
var1 = 42 def change(num): var1 = 42 var1 += num change(1337) print(var1)While you might expect this to print 1379, it actually prints 42 because the global variable var1 is never changed. Instead, the change(num) function creates its own local var1 variable that never leaves that function, and is never passed to the print function. What you need to do is tell the change function to use the global "var1" rather than create a new local variable with the same name.
Code:
var1 = 42 def change(num): global var1 var1 = 42 var1 += num change(1337) print(var1)In this script, the only addition is that var1 is defined as global in the change function, telling it to work with the global variable. Now, 1379 is printed out onscreen as expected when you run it.
So does that mean that I can just say
Code:
global pp1 = * global pp2 = * global pp3 = * global pp4 = *?
No; you'll get an error that way. Variables are automatically global if not declared within a function. You just need to tell the function to use the global variable instead of a local one.
Offline
Harakou wrote:
maxskywalker wrote:
Harakou wrote:
Variables defined outside of a function (i.e. in the main body) can be used as globals, but by default when you call a function it will create a new local variable even if the name is the same, so you have to declare that the variable you're going to use is an existing global before the function can use and change it. For example:
Code:
var1 = 42 def change(num): var1 = 42 var1 += num change(1337) print(var1)While you might expect this to print 1379, it actually prints 42 because the global variable var1 is never changed. Instead, the change(num) function creates its own local var1 variable that never leaves that function, and is never passed to the print function. What you need to do is tell the change function to use the global "var1" rather than create a new local variable with the same name.
Code:
var1 = 42 def change(num): global var1 var1 = 42 var1 += num change(1337) print(var1)In this script, the only addition is that var1 is defined as global in the change function, telling it to work with the global variable. Now, 1379 is printed out onscreen as expected when you run it.
So does that mean that I can just say
Code:
global pp1 = * global pp2 = * global pp3 = * global pp4 = *?
No; you'll get an error that way. Variables are automatically global if not declared within a function. You just need to tell the function to use the global variable instead of a local one.
But how? I tried doing 'global var' after every declaration, but it didn't work.
Last edited by maxskywalker (2011-07-24 16:38:36)
Offline
maxskywalker wrote:
Harakou wrote:
maxskywalker wrote:
So does that mean that I can just sayCode:
global pp1 = * global pp2 = * global pp3 = * global pp4 = *?
No; you'll get an error that way. Variables are automatically global if not declared within a function. You just need to tell the function to use the global variable instead of a local one.
But how? I tried doing 'global var' after every declaration, but it didn't work.
Adding this to the beginning of your playerMove() function (unless there's a variable I missed) should make that function work. Then just do the same with the other functions, except with whatever variables they need to work with to work.
global speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
Offline
Harakou wrote:
maxskywalker wrote:
Harakou wrote:
No; you'll get an error that way. Variables are automatically global if not declared within a function. You just need to tell the function to use the global variable instead of a local one.But how? I tried doing 'global var' after every declaration, but it didn't work.
Adding this to the beginning of your playerMove() function (unless there's a variable I missed) should make that function work. Then just do the same with the other functions, except with whatever variables they need to work with to work.
Code:
global speed, pp1, pp2, pp3, pp4, spatk, ehp, eatk
Okay! Thanks, man! Doing now!
Offline
Closed by request of owner.
Offline
You could try putting a checker in the runGame() function. Like
if loop == 0: break
Offline
nickbrickmaster wrote:
You could try putting a checker in the runGame() function. Like
Code:
if loop == 0: break
The break function only works in a loop. If I put it in the while loop, it doesn't work anyway.
Offline