以下の明らかな兆候でお気づきかもしれませんが、私はゲーム、つまり格闘シミュレーションのようなものを作成しようとしています。単純なゲームを作成しなければならないクラス課題の非常に基本的なものです (必要以上に複雑にしているのかもしれませんが、楽しみたかったのです。現時点では、メイン ループがあり、ユーザーのヘルスがゼロより大きく、開始時 (100) である場合、最初の戦闘に進み、100 より大きい状態ですべての戦闘を通過した場合、彼は勝利します. 100 を下回った場合、彼は負けます. . 私の問題は、ヘルスが実際に低下するかどうかをテストするときに、次のエラーのためにユーザーのヘルスが低下しなかったことです.情報を知る必要がある場合、私はpython 2.7を使用しています.
Traceback (most recent call last):
File "a3.py", line 109, in <module>
fighting_arena()
File "a3.py", line 63, in fighting_arena
menu_loop()
File "a3.py", line 37, in menu_loop
main_loop()
File "a3.py", line 69, in main_loop
easy_fight()
File "a3.py", line 96, in easy_fight
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
UnboundLocalError: local variable 'user_health' referenced before assignment
-
def main_loop():
global user_health
user_health = 100
while user_health > 0:
easy_fight()
end_game_victory()
else:
end_game_defeat()
def easy_fight():
easy_opponent_health = 50
easy_opponent_skills = ['1', '2', '3', '4']
print '"Your first opponent is Hagen, a germanic gladiator. He bares no armor, but he has a shield and uses a long sword. Beware of his vicious strength!"'
time.sleep(2)
print 'You enter the arena surrounded by thousands of Romans demanding blood with Hagen across the field. The fight begins!'
while easy_opponent_health > 0:
a = raw_input()
b = random.choice(easy_opponent_skills)
if a == "1" and b == "1":
print "You both slashed each other!"
user_health -= 5
easy_opponent_health -= 5
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
elif a == "1" and b == "2":
print "You slashed Hagen while he managed to get a non-lethal stab!"
user_health -= 2.5
easy_opponent_health -= 5
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
elif a == "1" and b == "3":
print "Your slash only scratched Hagen as he dodged it!"
easy_opponent_health -= 2.5
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
elif a == "1" and b == "4":
print "Your slash was blocked by Hagen!"