python狼人杀文字小游戏
时间: 2023-06-21 18:16:15 浏览: 50
这是一个简单的 Python 狼人杀文字小游戏,让玩家扮演村民和狼人,通过投票来决定谁是狼人,谁是村民。
```python
import random
import time
# 游戏角色
ROLES = {
"werewolf": "狼人",
"villager": "村民",
"prophet": "预言家",
"witch": "女巫",
"hunter": "猎人"
}
# 游戏流程
def werewolf_game():
# 随机分配角色
roles = list(ROLES.keys())
random.shuffle(roles)
players = {}
for i in range(1, 7):
players[i] = roles[i-1]
print("游戏开始!")
# 狼人和预言家先行动
werewolves = [k for k, v in players.items() if v == "werewolf"]
prophet = [k for k, v in players.items() if v == "prophet"][0]
print("狼人请睁眼,你们的同伴是:", werewolves)
time.sleep(3)
print("预言家请睁眼,选择要查验的玩家编号:")
prophet_choice = int(input())
# 女巫行动
witch = [k for k, v in players.items() if v == "witch"]
if witch:
witch = witch[0]
print("女巫请睁眼,选择是否使用解药,救哪个玩家?0表示不救,输入玩家编号:")
antidote_choice = int(input())
print("女巫请睁眼,选择是否使用毒药,毒哪个玩家?0表示不毒,输入玩家编号:")
poison_choice = int(input())
else:
antidote_choice = 0
poison_choice = 0
# 白天投票
print("天亮了,现在开始投票:")
vote = {}
for k in players.keys():
vote[k] = 0
for i in range(1, 7):
print("玩家{}请投票,输入要投出的玩家编号:".format(i))
choice = int(input())
vote[choice] += 1
max_vote = max(vote.values())
vote_results = [k for k, v in vote.items() if v == max_vote]
if len(vote_results) == 1:
print("玩家{}出局。".format(vote_results[0]))
del players[vote_results[0]]
else:
print("得票最多的玩家有:", vote_results)
time.sleep(3)
print("请得票最多的玩家之间再次投票:")
vote = {}
for k in vote_results:
vote[k] = 0
for i in range(1, 7):
print("玩家{}请投票,输入要投出的玩家编号:".format(i))
choice = int(input())
if choice in vote:
vote[choice] += 1
max_vote = max(vote.values())
vote_results = [k for k, v in vote.items() if v == max_vote]
if len(vote_results) == 1:
print("玩家{}出局。".format(vote_results[0]))
del players[vote_results[0]]
else:
print("得票最多的玩家有:", vote_results)
print("无人出局,继续游戏。")
# 游戏结束条件
werewolves_left = len([v for v in players.values() if v == "werewolf"])
villagers_left = len([v for v in players.values() if v == "villager"])
if werewolves_left == 0:
print("游戏结束,村民胜利!")
elif werewolves_left >= villagers_left:
print("游戏结束,狼人胜利!")
# 主程序
while True:
print("欢迎来到狼人杀游戏!")
print("请选择:1 - 开始游戏,2 - 退出游戏")
choice = int(input())
if choice == 1:
werewolf_game()
elif choice == 2:
print("谢谢游玩,再见!")
break
else:
print("无效选项,请重新输入。")
```
这个游戏使用了传统的狼人杀规则,有狼人、村民、预言家、女巫和猎人等角色。在游戏中,玩家需要通过投票来决定谁是狼人,谁是村民。每个角色都有自己的特殊能力,在游戏中起到不同的作用。
这个程序使用了简单的文字界面,玩家需要通过输入数字来做出选择。游戏逻辑比较简单,但可以作为一个基础来扩展更多的角色和游戏规则。
相关推荐

















