每日一个shell脚本之钓鱼游戏练手
bash
#!/usr/bin/bash
# **************************************
# CSDN: M乔木
# qq邮箱: [email protected]
# 解释器: 这是一个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 in
1)
echo "---------------------------"
echo "请选择钓竿:"
echo "1. 普通钓竿"
echo "2. 高级钓竿"
echo "---------------------------"
read -p "请输入钓竿序号:" rod
if [ $rod -eq 1 ]; then
fishing_rod="普通钓竿"
elif [ $rod -eq 2 ]; then
fishing_rod="高级钓竿"
fi
;;
2)
echo "---------------------------"
echo "请选择鱼饵:"
echo "1. 虫子"
echo "2. 面包"
echo "---------------------------"
read -p "请输入鱼饵序号:" bait
if [ $bait -eq 1 ]; then
bait="虫子"
elif [ $bait -eq 2 ]; then
bait="面包"
fi
;;
3)
# 生成随机事件
fish_type=$((RANDOM % 3))
fish_size=$((RANDOM % 10 + 1))
# 计算成功率
success_rate=0
if [ $fishing_rod == "普通钓竿" ]; then
success_rate=50
elif [ $fishing_rod == "高级钓竿" ]; then
success_rate=90
fi
if [ $bait == "虫子" ]; then
success_rate=$((success_rate + 10))
elif [ $bait == "面包" ]; then
success_rate=$((success_rate - 10))
fi
# 判断是否成功
if [ $((RANDOM % 100)) -lt $success_rate ]; then
clear
echo "[恭喜你,成功捕获了一条${fish_type}号鱼,大小为${fish_size}!]"
score=$((score + fish_size))
else
clear
echo "[很遗憾,未能捕获到鱼]"
fi
;;
4)
echo "游戏结束 最终得分:[${score}]"
exit 0
;;
*)
echo "[无效的操作,请重新输入]"
;;
esac
done
新建一个.sh的文件,复制进去运行即可!不会具体怎么操作的可以去我前面的文章学习掌握一下!
点赞关注转发感谢!