Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 24th, 2012, 11:51 AM
bloodycage bloodycage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 bloodycage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old July 24th, 2012, 03:31 PM
bloodycage bloodycage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 bloodycage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #3  
Old July 24th, 2012, 03:33 PM
bloodycage bloodycage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 bloodycage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old July 24th, 2012, 05:17 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help with RPG game (local and global scopes)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap