The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Help with RPG game (local and global scopes)
Discuss Help with RPG game (local and global scopes) in the Python Programming forum on Dev Shed. Help with RPG game (local and global scopes) Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 24th, 2012, 11:51 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 3 m 24 sec
Reputation Power: 0
|
|
|
Help with RPG game (local and global scopes)
ive just started to write a very simple rpg program, but then i remembered about local and global scopes, and it kind of mess up my plan, im wondering if theres a way to get more than one value (possibly stored in a variable) back from a def statement, i want to update the hp of the player after a battle(with rat in this case) but i cant change userhp from a local scope, and the rat function needs to be executed many times whenever i want, even when the player's stats have changed.
Code:
import random #i have a plan for these imports later on
import time
def welcome():
print('welcome to rpg world, here are your stats:')
print('Level=' + str(userlvl))
print('Hp=' + str(userhp))
print('Defence=' + str(userarm))
print('attack=' + str(useratt))
def rat():
userbhp = userhp # userbhp is the local scope of a player in battle's health
hp = 5 # monsters health
att = 2 # monsters attack
arm = 0 # monsters armor
while hp > 0 or userbhp > 0:
userbhp = userbhp - (att - userarm)
hp = hp - (useratt - arm)
if hp > 0:
print('you lost the battle')
elif userbhp > 0:
print('You win!')
userlvl = 1 # level of player
userhp = 10 # health points of player
useratt = 3 # attack of player
userarm = 0 # armor of player
usergold = 0 # amount of gold player has
input() # to not terminate the program instantly
|

July 24th, 2012, 03:31 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 3 m 24 sec
Reputation Power: 0
|
|
so i thought i found a solution, by using the global statement, but it didnt return anything when i ran the function, so i did it in idle and it said traceback: global name "useratt" not defined, is there a way to fix it because i set "useratt" to equal 3, thanks in advance
fixed code (almost):
Code:
import random #i have a plan for these imports later on
import time
def welcome():
print('welcome to rpg world, here are your stats:')
print('Level=' + str(userlvl))
print('Hp=' + str(userhp))
print('Defence=' + str(userarm))
print('attack=' + str(useratt))
def rat():
global userhp
hp = 5 # monsters health
att = 2 # monsters attack
arm = 0 # monsters armor
while hp > 0 or userbhp > 0:
hp = hp - (useratt - arm)
userhp = userhp - (att - userarm)
if hp > 0:
print('you lost the battle')
else:
print('You win!')
userlvl = 1 # level of player
userhp = 10 # health points of player
useratt = 3 # attack of player
userarm = 0 # armor of player
usergold = 0 # amount of gold player has
welcome()
print('press 1 for rat')
option = input()
if option == 1:
rat()
input() # to not terminate the program instantly
|

July 24th, 2012, 03:33 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 3 m 24 sec
Reputation Power: 0
|
|
|
oops
Quote: | Originally Posted by bloodycage
Code:
import random #i have a plan for these imports later on
import time
def welcome():
print('welcome to rpg world, here are your stats:')
print('Level=' + str(userlvl))
print('Hp=' + str(userhp))
print('Defence=' + str(userarm))
print('attack=' + str(useratt))
def rat():
global userhp
hp = 5 # monsters health
att = 2 # monsters attack
arm = 0 # monsters armor
while hp > 0 or userhp > 0:
hp = hp - (useratt - arm)
userhp = userhp - (att - userarm)
if hp > 0:
print('you lost the battle')
else:
print('You win!')
userlvl = 1 # level of player
userhp = 10 # health points of player
useratt = 3 # attack of player
userarm = 0 # armor of player
usergold = 0 # amount of gold player has
welcome()
print('press 1 for rat')
option = input()
if option == 1:
rat()
input() # to not terminate the program instantly
|
i changed the userbhp in the while statement back to userhp and still didnt work
|

July 24th, 2012, 05:17 PM
|
 |
Contributing User
|
|
|
|
Code:
import random
import time
def welcome():
print('welcome to rpg world, here are your stats:')
print('Level=' + str(userlvl))
print('Hp=' + str(userhp))
print('Defence=' + str(userarm))
print('attack=' + str(useratt))
def rat(usrhp): # function parameter ##############################################
hp = 5 # monsters health
att = 2 # monsters attack
arm = 0 # monsters armor
while (hp > 0) and (usrhp > 0): # and #############################################################
hp += arm - useratt
usrhp += userarm - att
if hp > 0:
print('you lost the battle')
else:
print('You win!')
return usrhp # return a value ##################################################
userlvl = 1 # level of player
userhp = 10 # health points of player
useratt = 3 # attack of player
userarm = 0 # armor of player
usergold = 0 # amount of gold player has
welcome()
print('press 1 for rat')
option = input()
if option == 1:
userhp = rat(userhp) # pass a value to the function and retrieve the return value ######
This should answer your immediate question.
Another option: pass a mutable parameter, such as a userObject, a list, set, or dictionary.
Also I hate to see you write a different function for each monster you construct. It could work but becomes soon unwieldly. Assuming this game will have similar features to other rpgs you'll be much better off with a design that has one "fight" function into which you pass player characteristics and monster characteristics. Telling you more than you're ready for:
Code:
import sys
import pprint
import random
import time
def welcome(u):
print('welcome to rpg world, here are your stats:')
print('Level=' + str(u['level']))
print('Hp=' + str(u['hp']))
print('Defence=' + str(u['arm']))
print('attack=' + str(u['att']))
def fight(user,monster):
while (monster['hp'] > 0) and (user['hp'] > 0):
monster['hp'] += monster['arm'] - user['att']
user['hp'] += user['arm'] - monster['att']
if monster['hp'] > 0:
print('you lost the battle')
else:
print('You win!')
monsters = {
'rat':dict(hp=5,att=2,arm=0),
'venomous spider':dict(hp=3,att=4,arm=1),
}
user = dict(level=1,hp=10,att=3,arm=0,gold=0)
welcome(user)
print('To the west is a rat. Far ahead north you see a spider.')
print('Which way or snooze? [n, w, z]')
option = sys.stdin.readline()[0].lower()
if 'z' == option:
user['hp'] = min(user['hp']+1,user['level']*10)
elif 'w' == option:
fight(user,monsters['rat'])
else:
fight(user,monsters['venomous spider'])
print('Your stats:')
pprint.pprint(user)
__________________
[code] Code tags[/code] are essential for python code!
Last edited by b49P23TIvg : July 24th, 2012 at 05:22 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|