每日一个shell脚本之钓鱼游戏练手
#!/usr/bin/bash
# **************************************
# CSDN: M乔木
# qq邮箱: 2776617348@qq.com
# 解释器: 这是一个shell脚本
# **************************************# 游戏初始化
fishing_rod="普通钓竿"
bait="虫子"
score=0
# 主循环
while true; do# 显示游戏进度echo "---------------------------"echo "当前分数:[${score}]"echo "请选择操作:"echo "1. 更换钓竿"echo "2. 更换鱼饵"echo "3. 开始钓鱼"echo "4. 退出游戏"echo "---------------------------"read -p "请输入操作序号:" action# 处理玩家操作case $action in1)echo "---------------------------"echo "请选择钓竿:"echo "1. 普通钓竿"echo "2. 高级钓竿"echo "---------------------------"read -p "请输入钓竿序号:" rodif [ $rod -eq 1 ]; thenfishing_rod="普通钓竿"elif [ $rod -eq 2 ]; thenfishing_rod="高级钓竿"fi;;2)echo "---------------------------"echo "请选择鱼饵:"echo "1. 虫子"echo "2. 面包"echo "---------------------------"read -p "请输入鱼饵序号:" baitif [ $bait -eq 1 ]; thenbait="虫子"elif [ $bait -eq 2 ]; thenbait="面包"fi;;3)# 生成随机事件fish_type=$((RANDOM % 3))fish_size=$((RANDOM % 10 + 1))# 计算成功率success_rate=0if [ $fishing_rod == "普通钓竿" ]; thensuccess_rate=50elif [ $fishing_rod == "高级钓竿" ]; thensuccess_rate=90fiif [ $bait == "虫子" ]; thensuccess_rate=$((success_rate + 10))elif [ $bait == "面包" ]; thensuccess_rate=$((success_rate - 10))fi# 判断是否成功if [ $((RANDOM % 100)) -lt $success_rate ]; thenclearecho "[恭喜你,成功捕获了一条${fish_type}号鱼,大小为${fish_size}!]"score=$((score + fish_size))elseclearecho "[很遗憾,未能捕获到鱼]"fi;;4)echo "游戏结束 最终得分:[${score}]"exit 0;;*)echo "[无效的操作,请重新输入]";;esac
done
新建一个.sh的文件,复制进去运行即可!不会具体怎么操作的可以去我前面的文章学习掌握一下!
点赞关注转发感谢!