0

タイトルが紛らわしくてすみません。そして、私が間違った用語を使用している場合。先週コーディングを始めたばかりです。

テキストアドベンチャーゲームのボス戦用のサイコロを振る関数を書いていますが、サイコロ関数で関数の外側の元のグローバル変数を使用し、数値を減算して関数内で報告することはできますが、そうではありません関数の実行後にグローバル変数を更新します。そのため、次に関数を呼び出そうとすると、元の値が再び使用され、そもそもそこにサイコロを配置するという目的が完全に無効になります。(ボスを殺すことはできません、笑)

これが私がデバッグしようとして遊んできたものです。前もって感謝します!

player = "Dib"
playerhealth = 3
boss = "Zim"
bosshealth = 5

import random

def dice(who, whohealth):
    min = 1
    max = 3
    dice = random.randint(min, max)

    if dice == 1:
        print "Your opponent lost no health"
        print "Your opponent has %d health" % whohealth
    elif dice == 2:
        print "%s hits" % who
        whohealth = whohealth - 1
        print "Your opponent lost 1 health"
        print "Your opponent has %d health" % whohealth
    elif dice == 3:
        print "%s crits" % who
        whohealth = whohealth - 2
        print "Your opponent lost 2 health"
        print "Your opponent has %d health" % whohealth
    else:
        print "stuff"

dice(player, bosshealth)
dice(player, bosshealth)

dice(boss, playerhealth)
dice(boss, playerhealth)
4

2 に答える 2

0

あなたは戻ってこなかっwhohealthた。Python は参照によってオブジェクトを渡しますが、関数内で参照を再バインドしています。

whohealth = whohealth - 1

これにより、新しい値がローカル名にのみ割り当てられwhohealthます。元の参照は更新されません。

これに対処する最善の方法は、新しい値を返すことです。

def dice(who, whohealth):
    min = 1
    max = 3
    dice = random.randint(min, max)

    if dice == 1:
        print "Your opponent lost no health"
        print "Your opponent has %d health" % whohealth
    elif dice == 2:
        print "%s hits" % who
        whohealth = whohealth - 1
        print "Your opponent lost 1 health"
        print "Your opponent has %d health" % whohealth
    elif dice == 3:
        print "%s crits" % who
        whohealth = whohealth - 2
        print "Your opponent lost 2 health"
        print "Your opponent has %d health" % whohealth
    else:
        print "stuff"

    return whohealth

bosshealth = dice(player, bosshealth)
bosshealth = dice(player, bosshealth)

playerhealth = dice(boss, playerhealth)
playerhealth = dice(boss, playerhealth)

これで、関数は新しいヘルス値を返し、その値をbosshealthまたはplayerhealthグローバルに割り当てることができます。

于 2013-10-09T17:27:01.827 に答える
0

物事を書き直そうとするのではなく、辞書を使用してそれを行う別の方法を次に示します。

player = {
  'name' : "Dib",
  'health' : 3
}
boss = {
  'name' : "Zim",
  'health' : 5
}

import random

def attack(attacker, attacked):
    min = 1
    max = 3
    dice = random.randint(min, max)

    if dice == 1:
        print "Your opponent lost no health"
        print "Your opponent has %d health" % attacked['health']
    elif dice == 2:
        print "%s hits" % attacker['name']
        attacked['health'] -= 1
        print "Your opponent lost 1 health"
        print "Your opponent has %d health" % attacked['health']
    elif dice == 3:
        print "%s crits" % attacker['name']
        attacked['health'] -= 2
        print "Your opponent lost 2 health"
        print "Your opponent has %d health" % attacked['health']
    else:
        print "stuff"

attack(player, boss)
attack(player, boss)

attack(boss, player)
attack(boss, player)
于 2013-10-09T17:27:01.893 に答える