| | |
global or function?
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 71
Reputation:
Solved Threads: 1
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.
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.
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.
[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)
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!
•
•
•
•
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')?
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!
![]() |
Similar Threads
- C++ Performance Tips (C++)
- How to create a global function/class ? (C#)
- Separating socket code from GUI code (C++)
- Function variables (PHP)
- rounding number to specific decimal place (C)
- code problems (C++)
Other Threads in the Python Forum
- Previous Thread: wxPython Error while running.
- Next Thread: Class - Shuffle()
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






