以下是一个简单的捕鱼游戏的代码示例,使用 Python 语言编写:

import random

# 定义鱼的种类和价格
fishes = {'鲤鱼': 10, '草鱼': 20, '鲫鱼': 30, '鲨鱼': 100}

# 初始化玩家的金币数量和背包
coins = 100
bag = {}

# 游戏循环
while True:
    # 打印当前金币数量和背包内容
    print('当前金币数量:', coins)
    print('当前背包:', bag)
    
    # 让玩家选择是否继续捕鱼
    choice = input('继续捕鱼吗?(y/n)')
    if choice == 'n':
        print('游戏结束,感谢您的参与!')
        break
    
    # 随机生成一条鱼
    fish_name = random.choice(list(fishes.keys()))
    fish_price = fishes[fish_name]
    
    # 计算是否捕到鱼,更新金币数量和背包
    if random.random() < 0.5:
        print('恭喜您捕到了一条', fish_name, ',价值', fish_price, '金币!')
        coins += fish_price
        if fish_name in bag:
            bag[fish_name] += 1
        else:
            bag[fish_name] = 1
    else:
        print('很遗憾,您这次没有捕到鱼。')

这段代码实现了一个简单的捕鱼游戏,每次循环会随机生成一条鱼,并让玩家选择是否继续捕鱼。如果捕到鱼,会增加金币数量和背包内容,否则不做任何操作。


原文地址: https://www.cveoy.top/t/topic/mWXF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录