#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep 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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep 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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    oops


    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
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    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)
    Last edited by b49P23TIvg; July 24th, 2012 at 06:22 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo