因此,对于游戏的主要动作序列,我具有用于战斗的代码。它的工作原理。我可以选择移动,所有的文字都会显示出来,但不会使对手失去生命,转弯也不会改变。我一生无法弄清楚自己做错了什么。这是代码: 我正在使用python 2.7。
play_again = True
while play_again is True:
winner = None
player_health = 100
computer_health = random.randrange(1,200)
player_turn = True
computer_turn = False
while (player_health != 0 or computer_health != 0):
heal_up = False
miss = False
moves = {"Punch": random.randint(18, 25),
"Slap": random.randint(10, 35),
"Kick": random.randint(10, 25)}
if player_turn is True:
print("""1. Punch
2. Slap
3. Kick
""")
player_move = raw_input(">>> ")
move_miss = random.randint(1,10)
if move_miss == 1:
miss = True
else:
miss = False
if miss:
player_move = 0
print("You missed!")
else:
if player_move in ("1", "punch"):
player_move = moves["Punch"]
print("You used Punch! It dealt %s damage!") % player_move
elif player_move in ("2", "slap"):
player_move = moves["Slap"]
print("\nYou used Slap!. It dealt %s damage.") % player_move
elif player_move in ("3", "kick"):
player_move = moves["Kick"]
print("\nYou used Kick! It dealt %s damage.") % player_move
else:
print("\nThat is not a valid move. Please try again.")
else:
move_miss = random.randint(0, 10)
if move_miss == 1:
miss = True
else:
miss = False
if miss:
computer_move = 0
print('The Opponent Missed!')
else:
imoves = ["Punch", "Slap","Kick"]
imoves = random.choice(imoves)
CPU_move = moves[imoves]
if CPU_move == moves["Punch"]:
print("The opponent used Punch. It dealt %s Damage.") % CPU_move
player_health -= CPU_move
if CPU_move == moves["Slap"]:
print("\nThe opponent used Slap. It dealt %s Damage.") % CPU_move
player_health -= CPU_move
if CPU_move == moves["Kick"]:
print("\nThe opponent used Kick. It dealt %s Damage.") % CPU_move
player_health -= CPU_move
if player_turn is true:
computer_health -= player_move
if computer_health <= 0:
computer_health = 0
winner = "Player"
break
else:
if player_health <= 0:
player_health = 0
winner = "Computer"
break
print("Your health: %s, Opponents health: %s") % (player_health, computer_health)
# switch turns
player_turn = not player_turn
computer_turn = not computer_turn
if winner == "Player":
print("Your health: %s, Opponents health: %s") % (player_health, computer_health)
print('Congratulations! You have won.')
else:
print("Your health: %s, Opponents health: %s") % (player_health, computer_health)
print("Sorry, but your opponent wiped the floor with you. Better luck next time.")
答案 0 :(得分:2)
您用于处理降低对手健康状况和翻转转弯的整个代码不正确地缩进了外循环(检查玩家是否想再次玩游戏)而不是内循环(实际上是主循环)每回合播放一次。
只需再缩进两个空格,然后在True
中修正if player_turn is true:
的错字(True
的首字母必须大写),您的代码就可以工作:
import random
play_again = True
while play_again is True:
winner = None
player_health = 100
computer_health = random.randrange(1,200)
player_turn = True
computer_turn = False
while (player_health != 0 or computer_health != 0):
heal_up = False
miss = False
moves = {"Punch": random.randint(18, 25),
"Slap": random.randint(10, 35),
"Kick": random.randint(10, 25)}
if player_turn is True:
print("""1. Punch
2. Slap
3. Kick""")
player_move = raw_input(">>> ")
move_miss = random.randint(1,10)
if move_miss == 1:
miss = True
else:
miss = False
if miss:
player_move = 0
print("You missed!")
else:
if player_move in ("1", "punch"):
player_move = moves["Punch"]
print("You used Punch! It dealt %s damage!") % player_move
elif player_move in ("2", "slap"):
player_move = moves["Slap"]
print("\nYou used Slap!. It dealt %s damage.") % player_move
elif player_move in ("3", "kick"):
player_move = moves["Kick"]
print("\nYou used Kick! It dealt %s damage.") % player_move
else:
print("\nThat is not a valid move. Please try again.")
else:
move_miss = random.randint(0, 10)
if move_miss == 1:
miss = True
else:
miss = False
if miss:
computer_move = 0
print('The Opponent Missed!')
else:
imoves = ["Punch", "Slap","Kick"]
imoves = random.choice(imoves)
CPU_move = moves[imoves]
if CPU_move == moves["Punch"]:
print("The opponent used Punch. It dealt %s Damage.") % CPU_move
player_health -= CPU_move
if CPU_move == moves["Slap"]:
print("\nThe opponent used Slap. It dealt %s Damage.") % CPU_move
player_health -= CPU_move
if CPU_move == moves["Kick"]:
print("\nThe opponent used Kick. It dealt %s Damage.") % CPU_move
player_health -= CPU_move
if player_turn is True:
computer_health -= player_move
if computer_health <= 0:
computer_health = 0
winner = "Player"
break
else:
if player_health <= 0:
player_health = 0
winner = "Computer"
break
print("Your health: %s, Opponents health: %s") % (player_health, computer_health)
# switch turns
player_turn = not player_turn
computer_turn = not computer_turn
if winner == "Player":
print("Your health: %s, Opponents health: %s") % (player_health, computer_health)
print('Congratulations! You have won.')
else:
print("Your health: %s, Opponents health: %s") % (player_health, computer_health)
print("Sorry, but your opponent wiped the floor with you. Better luck next time.")
以下是带有修复程序的示例输出:
1. Punch
2. Slap
3. Kick
>>> 2
You used Slap!. It dealt 34 damage.
Your health: 100, Opponents health: 59
The opponent used Slap. It dealt 31 Damage.
Your health: 69, Opponents health: 59
1. Punch
2. Slap
3. Kick
>>> 1
You used Punch! It dealt 21 damage!
Your health: 69, Opponents health: 38
The Opponent Missed!
Your health: 69, Opponents health: 38
1. Punch
2. Slap
3. Kick
>>> 1
You used Punch! It dealt 19 damage!
Your health: 69, Opponents health: 19
The opponent used Kick. It dealt 19 Damage.
Your health: 50, Opponents health: 19
1. Punch
2. Slap
3. Kick
>>> 1
You used Punch! It dealt 22 damage!
Your health: 50, Opponents health: 0
Congratulations! You have won.
1. Punch
2. Slap
3. Kick
>>>