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

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

今天给大家分享一个linux系统下一个简单的小游戏
linux系统下农场种菜小游戏!_第1张图片
源码如下,在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

一键三连哦

你可能感兴趣的:(日常分享,linux,运维,服务器,python)