0

私はゲームのようなRPGを作っています。プレイヤーと敵が交代で戦うところ。敵とプレイヤーの両方にダメージ範囲があり[例(4,10)]、攻撃するときにこの範囲から乱数を取ります。

キャラクターの1人が0HPに達するまで、この関数をループさせています。唯一の問題は、最初のループでは乱数のみを使用し、その後は残りのループで同じ番号を使用することです。新しいループごとに新しい乱数を取得するにはどうすればよいですか?

   import time
import random
global myhp
myhp = 20
global mydmg
mydmg = random.randint(2,5)
mygold = 0
mystr = 2



def start():
    print "Hello there."
    myname = raw_input("What is your name? ")
    print "Welcome %s, this is..." %myname
    uname = myname.upper()
    print "\t\t\tTHE ADVENTURES OF %s" %uname
    choice0 = ''
    allowed = ["y", "n"]
    while choice0.lower() not in allowed:
        choice0 = raw_input("\nWould you like to play the game? Y/N ")
        choice0 = choice0.lower()
    if choice0 == "y":
        game1()
    if choice0 == "n":
        print "Alright, bye!"

def fightmode(name, hp, dmg, gold):
    global myhp
    print '\n\n\nYou are in a fight with %s' %name
    print '%s has %sHP' %(name, hp)
    while myhp > 0 and hp > 0:
        print '\n\t1. Attack \n\t2. Guard \n\t3. Run away.'
        opt1= ''
        allowed = ["1", "2", "3"]
        while opt1 not in allowed:
            opt1 = raw_input("\nWhat will you do? ")
            if opt1 == "1":
                hp = hp - mydmg
                myhp = myhp - dmg
                print "You have inflicted %d damage on %s. %s's HP is %s" %(mydmg, name, name, hp)
                print "%s attacked you and did %d damage. Your HP fell down to %s" %(name, dmg, myhp)
            if opt1 == "2":
                myhp = myhp + 5
                print "You are now guarding yourself. Your HP is now %d" %myhp
                myhp = myhp - dmg
                print "%s attacked you and did %d damage. Your HP fell down to %s" %(name, dmg, myhp)

    if myhp > 0 :
        print"myhp"
    if hp > 0 :
        print"theirhp"





    def fightmode0():
        print """\n\nThis is your first fight. You have 3 seconds each turn
    if you fail to make a move in 3 seconds, you will lose your turn.

    By "Attacking", you inflict damage on the enemy\'s HP, get it down to 0 to defeat him.
    If your HP reaches 0, you will be defeated. 

    By "Guarding", you will regain 10HP back, but that counts as your turn.
    By defeating enemies, you gain gold Use gold to purchase upgrades 
    when you come across a shop.

    You can choose to "Run Away", but you will only have a 1/10 chance of it being sucessful
    """
        raw_input("\nPress any key to continue to battle")
        fightmode("Scrawny Thug", 15, random.randint(1,5), 4)




    def game1():
        print "You wake up and find yourself locked in a room..."
        print "You think you're kidnapped."
        print "Yea, you're probably kidnapped."
        print "You hear footsteps approaching the door..."
        print "\n\t1. Remain in fetal position \n\t2. Attempt a sneak attack" 
        choice1 = ''
        allowed = ["1", "2"]
        while choice1 not in allowed:
            choice1 = raw_input("\nWhat will you do? ")
        print "The doorknob rattles..."
        print "..."
        print "..."
        if choice1 == "1":
            print '"Hey!"'
            print '"Get up maggot!"'
            print 'You see that the thug is small and scrawny'
            print 'He grabs you by your hair and pulls you up'
            print "\n\t1. Punch him in the face. \n\t2. Do nothing" 
            choice1_1 = ''
            allowed = ["1", "2",]
            while choice1_1 not in allowed:
                choice1_1= raw_input("\nWhat will you do?? ")
            if choice1_1 == "1":
                print '\nYou punch the scrawny thug and he lets you go'
                print '"You\'re going to pay for that."'
                print '\n\t\t>>>>>>>>>>ENTERING FIGHT MODE'
                fightmode0()



    start()
4

2 に答える 2

1

functionのループ内にdmg = random.randint(dmg_min, dmg_max)配置して、各ループが再計算されるようにし、関数を次のように再定義します。whilefightmode

def fightmode(name, hp, dmg_min, dmg_max, gold):
于 2012-09-26T09:12:43.590 に答える
0

必要に応じて新しい乱数を生成するだけです:

それ以外の:

fightmode("Scrawny Thug", 15, random.randint(1,5), 4)

これを次のように変更します。

fightmode("Scrawny Thug", 15, (1, 5), 4)

関数を少し変更します。

def fightmode(name, hp, dmg_range, gold): # takes range as randint parameters
    global myhp    
    print '\n\n\nYou are in a fight with %s' %name    
    print '%s has %sHP' %(name, hp)    
    while myhp > 0 and hp > 0:   
        dmg = random.randint(*dmg_range) # generate new each time
        print '\n\t1. Attack \n\t2. Guard \n\t3. Run away.'
于 2012-09-26T09:14:53.683 に答える