py文字游戏(5个)

python!

1.

import random

def explore():
    print("你进入了一个神秘的洞穴...")
    print("你面前有两个通道,你要选择左边的通道还是右边的通道?(输入 '左' 或 '右')")
    choice = input().lower()

    if choice == '左':
        print("你进入了左边的通道...")
        left_path()
    elif choice == '右':
        print("你进入了右边的通道...")
        right_path()
    else:
        print("请输入有效的选择!")
        explore()

def left_path():
    print("你来到了一个宝箱前...")
    print("你要打开宝箱吗?(输入 '是' 或 '否')")
    choice = input().lower()

    if choice == '是':
        print("你打开了宝箱,发现了一束闪闪发光的宝石!你获得了胜利!")
    elif choice == '否':
        print("你决定不打开宝箱,但突然洞穴开始颤抖...")
        print("天花板塌落下来,你被埋在了石头下,游戏失败!")
    else:
        print("请输入有效的选择!")
        left_path()

def right_path():
    print("你遇到了一只凶恶的怪兽...")
    print("你要和怪兽战斗吗?(输入 '是' 或 '否')")
    choice = input().lower()

    if choice == '是':
        print("你拔出了武器,与怪兽展开激烈的战斗...")
        if random.randint(0, 1) == 0:
            print("你英勇地击败了怪兽,获得了胜利!")
        else:
            print("怪兽太强大了,你被击败了,游戏失败!")
    elif choice == '否':
        print("你决定逃跑,但怪兽追了上来...")
        print("你被怪兽追到了悬崖边,你掉了下去,游戏失败!")
    else:
        print("请输入有效的选择!")
        right_path()

# 游戏开始
print("欢迎来到探险游戏!")
explore()

2.

import random

def hangman():
    print("欢迎来到猜单词游戏!")
    print("我已经选择了一个单词,你需要猜出这个单词是什么。")
    words = ["apple", "banana", "orange", "watermelon", "pineapple"]
    word = random.choice(words)
    guessed_letters = []
    attempts = 6

    while attempts > 0:
        print("\n可用尝试次数:", attempts)
        print("已猜测到的字母:", " ".join(guessed_letters))

        hanged_word = ""
        for letter in word:
            if letter in guessed_letters:
                hanged_word += letter
            else:
                hanged_word += "_"

        print("猜测单词:", hanged_word)

        if hanged_word == word:
            print("恭喜你猜对了!")
            break

        guess = input("请输入你的猜测字母: ").lower()

        if len(guess) != 1 or not guess.isalpha():
            print("请输入有效的字母!")
            continue
        
        if guess in guessed_letters:
            print("你已经猜过这个字母了,请再试一次。")
            continue
        
        guessed_letters.append(guess)

        if guess not in word:
            attempts -= 1
            print("很遗憾,你猜错了。")

        if attempts == 0:
            print("你的尝试次数用尽了,游戏结束。正确答案是", word)
            break

    play_again()

def play_again():
    print("你想再玩一次吗?(输入 '是' 或 '否')")
    choice = input().lower()

    if choice == '是':
        hangman()
    elif choice == '否':
        print("谢谢游玩!再见!")
    else:
        print("请输入有效的选择!")
        play_again()

# 游戏开始
hangman()

3.

import random
class Planet:
def init(self, name, description):
self.name = name
self.description = description
class Task:
def init(self, name, description):
self.name = name
self.description = description
def game():
# 创建星球
planets = []
planets.append(Planet("地球", "一个富饶美丽的星球"))
planets.append(Planet("火星", "一个干燥冷漠的星球"))
planets.append(Planet("木星", "一个巨大气候恶劣的星球"))


# 创建任务
tasks = []
tasks.append(Task("采集样本", "在星球上采集一些植物样本"))
tasks.append(Task("寻找资源", "寻找并采集一些宝贵资源"))
tasks.append(Task("解密密码", "解密一道星球上的密码"))

# 开始游戏
print("欢迎来到宇宙探险游戏!")
print("你将在宇宙中探索不同的星球,并完成相应的任务。")
print("输入 'q' 可以退出游戏。")

while True:
    # 随机选择一个星球和任务
    planet = random.choice(planets)
    task = random.choice(tasks)

    # 输出星球信息和任务要求
    print("你来到了一个新的星球 - " + planet.name)
    print("这里是" + planet.description)
    print("你的任务是:" + task.name)
    print(task.description)

    # 等待玩家输入
    choice = input("输入 'a' 表示接受任务,输入 'n' 表示不接受任务继续探索:")

    if choice.lower() == 'a':
        print("你接受了任务,开始行动!")
        # TODO:根据任务要求编写相应的行动逻辑
        # ...

    elif choice.lower() == 'n':
        print("你放弃了任务,继续探索其他星球。")

    elif choice.lower() == 'q':
        print("谢谢你的参与,欢迎下次再来!")
        break

    else:
        print("无效的输入,请重新输入。")
game()

4. 

game_map = {
'起始点': {
'description': '你现在处于一个神秘的起始点,四周一片漆黑。',
'directions': {
'北': '山洞',
'东': '河流',
'西': '森林'
}
},
'山洞': {
'description': '你来到了一个黑暗的山洞,洞口尽是尖锐的岩石。',
'directions': {
'南': '起始点'
}
},
'河流': {
'description': '你来到了一条湍急的河流,河水冰冷刺骨。',
'directions': {
'西': '起始点'
}
},
'森林': {
'description': '你进入了一座幽深的森林,树木茂密。',
'directions': {
'东': '起始点',
'南': '山洞'
}
}
}

def game():
current_location = '起始点'  # 当前所在位置


print("欢迎来到文字探险游戏!")
print("你需要在这个神秘的世界中寻找宝藏。")
print("输入 'q' 可以退出游戏。")

while True:
    # 输出当前位置的描述信息
    print("你现在处于" + current_location)
    print(game_map[current_location]['description'])

    # 输出可前进的方向
    directions = game_map[current_location]['directions']
    print("可前进方向:")
    for direction in directions:
        print(direction)

    # 等待玩家输入
    choice = input("请输入你的选择:")

    if choice.lower() == 'q':
        print("谢谢你的参与,欢迎下次再来!")
        break

    if choice not in directions:
        print("无效的选择,请重新输入。")
        continue

    # 前进到下一个位置
    current_location = directions[choice]
game()

4.5

import random

# 定义AI回复的语句列表
greetings = ['你好', '哈喽', '嗨']
questions = ['你喜欢什么颜色?', '你最喜欢的食物是什么?', '你的爱好是什么?']
responses = ['我喜欢蓝色', '我最喜欢的食物是披萨', '我喜欢画画']

# 定义AI的回复函数
def get_response(user_input):
    if user_input in greetings:
        return random.choice(greetings)
    elif '?' in user_input:
        return random.choice(questions)
    else:
        return random.choice(responses)

# 主循环
while True:
    user_input = input("你: ")
    ai_response = get_response(user_input)
    print("AI: ", ai_response)

5.

while True:
    # 显示命令提示符
    command = input("$ ")

    # 判断输入的命令
    if command == "exit":
        # 退出循环
        break
    elif command == "help":
        # 显示帮助信息
        print("这是一个简单的命令提示符示例。可用命令:\n"
              " - help: 显示帮助信息\n"
              " - exit: 退出命令提示符")

    else:
        # 未知命令
        print("未知命令,请输入 'help' 获取帮助信息")

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值