linux系统下农场种菜小游戏!

linux系统下农场种菜小游戏!

今天给大家分享一个linux系统下一个简单的小游戏
在这里插入图片描述
源码如下,在linux系统下创建一个.sh的脚本文件,复制粘贴进去即可!

#!/bin/bash

# 初始化变量
vegetables=("生菜" "西兰花" "萝卜")
inventory=()
profit=0

# 显示菜单
show_menu() {
    echo "欢迎来到农场游戏!"
    echo "1.种菜"
    echo "2.收菜"
    echo "3.卖菜"
    echo "4.查看收益"
    echo "5.查看仓库"
    echo "6.退出"
}

# 种植蔬菜
plant_vegetable() {
    echo "请选择要种植的蔬菜:"
    for i in "${!vegetables[@]}"; do
        echo "$((i+1)). ${vegetables[$i]}"
    done
    read -p "输入序列号: " choice
    if [ $choice -ge 1 ] && [ $choice -le ${#vegetables[@]} ]; then
        inventory+=(${vegetables[$((choice-1))]})
        echo "已成功种植${vegetables[$((choice-1))]}。"
    else
        echo "无效的选择,请重新输入。"
    fi
}

# 收获蔬菜
harvest_vegetable() {
    echo "请选择要收获的蔬菜:"
    for i in "${!inventory[@]}"; do
        echo "$((i+1)). ${inventory[$i]}"
    done
    read -p "输入序列号: " choice
    if [ $choice -ge 1 ] && [ $choice -le ${#inventory[@]} ]; then
        profit=$((profit + 10))
        inventory=("${inventory[@]:0:$choice-1}" "${inventory[@]:$((choice))}")
        echo "已成功收获${inventory[$((choice-1))]}。"
    else
        echo "无效的选择,请重新输入。"
    fi
}

# 出售蔬菜
sell_vegetable() {
    echo "请选择要出售的蔬菜:"
    for i in "${!inventory[@]}"; do
        echo "$((i+1)). ${inventory[$i]}"
    done
    read -p "输入序列号: " choice
    if [ $choice -ge 1 ] && [ $choice -le ${#inventory[@]} ]; then
        profit=$((profit + 20))
        inventory=("${inventory[@]:0:$choice-1}" "${inventory[@]:$((choice))}")
        echo "已成功出售${inventory[$((choice-1))]}。"
    else
        echo "无效的选择,请重新输入。"
    fi
}

# 查看收益
check_profit() {
    echo "当前收益为:$profit"
}

# 查看仓库
check_inventory() {
    echo "当前仓库中的蔬菜有:"
    for vegetable in "${inventory[@]}"; do
        echo "$vegetable"
    done
}

# 主循环
while true; do
    show_menu
    read -p "请输入操作序号: " choice
    case $choice in
        1)
            plant_vegetable
            ;;
        2)
            harvest_vegetable
            ;;
        3)
            sell_vegetable
            ;;
        4)
            check_profit
            ;;
        5)
            check_inventory
            ;;
        6)
            echo "感谢使用农场游戏,再见!"
            exit 0
            ;;
        *)
            echo "无效的选择,请重新输入。"
            ;;
    esac
done

一键三连哦

  • 20
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值