import random import time from random import randint, choice import json # 游戏角色 ROLES = { "werewolf1": "狼人", "werewolf2": "狼人", "werewolf3": "狼人", "villager1": "村民", "villager2": "村民", "villager3": "村民", "prophet": "预言家", "witch": "女巫", "hunter": "猎人" } # 游戏流程 def werewolf_game(): # 随机分配角色 roles = list(ROLES.keys()) random.shuffle(roles) players = {} shenfen={} for i in range(1, 10): players[i] = roles[i - 1] print("游戏开始!") print(f"选择你的位置:") play_choice = int(input()) for i in players: shenfen[i] = ROLES[roles[i - 1]] print(f"身份是{shenfen}") print(players) for i in players: if play_choice == i: player_look = ROLES[roles[i - 1]] print("\n你的身份是" + player_look) nvwuhuozhe = True nvwuyouduyao = True nvwujiuming = True lierenkaiqiang=True du = 10 duyao = 0 chuju = 0 days = 0 ######print(ap) while True: werewolves_left = len([v for v in players.values() if v == "werewolf1" or v == "werewolf2" or v == "werewolf3"]) villagers_left = len([v for v in players.values() if v == "villager1"or v == "villager2" or v == "villager3"]) shenzhi_left = len([v for v in players.values() if v == "prophet" or v == "witch" or v == "hunter"]) print(werewolves_left) print(villagers_left) print(shenzhi_left) if werewolves_left == 0: print("游戏结束,村民胜利!") break elif shenzhi_left==0: print("游戏结束,狼人胜利!") break elif len(players)==2 and werewolves_left>0: print("游戏结束,狼人胜利!") break days += 1 print("\t\t\t\t\t\t第" + str(days) + "天~~~~~~~~~~~~~~~") huozhe = list(players.keys()) print(f"场上剩余玩家为{huozhe}") qiangkou=[k for k , v in players.items() if v != "hunter" ] qiangkou=[k for k in qiangkou if k in huozhe] # c = ','.join(players[]) print("天黑请闭眼") # 狼人和预言家先行动 time.sleep(2) werewolves = [k for k, v in players.items() if v == "werewolf1" or v == "werewolf2" or v == "werewolf3"] if play_choice in werewolves: print(f"狼人请睁眼,你们的同伴是: {werewolves},选择要杀害的玩家编号:") werewolves_choice = int(input()) else: print(f"狼人请睁眼,你们的同伴是: ,选择要杀害的玩家编号: ") werewolves_choice=random.sample(huozhe,1)[0] print(werewolves_choice) time.sleep(2) prophet = [k for k, v in players.items() if v == "prophet"] if prophet: prophet = prophet[0] if play_choice == prophet: print(f"预言家请睁眼,你的身份是{prophet}号,选择要查验的玩家编号:") prophet_choice = int(input()) for i in players: if prophet_choice== i: prophet_look= ROLES[roles[i-1]] if prophet_look =="狼人": print(f"他是狼人") else: print(f"他是好人") print(f"预言家闭眼") else: prophet_choice=random.sample(huozhe,1)[0] print(f"预言家请睁眼,你要验谁{prophet_choice}..., 他是...") print(f"预言家闭眼") time.sleep(2) # 女巫行动 witch = [k for k, v in players.items() if v == "witch"] if witch: witch = witch[0] if play_choice==witch: print(f"女巫请睁眼,你的身份是{witch}号") if nvwujiuming: if werewolves_choice == witch: time.sleep(2) print("今天晚上,你死了,你是否要进行自救(输入0为否,输入1为是)") jiuming = int(input()) if jiuming == 1: print("救") nvwujiuming = False chuju = 0 else: if nvwuyouduyao: print("不救,你有一瓶毒药,你是否要毒?(输入0为否,输入1为是)") duyao = int(input()) if duyao == 1: print("要毒谁呢?") # print("要毒谁呢?(输入数字" + c + ")") du = int(input()) nvwuyouduyao = False chuju = werewolves_choice else: print("女巫没有使用毒药") else: print(f"今天晚上,玩家 {werewolves_choice} 死了,你是否要救(输入0为否,输入1为是)") jiuming = int(input()) if jiuming == 1: print("救") nvwujiuming = False chuju = 0 else: if nvwuyouduyao: time.sleep(2) print("你有一瓶毒药,你是否要毒?(输入0为否,输入1为是)") duyao = int(input()) if duyao == 1: print("要毒谁呢?") # print("要毒谁呢?(输入数字" + c + ")") du = int(input()) nvwuyouduyao = False chuju = werewolves_choice else: print("女巫没有使用毒药") chuju = werewolves_choice else: chuju = werewolves_choice else: if nvwuyouduyao: time.sleep(2) print("你有一瓶毒药,你是否要毒?(输入0为否,输入1为是)") duyao = int(input()) if duyao == 1: print("要毒谁呢?") # print("要毒谁呢?(输入数字" + c + ")") du = int(input()) nvwuyouduyao = False chuju = werewolves_choice else: print("女巫没有使用毒药") chuju = werewolves_choice else: chuju = werewolves_choice else: print("女巫请睁眼") if nvwujiuming: print(f"今天晚上,玩家...{werewolves_choice}死了,你是否要救") jiuming = randint(0, 1) print(jiuming) if jiuming == 1: nvwujiuming = False chuju = 0 else: if nvwuyouduyao: print("你有一瓶毒药,你是否要毒?") duyao = randint(0, 1) print(duyao) if duyao == 1: print("要毒谁呢?") # print("要毒谁呢?(输入数字" + c + ")") du = random.sample(huozhe,1)[0] print(du) nvwuyouduyao = False chuju = werewolves_choice else: chuju = werewolves_choice print("女巫请闭眼") else: if nvwuyouduyao: print("你有一瓶毒药,你是否要毒?") duyao = randint(0, 1) print(duyao) if duyao == 1: print("要毒谁呢?") # print("要毒谁呢?(输入数字" + c + ")") du = random.sample(huozhe,1)[0] print(du) nvwuyouduyao = False chuju = werewolves_choice else: print("女巫请闭眼") chuju = werewolves_choice else: chuju = werewolves_choice else: chuju = werewolves_choice print("女巫请闭眼") time.sleep(2) # 猎人行动 hunter = [k for k, v in players.items() if v == "hunter"] if hunter: hunter = hunter[0] if play_choice==hunter: print(f"猎人请睁眼,你的身份是{hunter}号") print("猎人闭眼") else: print(f"猎人请睁眼") print("猎人闭眼") time.sleep(2) if werewolves_left == 0: print("游戏结束,村民胜利!") break elif shenzhi_left==0: print("游戏结束,狼人胜利!") break print("天亮了") print(chuju,duyao) if chuju == 0 and duyao == 0: print("昨天没有人死,平安夜") else: if chuju != 0 and duyao == 0: print(f"昨天晚上玩家{werewolves_choice}死了") if werewolves_choice ==hunter and lierenkaiqiang: if play_choice==hunter: print("请选择是否发动技能?(输入0为否,输入1为是)") kaiqiang=int(input()) if kaiqiang==1: print("请选择你要带走的玩家") dairen =int(input()) del players[dairen] print(f"{werewolves_choice}号玩家身份是猎人,猎人选择开枪带走{dairen}号") del players[werewolves_choice] else: del players[werewolves_choice] else: print("请选择是否发动技能?(输入0为否,输入1为是)") kaiqiang = randint(0, 1) if kaiqiang == 1: print("请选择你要带走的玩家") dairen = random.sample(qiangkou,1)[0] if dairen !=hunter: del players[dairen] print(f"{werewolves_choice}号玩家身份是猎人,猎人选择开枪带走{dairen}号") else: print(f"{werewolves_choice}号玩家身份是猎人,猎人选择开枪带走{dairen}号") del players[werewolves_choice] else: del players[werewolves_choice] elif chuju == 0 and duyao == 1: if du ==hunter: lierenkaiqiang=False print(f"昨天晚上玩家{du}死亡") del players[du] duyao=0 else: print(f"昨天晚上玩家{du}死亡") del players[du] elif chuju != 0 and duyao == 1: print(f"昨夜双死{du},{werewolves_choice}死亡") if chuju == hunter and du !=hunter: duyao = 0 if play_choice == hunter: print("请选择是否发动技能?(输入0为否,输入1为是)") kaiqiang = int(input()) if kaiqiang == 1: print("请选择你要带走的玩家") dairen = int(input()) del players[dairen] print(f"{werewolves_choice}号玩家身份是猎人,猎人选择开枪带走{dairen}号") del players[werewolves_choice] else: print("请选择是否发动技能?(输入0为否,输入1为是)") kaiqiang = randint(0, 1) if kaiqiang == 1: print("请选择你要带走的玩家") dairen = random.sample(qiangkou, 1)[0] del players[dairen] print(f"{werewolves_choice}号玩家身份是猎人,猎人选择开枪带走{dairen}号") del players[werewolves_choice] del players[du] else: del players[du] del players[werewolves_choice] elif werewolves_choice==du: print(f"昨天晚上玩家{du}死亡") del players[du] duyao = 0 else: del players[du] del players[werewolves_choice] duyao = 0 # 白天投票 if werewolves_left == 0: print("游戏结束,村民胜利!") break elif shenzhi_left==0: print("游戏结束,狼人胜利!") break elif len(players)==2 and werewolves_left>0: print("游戏结束,狼人胜利!") break print("天亮了,现在开始投票:") time.sleep(2) huozhe=list(players.keys()) # print(shengyu) print(huozhe) vote = {} for k in huozhe: vote[k] = 0 for i in huozhe: if i ==play_choice: print("玩家{}请投票,输入要投出的玩家编号:".format(i)) choice = int(input()) else: choice = random.sample(huozhe,1)[0] print("玩家{}请投票,输入要投出的玩家编号:".format(i),choice) 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])) if vote_results[0] == hunter: if play_choice == hunter: print("请选择是否发动技能?(输入0为否,输入1为是)") kaiqiang = int(input()) if kaiqiang == 1: print("请选择你要带走的玩家") dairen = int(input()) del players[dairen] print(f"{vote_results[0]}号玩家身份是猎人,猎人选择开枪带走{dairen}号") else: del players[vote_results[0]] else: print("请选择是否发动技能?(输入0为否,输入1为是)") kaiqiang = randint(0,1) if kaiqiang == 1: print("请选择你要带走的玩家") dairen = random.sample(qiangkou, 1)[0] del players[dairen] print(f"{vote_results[0]}号玩家身份是猎人,猎人选择开枪带走{dairen}号") else: del players[vote_results[0]] else: 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 huozhe : if i in vote_results: print("玩家{}不能参与投票:".format(i)) else: if i ==play_choice: print("请选择你要投票玩家的编号:") choice = int(input()) else: choice = random.sample(vote_results,1)[0] print("玩家{}请投票,输入要投出的玩家编号:".format(i), choice) # 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])) if vote_results[0] == hunter: if play_choice == hunter: print("请选择是否发动技能?(输入0为否,输入1为是)") kaiqiang = int(input()) if kaiqiang == 1: print("请选择你要带走的玩家") dairen = int(input()) del players[dairen] print(f"{vote_results[0]}号玩家身份是猎人,猎人选择开枪带走{dairen}号") del players[vote_results[0]] else: del players[vote_results[0]] else: print("请选择是否发动技能?(输入0为否,输入1为是)") kaiqiang = randint(0, 1) if kaiqiang == 1: print("请选择你要带走的玩家") dairen = random.sample(qiangkou, 1)[0] del players[dairen] print(f"{vote_results[0]}号玩家身份是猎人,猎人选择开枪带走{dairen}号") del players[vote_results[0]] else: del players[vote_results[0]] else: del players[vote_results[0]] else: print("得票最多的玩家有:", vote_results) print("无人出局,继续游戏。") if werewolves_left == 0: print("游戏结束,村民胜利!") break elif shenzhi_left==0: print("游戏结束,狼人胜利!") break elif len(players)==2 and werewolves_left>0: print("游戏结束,狼人胜利!") break # # 主程序 print("欢迎来到狼人杀游戏!") print("请选择:1 - 开始游戏,2 - 退出游戏") choice = int(input()) if choice == 1: werewolf_game() # elif choice == 2: # print("谢谢游玩,再见!") # break # else: # print("无效选项,请重新输入。")
python狼人杀小游戏9人单机版(待改进)
于 2023-05-31 09:20:39 首次发布