文本冒险游戏战斗循环异常:满足条件后停止而非继续执行
文本冒险游戏战斗循环终止问题修复
问题描述
开发文本类冒险游戏时,战斗环节未按预期循环至敌人或玩家生命值归零,仅执行几次循环后就终止,未进入后续结局交互流程。
原代码片段
初始属性设置
def game(): import random att = random.randint(15,30) health = 100 defen = random.randint(10,20) print("Your stats are:\n") print("Attack Points:", att) print("HP:", health) print("Defense Points:", defen)
战斗环节代码
skel_att = random.randint(10,20) skel_health = 30 skel_def = random.randint(8,12) while skel_health > 0 and health > 0: print("You attack the skeleton!") dam_at_skel = att - skel_def skel_health = skel_health - dam_at_skel print("The skeleton attacked!") skel_dam = skel_att - defen health = health - skel_dam print("Oh no! Your health is:", health) if skel_health > 0 or health > 0: continue elif health == 0: print("The skeleton lays his final blow and you fall to the cold stone floor. The villagers will never find your body, but at least the skeleton will have a buddy in his afterlife!\nEnding Four: Spooky Scary Skeletons\nTry Again!") elif skel_health == 0: print("You land your final blow and the skeleton falls to pieces on the floor. You grab the EPIC SWORD and feel prepared to take on the dragon!") new_heal = 100 att = att + 5 print("With the sword in hand you feel reinvigorated and energized. Your health has been healed in full!\nThe only places left to go is up or out the door. Where do you go?") fourth_choice = str(input())
运行输出
game() Your stats are: Attack Points: 28 HP: 100 Defense Points: 11 You are the epic hero of legend. At least as epic as this text based adventure can get. You approach the dark castle belonging to the evil dragon. Upon entering the castle there's only a few directions you can go. Up the grand staircase, down the left passage, down the right passage, or back out the front door. Probably not a good idea to go upstairs yet since the dragon is there, but it's your funeral. Will you go up, left, right, or leave? Right You go down the right corridor. Up on the wall there is an EPIC SWORD fit for an epic hero of legend like you. It's much more fitting than the frying pan you've been carrying at least! But oh no, a skeleton popped out! And he is standing between you and the EPIC SWORD. Time to send this skeleton back to the graveyard! You attack the skeleton! The skeleton attacked! Oh no! Your health is: 91 You attack the skeleton! The skeleton attacked! Oh no! Your health is: 82
问题原因及修复方案
核心问题
- 结局判断逻辑位置错误:当敌人或玩家生命值归零后,
while循环条件不满足会直接退出,此时循环内的elif分支根本不会执行,导致程序终止。 - 伤害计算可能出现异常:若玩家攻击小于等于敌人防御,会造成0或负数伤害,导致敌人生命值不减反增,引发不符合逻辑的战斗流程。
- 敌人攻击值固定:
skel_att在循环外定义,每次战斗敌人攻击数值相同,缺乏随机性。
修复后的代码
def game(): import random att = random.randint(15,30) health = 100 defen = random.randint(10,20) print("Your stats are:\n") print("Attack Points:", att) print("HP:", health) print("Defense Points:", defen) # 战斗环节 skel_health = 30 skel_def = random.randint(8,12) while skel_health > 0 and health > 0: # 敌人每次攻击随机生成数值 skel_att = random.randint(10,20) print("You attack the skeleton!") # 确保伤害至少为1,避免无效攻击 dam_at_skel = max(att - skel_def, 1) skel_health -= dam_at_skel print(f"Skeleton's health: {skel_health}") # 新增敌人生命值显示,便于调试 # 敌人攻击后检查是否已死亡,避免玩家被攻击 if skel_health <= 0: break print("The skeleton attacked!") # 确保敌人伤害至少为1,避免玩家回血 skel_dam = max(skel_att - defen, 1) health -= skel_dam print("Oh no! Your health is:", health) # 循环结束后判断结局 if health <= 0: print("The skeleton lays his final blow and you fall to the cold stone floor. The villagers will never find your body, but at least the skeleton will have a buddy in his afterlife!\nEnding Four: Spooky Scary Skeletons\nTry Again!") elif skel_health <= 0: print("You land your final blow and the skeleton falls to pieces on the floor. You grab the EPIC SWORD and feel prepared to take on the dragon!") att += 5 health = 100 # 直接重置生命值,无需额外变量 print("With the sword in hand you feel reinvigorated and energized. Your health has been healed in full!\nThe only places left to go is up or out the door. Where do you go?") fourth_choice = input() # str()可省略,input默认返回字符串 # 这里可以继续处理fourth_choice的逻辑
修复说明
- 将结局判断移至
while循环外部,确保循环结束后必然执行结局逻辑。 - 优化伤害计算,用
max()函数保证伤害至少为1,避免无效攻击或玩家回血的不合理情况。 - 将
skel_att移至循环内部,每次战斗敌人攻击数值随机,提升游戏体验。 - 新增敌人生命值打印,便于调试战斗流程。
- 简化生命值重置逻辑,直接赋值100替代多余变量
new_heal。
内容的提问来源于stack exchange,提问作者ForeverLost20
相关产品推荐
相关产品推荐

