python末日文字游戏
时间: 2023-11-20 12:57:50 浏览: 44
以下是一个简单的Python末日文字游戏的例子,其中包括菜单、人物状态和boss等元素。你可以根据自己的需求进行修改和扩展。
```python
import random
# 初始化人物状态
player = {
'name': 'Player',
'health': 100,
'attack': 10,
'defense': 5,
'money': 0
}
# 初始化boss状态
boss = {
'name': 'Boss',
'health': 200,
'attack': 20,
'defense': 10,
'money': 100
}
# 初始化资源
resources = {
'food': 10,
'water': 10,
'medicine': 5,
'weapon': 1
}
# 菜单选项
menu = {
'1': '搜索资源',
'2': '查看人物状态',
'3': '与Boss战斗',
'4': '退出游戏'
}
# 搜索资源函数
def search_resources():
global player, resources
# 随机获取一种资源
resource = random.choice(list(resources.keys()))
# 随机获取资源数量
amount = random.randint(1, 3)
# 增加资源数量
resources[resource] += amount
# 显示搜索结果
print('你找到了{}个{}!'.format(amount, resource))
# 减少人物健康值
player['health'] -= 10
# 查看人物状态函数
def show_player_status():
global player
print('人物状态:')
for key, value in player.items():
print('{}: {}'.format(key, value))
# 与Boss战斗函数
def fight_boss():
global player, boss
print('你与{}展开了激烈的战斗!'.format(boss['name']))
while player['health'] > 0 and boss['health'] > 0:
# 计算双方造成的伤害
player_damage = player['attack'] - boss['defense']
boss_damage = boss['attack'] - player['defense']
# 减少双方健康值
player['health'] -= boss_damage
boss['health'] -= player_damage
# 显示战斗结果
print('你对{}造成了{}点伤害!'.format(boss['name'], player_damage))
print('{}对你造成了{}点伤害!'.format(boss['name'], boss_damage))
if player['health'] <= 0:
print('你被{}打败了!'.format(boss['name']))
else:
print('你打败了{}!'.format(boss['name']))
# 增加人物金钱
player['money'] += boss['money']
# 游戏主循环
while True:
# 显示菜单
print('菜单:')
for key, value in menu.items():
print('{} - {}'.format(key, value))
# 获取用户输入
choice = input('请选择菜单选项:')
# 处理用户输入
if choice == '1':
search_resources()
elif choice == '2':
show_player_status()
elif choice == '3':
fight_boss()
elif choice == '4':
print('游戏结束!')
break
else:
print('无效的菜单选项!')
```