944,167 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3077
  • Python RSS
Oct 29th, 2006
0

global or function?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mruane is offline Offline
76 posts
since Oct 2006
Oct 29th, 2006
0

Re: global or function?

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
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 30th, 2006
0

Re: global or function?

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')?
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 30th, 2006
0

Re: global or function?

Click to Expand / Collapse  Quote originally posted by LaMouche ...
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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 30th, 2006
0

Re: global or function?

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.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 30th, 2006
0

Re: global or function?

Might be better to start a new thread with a meaningful title, like "RPG Battle Action"
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: wxPython Error while running.
Next Thread in Python Forum Timeline: Class - Shuffle()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC