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[@]}"; doecho "$((i+1)). ${vegetables[$i]}"doneread -p "输入序列号: " choiceif [ $choice -ge 1 ] && [ $choice -le ${#vegetables[@]} ]; theninventory+=(${vegetables[$((choice-1))]})echo "已成功种植${vegetables[$((choice-1))]}。"elseecho "无效的选择,请重新输入。"fi
}# 收获蔬菜
harvest_vegetable() {echo "请选择要收获的蔬菜:"for i in "${!inventory[@]}"; doecho "$((i+1)). ${inventory[$i]}"doneread -p "输入序列号: " choiceif [ $choice -ge 1 ] && [ $choice -le ${#inventory[@]} ]; thenprofit=$((profit + 10))inventory=("${inventory[@]:0:$choice-1}" "${inventory[@]:$((choice))}")echo "已成功收获${inventory[$((choice-1))]}。"elseecho "无效的选择,请重新输入。"fi
}# 出售蔬菜
sell_vegetable() {echo "请选择要出售的蔬菜:"for i in "${!inventory[@]}"; doecho "$((i+1)). ${inventory[$i]}"doneread -p "输入序列号: " choiceif [ $choice -ge 1 ] && [ $choice -le ${#inventory[@]} ]; thenprofit=$((profit + 20))inventory=("${inventory[@]:0:$choice-1}" "${inventory[@]:$((choice))}")echo "已成功出售${inventory[$((choice-1))]}。"elseecho "无效的选择,请重新输入。"fi
}# 查看收益
check_profit() {echo "当前收益为:$profit"
}# 查看仓库
check_inventory() {echo "当前仓库中的蔬菜有:"for vegetable in "${inventory[@]}"; doecho "$vegetable"done
}# 主循环
while true; doshow_menuread -p "请输入操作序号: " choicecase $choice in1)plant_vegetable;;2)harvest_vegetable;;3)sell_vegetable;;4)check_profit;;5)check_inventory;;6)echo "感谢使用农场游戏,再见!"exit 0;;*)echo "无效的选择,请重新输入。";;esac
done
一键三连哦