python狼人杀小游戏9人单机版(待改进)

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("无效选项,请重新输入。")
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是一个简单的 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("无效选项,请重新输入。") ``` 这个游戏使用了传统的狼人杀规则,有狼人、村民、预言家、女巫和猎人等角色。在游戏中,玩家需要通过投票来决定谁是狼人,谁是村民。每个角色都有自己的特殊能力,在游戏中起到不同的作用。 这个程序使用了简单的文字界面,玩家需要通过输入数字来做出选择。游戏逻辑比较简单,但可以作为一个基础来扩展更多的角色和游戏规则。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值