- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,对于我的游戏的主要 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/
我想循环遍历 gpx 文件并计算总上升和下降。我有一个函数可以计算两组经纬度点之间的高程差异,我已经设置了 simplexml 来读取和循环遍历 gpx 文件 trkseg 点。 问题是,这不准确(实
我有两个在不同时间段拍摄的数组。如何通过将新玩家标记为上升来检查哪些玩家在列表中上升/下降? 附言- 数组已经根据分数排序。 pastData:[ { playerName:'Jo
我想捕获 ctrl/alt/etc 键的起伏,无论表单上的哪个控件获取 keyup 或 keydown 事件。由于我的表单上有大约 100 个控件,如果我要为每个单独的控件添加代码,那将非常难看。我怎
vector1 = c(2, 2, 2, 2, 2, 2) vector2 = c(2, 2, 3, 3, 3, 3) vector3 = c(2, 2, 1, 2, 2, 2) 我想知道向量中的数字
我不知道如何遵循编译器的建议:consider using a let binding to create a longer lived value。 Playground #![allow(unus
我希望有人能帮助我理解 AngularJS 中的 $scope 遇到的一个恼人的问题。请参阅下面我的代码中的注释: app.controller('MyController', function ($
我有一个 flex 搜索集群,其中有2个节点在2核CPU 8GB ram实例上运行。每个节点都传入了参数“ES_JAVA_OPTS = -Xms3g -Xmx3g”。我有4个索引,每个索引有2个分片和
我正在学习 R(及其通过 quantmod lib 在交易任务中的应用)并定期浏览社区以从这里获得许多新知识和技巧。我对 R 的总体印象和特别是 quantmod lib 的印象 - 它很棒。 在这一
当我们点击屏幕时,我正在绘制纹理正方形。我正在使用相同的纹理。在新 ios 设备中点击几次后,FPS 从 120 下降到 4 左右。每次手指点击时,我都会将点击的点以及纹理和纹理的大小传递给着色器。
只有当对象被点击并且需要从列表中移除时它才会掉落。这是代码: if(event.type == TouchEvent.TOUCH_DOWN){ for(Bottle bottl
我有一个基于SpriteKit的小游戏。 在这个游戏中,我使用了很多带有字母(或字母组合)的节点,用户可以四处移动来构建单词。 这些节点基本上是带有 SKLabelNode 的 SKSpriteNod
我有一个简单的CSS布局 wrapper header left-sidebar / main-content / right-sidebar footer 但我的主要内容似乎下降了(float dr
在标题中,我给出了四个不同的部分,并使用 float 属性使所有内容都显示在一条水平线上。 当我调整浏览器窗口大小时,最后一个 div 位于黑色边框线下方。 如何解决。 http://jsfiddle
CSS: .desc{ text-align: center; color:#60A8D5; padding-top: 17px;
这是一段简单的代码,但我为这个问题尝试过的解决方案都没有奏效。 #ONE { float: left; border: 1
我有一个 SceneKit 设置,其中有一个 Sphere 设置为 Dynamic body。 我能够运行该应用程序并看到球体落在静态 body 地板上。 我想做的是设置场景,这样 sfere 最初就
首先,我的类(class): export class FooBar { ... isFavorite: boolean = false; constructor() { this.isF
我正在尝试删除所有端口上的所有传出 RST 和传入 RST。我正在使用 Debian Linux。我尝试了互联网上列出的所有可能的命令组合,但似乎没有任何效果。 例如,我试过: iptables -A
我正在做这样的事情: fn main() { //[1, 0, 0, 0, 99]; // return [2, 0, 0, 0, 99] //[2, 3, 0, 3, 99]; //
我正在使用 Rusqlite,它可以让你做这样的查询: statement.query_row(params!([1, 2, 3]), ...); params!()定义如下: macro_rules
我是一名优秀的程序员,十分优秀!