帮我写一条魔塔游戏的代码
时间: 2023-08-20 15:27:54 浏览: 137
好的,以下是一个简单的魔塔游戏代码示例:
```python
import random
player = {
"name": "",
"hp": 100,
"attack": 10,
"defend": 5,
"gold": 0,
"items": []
}
monster_list = [
{"name": "小蝙蝠", "hp": 50, "attack": 8, "defend": 2, "gold": 5},
{"name": "骷髅兵", "hp": 80, "attack": 10, "defend": 5, "gold": 10},
{"name": "兽人战士", "hp": 120, "attack": 15, "defend": 8, "gold": 20},
{"name": "巨型蜘蛛", "hp": 150, "attack": 20, "defend": 10, "gold": 30},
{"name": "魔王", "hp": 1000, "attack": 50, "defend": 30, "gold": 100},
]
def show_menu():
print("欢迎进入魔塔游戏")
print("1. 开始游戏")
print("2. 查看角色")
print("3. 商店")
print("4. 退出游戏")
def start_game():
player["name"] = input("请输入你的名字:")
print("你进入了魔塔,开始冒险!")
while True:
monster = random.choice(monster_list)
print("你遇到了一只%s,准备战斗!" % monster["name"])
while True:
input("按回车键攻击!")
player_att = player["attack"] + random.randint(0, 10)
monster_def = monster["defend"] + random.randint(0, 5)
damage = player_att - monster_def
if damage <= 0:
print("你的攻击被%s抵挡了!" % monster["name"])
else:
monster["hp"] -= damage
print("你对%s造成了%d点伤害!" % (monster["name"], damage))
if monster["hp"] <= 0:
print("你打败了%s,获得了%d金币!" % (monster["name"], monster["gold"]))
player["gold"] += monster["gold"]
break
monster_att = monster["attack"] + random.randint(0, 5)
player_def = player["defend"] + random.randint(0, 3)
damage = monster_att - player_def
if damage <= 0:
print("%s的攻击被你抵挡了!" % monster["name"])
else:
player["hp"] -= damage
print("%s对你造成了%d点伤害!" % (monster["name"], damage))
if player["hp"] <= 0:
print("你被%s打败了,游戏结束!" % monster["name"])
return
def show_player():
print("角色名字:%s" % player["name"])
print("生命值:%d" % player["hp"])
print("攻击力:%d" % player["attack"])
print("防御力:%d" % player["defend"])
print("金币:%d" % player["gold"])
print("物品列表:%s" % player["items"])
def show_shop():
print("欢迎来到商店,以下是商品列表:")
print("1. 铁剑(攻击力+5)- 10金币")
print("2. 铁盾(防御力+5)- 10金币")
while True:
choice = input("请选择要购买的商品编号(按0返回):")
if choice == "1":
if player["gold"] >= 10:
player["gold"] -= 10
player["attack"] += 5
print("购买成功,攻击力增加5!")
else:
print("金币不足,无法购买!")
elif choice == "2":
if player["gold"] >= 10:
player["gold"] -= 10
player["defend"] += 5
print("购买成功,防御力增加5!")
else:
print("金币不足,无法购买!")
elif choice == "0":
break
else:
print("无效的选择,请重新选择!")
while True:
show_menu()
choice = input("请选择操作:")
if choice == "1":
start_game()
elif choice == "2":
show_player()
elif choice == "3":
show_shop()
elif choice == "4":
print("退出游戏,再见!")
break
else:
print("无效的选择,请重新选择!")
```
这个代码实现了一个简单的魔塔游戏,玩家可以在游戏中遇到各种怪物并与其战斗。玩家可以通过不断战斗获取金币并在商店中购买装备来提升自己的属性。
阅读全文