global or function?

Thread Solved

Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

global or function?

 
0
  #1
Oct 29th, 2006
I am trying Vegaseat's suggestion in one of my threads to use shuffled lists for combat in my text adventure. He suggested creating two lists,
myhit_list[1, 0, 2, 0, 3, 0, 0, 1] and monsterhit_list[1, 0, 2, 0, 3, 0, 0, 1] and using random.shuffle(myhit_list) and random.shuffle(monsterhit_list) iterated with "I hit him" and "he hit me" for loops.

My question is the method of creating this. First, do I create them as globals or as functions? In my understanding a global is a variable that stays consistant throughout every function in the program. Is that correct?

Second, my understanding of a function is a defined mini-program type that allows you to utilize one statement in replace of lengthy codes. Is that anywhere near correct?

And third, is there a website that breaks down Python statements and explains every part of them? Such as what "[" means compared to "(", why it is used where and all that great stuff? I am really interested in getting a more scientific explaination of python code.
thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: global or function?

 
0
  #2
Oct 29th, 2006
Here is an example I was thinking of. Don't create global variables, just pass to and from your functions as needed ...
[php]import random
import time

def delay(seconds):
time.sleep(seconds)

def shuffle_hitlists(my_hitlist, mo_hitlist):
"""shuffle the two lists and return the result"""
random.shuffle(my_hitlist)
random.shuffle(mo_hitlist)
return my_hitlist, mo_hitlist

def battle(my_hitlist, mo_hitlist, my_strength, mo_strength):
# player starts swinging first
for k in range(len(my_hitlist)):
my_hit = my_hitlist[k]
mo_hit = mo_hitlist[k]
# your turn
if my_hit == 1:
print "Scratched him good!"
mo_strength -= 1
elif my_hit == 2:
print "Sliced him deep! Blood and guts!"
mo_strength -= 2
elif my_hit == 3:
print "Excellent hit! He is screaming with pain!"
mo_strength -= 3
else: # my_hit == 0:
print "You missed!"
if mo_strength <= 0:
print "The monster is dead!"
break
delay(2)
# monster's turn
if mo_hit == 1:
print "You got scratched! It will heal quickly!"
my_strength -= 1
elif mo_hit == 2:
print "Ouch, received a deep slice!"
my_strength -= 2
elif mo_hit == 3:
print "You are hit and in pain! Loosing blood!"
my_strength -= 3
else: # mo_hit == 0
print "The monster missed!"
delay(2)
print
if my_strength < 5:
print "You are weak and wounded, better withdraw!"
break

if mo_strength > 0:
print "The fleeing monster's strength is down to", mo_strength
print "You still have a strength of", my_strength

my_hitlist = [1, 0, 2, 1, 3, 0, 3, 1]
mo_hitlist = [1, 0, 2, 0, 3, 0, 1, 1]

my_strength = 20
mo_strength = 10

my_hitlist, mo_hitlist = shuffle_hitlists(my_hitlist, mo_hitlist)

print my_hitlist, '\n', mo_hitlist # test

battle(my_hitlist, mo_hitlist, my_strength, mo_strength)
[/php]You need to improve the battle descriptions, maybe draw on sentence lists with random.choice() to get some variety in there.
Last edited by vegaseat; Oct 29th, 2006 at 7:18 pm. Reason: code error
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: global or function?

 
0
  #3
Oct 30th, 2006
cool battle system, vegaseat! I think I'll use this model to create mine for my RPG. Do you have suggestions for how you could make person equip an item in your inventory and change your stats (such as a weapon increase 'my_strength')?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: global or function?

 
0
  #4
Oct 30th, 2006
Originally Posted by LaMouche View Post
cool battle system, vegaseat! I think I'll use this model to create mine for my RPG. Do you have suggestions for how you could make person equip an item in your inventory and change your stats (such as a weapon increase 'my_strength')?
Depending on your weapon you could deduct another notch from the monster's strength on a hit. Conversely you could do the same thing to your own strength, if the monster is a nastier one!

You also could have a weapon that never misses! So my_hit = 0 would always be at least my_hit = 1.
Last edited by vegaseat; Oct 30th, 2006 at 10:18 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: global or function?

 
0
  #5
Oct 30th, 2006
Hmmm... okay.

So I think I would do it like this:

my_str = weapon # fists default!
my_life = 10
weapon = fists
fists = 1
dagger = 2
short_sword = 3
broad_sword = 5

So when the character picks up a new weapon, you change the self.weapon variable... this could apply to armor, too.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: global or function?

 
0
  #6
Oct 30th, 2006
Might be better to start a new thread with a meaningful title, like "RPG Battle Action"
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC