gpt4 book ai didi

python - 对手的生命值没有下降,转弯也没有翻转

转载 作者:行者123 更新时间:2023-12-01 01:43:33 25 4
gpt4 key购买 nike

所以,对于我的游戏的主要 Action 序列,我有这个战斗代码。它确实有效。我可以选择 Action 并且所有文本都会显示,但它不会减少对手的生命值,并且回合不会改变。我这辈子都无法弄清楚我做错了什么。这是代码:我使用的是 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.")

最佳答案

处理降低对手生命值和翻转回合的整个代码不正确地缩进到外循环(检查玩家是否想再次玩)而不是内循环(这是实际上的主循环)内。每回合播放)。

只需将该代码缩进两个空格,并修复 if player_turn is true: 中的 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
>>>

关于python - 对手的生命值没有下降,转弯也没有翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51628255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com