python丧尸小游戏(纯文字版)

import random
from termcolor import colored

def display_intro():
    print(colored("欢迎来到丧尸小游戏!", "yellow"))
    print(colored("你发现自己被困在一个充满丧尸的城市中。", "yellow"))
    print(colored("你的任务是存活下去。", "yellow"))
    print()

def choose_action():
    print("请选择你的行动:")
    print("1. 搜寻武器")
    print("2. 搜寻食物")
    print("3. 搜寻药品")
    print("4. 搜寻避难所")
    print("5. 逃跑")
    action = input("你的选择是:")
    print()
    return action

def search_weapon():
    print(colored("你搜寻周围的建筑物,找到了一把武器。", "green"))
    print(colored("你拿起武器,准备应对丧尸的袭击。", "green"))
    print()

def search_food():
    print(colored("你四处寻找食物,找到了一些干粮。", "green"))
    print(colored("你吃了一些干粮,恢复了一些体力。", "green"))
    print()

def search_medkit():
    print(colored("你找到了一个药品箱。", "green"))
    chance = random.randint(1, 10)
    if chance <= 7:
        print(colored("你打开药品箱,发现了一瓶急救药。", "green"))
        print(colored("你使用急救药,回复了一些生命值。", "green"))
    else:
        print(colored("你打开药品箱,但里面是空的。", "red"))
    print()

def search_shelter():
    print(colored("你寻找避难所,终于找到了一个废弃的建筑物。", "green"))
    print(colored("你进入避难所,暂时躲避了丧尸的追击。", "green"))
    print()

def escape():
    print(colored("你试图逃离城市,但丧尸们发现了你的行踪。", "red"))
    print(colored("你被追赶并被丧尸们围困。", "red"))
    print(colored("很不幸,你没有成功逃脱。", "red"))
    print(colored("游戏结束!", "red"))
    print()

def random_event():
    event = random.randint(1, 4)
    if event == 1:
        print(colored("你听到了远处传来的丧尸嚎叫声。", "blue"))
    elif event == 2:
        print(colored("你发现了一群丧尸正在远处游荡。", "blue"))
    elif event == 3:
        print(colored("你遭遇了一个友好的幸存者,他给了你一些食物。", "blue"))
    elif event == 4:
        print(colored("你发现了一个被丧尸围困的建筑物,里面可能有宝贵的物品。", "blue"))
    print()

def play_game():
    display_intro()
    health = 100
    while True:
        print(colored("你的当前生命值:", "yellow"), colored(health, "red"))
        action = choose_action()
        if action == "1":
            search_weapon()
        elif action == "2":
            search_food()
        elif action == "3":
            search_medkit()
        elif action == "4":
            search_shelter()
        elif action == "5":
            escape()
            break
        else:
            print(colored("无效的选择,请重新输入。", "red"))
            continue
        
        random_event()
        
        # 每次行动后,有一定几率遭遇丧尸攻击
        zombie_chance = random.randint(1, 10)
        if zombie_chance <= 3:
            print(colored("你遭遇了一群丧尸!", "red"))
            damage = random.randint(10, 30)
            health -= damage
            print(colored("你受到了", "red"), colored(damage, "red"), colored("点伤害。", "red"))
            if health <= 0:
                print(colored("你的生命值耗尽,被丧尸们吞噬。", "red"))
                print(colored("游戏结束!", "red"))
                break
            else:
                print(colored("你成功逃脱了丧尸的追击。", "green"))
                print()
        else:
            print(colored("你没有遇到丧尸,继续前进。", "green"))
            print()

play_game()

给个赞吧!制作不易。

你可能感兴趣的:(python入门教程,整活系列,python,开发语言)