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.

Recommended Answers

All 5 Replies

Here is an example I was thinking of. Don't create global variables, just pass to and from your functions as needed ...

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)

You need to improve the battle descriptions, maybe draw on sentence lists with random.choice() to get some variety in there.

Member Avatar for Mouche

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')?

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.

Member Avatar for Mouche

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.

Might be better to start a new thread with a meaningful title, like "RPG Battle Action"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.