# ==========================================
# 🎮 少年侠客闯江湖 - 第 7 课
# 📚 本课知识点:函数 def(参数/返回值/默认参数)
# ==========================================
import random
# --- 游戏数据(和第 6 课一样)---
monsters = [
{"name": "狐妖", "hp": 30, "attack": 8, "silver_drop": 10, "item_drop": "狐妖尾毛"},
{"name": "山魅", "hp": 40, "attack": 12, "silver_drop": 15, "item_drop": "山魅石"},
{"name": "火鸦", "hp": 50, "attack": 15, "silver_drop": 20, "item_drop": "火鸦羽"},
]
npcs = [
{"name": "白胡老者", "riddle": "什么东西越长越短?", "answer": "蜡烛", "silver_correct": 15, "silver_wrong": 3},
{"name": "书生", "riddle": "四面都是山,中间有个田", "answer": "画", "silver_correct": 12, "silver_wrong": 3},
]
# TODO: 提取 battle(hero, monster) 函数
# 提示:def battle(hero, monster):
# """回合制战斗,返回 True/False"""
# while hero["hp"] > 0 and monster["hp"] > 0:
# ...
# return hero["hp"] > 0
# TODO: 提取 talk_to_npc(hero, npc) 函数
# 提示:def talk_to_npc(hero, npc):
# ...
# TODO: 用函数简化主循环代码
# ==========================================
# 🎮 少年侠客闯江湖 - 第 7 课(参考答案)
# 📚 本课知识点:函数 def(参数/返回值/默认参数)
# ⚠️ 这是参考答案,请先让孩子自己尝试!
# ==========================================
import random
# --- 游戏数据 ---
monsters = [
{"name": "狐妖", "hp": 30, "attack": 8, "silver_drop": 10, "item_drop": "狐妖尾毛"},
{"name": "山魅", "hp": 40, "attack": 12, "silver_drop": 15, "item_drop": "山魅石"},
{"name": "火鸦", "hp": 50, "attack": 15, "silver_drop": 20, "item_drop": "火鸦羽"},
]
npcs = [
{"name": "白胡老者", "riddle": "什么东西越长越短?", "answer": "蜡烛", "silver_correct": 15, "silver_wrong": 3},
{"name": "书生", "riddle": "四面都是山,中间有个田", "answer": "画", "silver_correct": 12, "silver_wrong": 3},
{"name": "铁匠", "riddle": "千锤百炼出深山", "answer": "石灰", "silver_correct": 10, "silver_wrong": 3},
]
routes = [("忘忧山", "清风鸟语"), ("龙王洞", "阴暗潮湿"), ("幽灵峰", "雷电交加")]
# --- 函数定义(重构!)---
def battle(hero, monster):
"""回合制战斗,返回 True 胜利 / False 落败"""
print(f"\n⚔️ {monster['name']}出现!HP:{monster['hp']} ATK:{monster['attack']}")
while hero["hp"] > 0 and monster["hp"] > 0:
dmg = random.randint(5, hero["attack"])
monster["hp"] -= dmg
print(f"💥 攻击!伤害 {dmg},{monster['name']}剩余 {max(monster['hp'], 0)}")
if monster["hp"] <= 0:
break
m_dmg = random.randint(3, monster["attack"])
hero["hp"] -= m_dmg
print(f"👹 反击!伤害 {m_dmg},{hero['name']}剩余 {max(hero['hp'], 0)}")
if hero["hp"] > 0:
hero["silver"] += monster["silver_drop"]
hero["battles_won"] += 1
print(f"🎉 击败{monster['name']}!+{monster['silver_drop']}银两")
if monster.get("item_drop") and len(hero["backpack"]) < 10:
hero["backpack"].append(monster["item_drop"])
print(f"🎁 获得:{monster['item_drop']}")
return True
print(f"💀 {hero['name']}落败了...")
return False
def talk_to_npc(hero, npc):
"""NPC 互动问答"""
print(f"\n🧙 遇到{npc['name']}!\n "{npc['riddle']}"")
if input("答案:") == npc["answer"]:
hero["silver"] += npc["silver_correct"]
print(f"✨ 答对!+{npc['silver_correct']}银两")
else:
hero["silver"] += npc["silver_wrong"]
print(f"😊 答错了,答案是"{npc['answer']}"。+{npc['silver_wrong']}银两")
def show_status(hero):
"""打印侠客状态卡"""
print(f"\n╔{'═' * 28}╗")
print(f"║ 📋 {hero['name']} 的状态卡")
print(f"║ HP: {max(hero['hp'],0)}/{hero['max_hp']} ATK: {hero['attack']}")
print(f"║ 💰 {hero['silver']} ⚔️ 胜利 {hero['battles_won']}次")
print(f"║ 🎒 背包({len(hero['backpack'])}/10): {', '.join(hero['backpack']) or '空'}")
print(f"╚{'═' * 28}╝")
# --- 主程序 ---
print("=" * 40)
print(" ⚔️ 少年侠客闯江湖 ⚔️")
print("=" * 40)
name = input("\n少年侠客,请赐名:")
hero = {"name": name, "hp": 100, "max_hp": 100, "attack": 15,
"silver": 0, "backpack": [], "battles_won": 0, "adventures": 0}
print(f"\n少年侠客【{hero['name']}】,踏入了江湖...")
print("\n前方出现三条路:")
for idx, (r_name, r_desc) in enumerate(routes, 1):
print(f" {idx}. {r_name} — {r_desc}")
choice = input("请选择路线(1/2/3):")
route_name = routes[int(choice)-1][0] if choice in "123" else routes[0][0]
print(f"\n{hero['name']}踏上了{route_name}...")
for i in range(3):
if hero["hp"] <= 0:
break
print(f"\n--- 第 {i+1} 次遭遇 ---")
if random.choice(["妖怪", "仙人"]) == "妖怪":
m = dict(random.choice(monsters))
if not battle(hero, m):
break
else:
talk_to_npc(hero, random.choice(npcs))
hero["adventures"] += 1
show_status(hero)