shell小游戏

编程入门 行业动态 更新时间:2024-10-10 13:17:19

shell<a href=https://www.elefans.com/category/jswz/34/1769974.html style=小游戏"/>

shell小游戏

猜数字小游戏

#!/bin/bash
jd=$(expr $RANDOM % 101)
sj=$(expr $RANDOM % 101)
#echo $sj                   #正确答案
echo $jd                    #错误答案
n=1
read -p "请输入你猜的数字:"  szfor((n=1;n<10;))doif [ $sz = $sj ]thenecho "恭喜你猜对了" break                         #猜对了停止for循环elif [ $sz != $sj ]thenecho "恭喜你猜错了。。。。"   #猜错for死循环  Ctrl + c  停止运行fi
done

石头剪刀布

#!/bin/bash
echo "人机游戏欢迎玩                                  规则 :石头 剪刀 布 "
a=`echo "石头"`
b=`echo "剪刀"`
c=`echo "布"`
while true        #true 意思是 真do
pc=( 石头 剪刀 布 )
w=$(expr $RANDOM % 3)o="echo ${pc[$w]}"echo "$o"read -p "输入你想出的 石头 剪刀 布 :" Rif [[ $w -eq 0 && $a = $R ]]thenecho "平局"
elif [[ $w -eq 1 && $b = $R ]]thenecho "平局"
elif [[ $w -eq 2 && $c = $R ]]thenecho "平局"
elif [[ $w -eq 0 && $b = $R ]]thenecho "你输了"
elif [[ $w -eq 1 && $c = $R ]]thenecho "你输了"
elif [[ $w -eq 2 && $a = $R ]]thenecho "你输了"
elif [[ $w -eq 0 && $c = $R ]]thenecho "你赢了"exit
elif [[ $w -eq 1 && $a = $R ]]thenecho "你赢了"exit
elif [[ $w -eq 2 && $b = $R ]]thenecho "你赢了"exitfi
done

贪吃蛇小游戏

#!/bin/bashdeclare -i x=5
declare -i y=5
declare -i len=4declare -i x_arr=(1 2 3 4 5)
declare -i y_arr=(5 5 5 5 5)dir='d'init() {clearecho -e "\033[0m"echo -e "\033[4${2}m"echo -e "\033[4${1}m"echo -e "\033[?25l"gen_food
}set_show() {clearfor ((i = 1; i < $len; i++)); doecho -e "\033[${y_arr[$i]};${x_arr[$i]}H*\033[0m"doneecho -e "\033[${y_arr[$i]};${x_arr[$i]}H$\033[0m"echo -e "\033[${randomy};${randomx}H#\033[0m"for ((i = 0; i <= 20; i++)); doecho -e "\033[20;${i}H@\033[0m"echo -e "\033[${i};20H@\033[0m"doneecho -e "\033[21;0Hsnake:${x},${y}\033[0m"echo -e "\033[22;0Hfood :${randomx},${randomy}\033[0m"
}move() {case $dir in"w") y=$y-1 ;;"s") y=$y+1 ;;"a") x=$x-1 ;;"q") exit ;;*) x=$x+1 ;;esacif [[ $x -eq $randomx && $y -eq $randomy ]]; then((len++))x_arr[$len]=$xy_arr[$len]=$ygen_foodreturnfiif [[ $x -lt 1 || $y -lt 1 || $x -gt 19 || $y -gt 19 ]]; thenexit;fifor ((i = 2; i <= $len; i++)); doif [[ $x -eq ${x_arr[$i]} && $y -eq ${y_arr[$i]} ]]; thenexit;fidonefor ((i = 0; i <= $len; i++)); dox_arr[$i]=${x_arr[$i+1]}y_arr[$i]=${y_arr[$i+1]}donex_arr[$len]=$xy_arr[$len]=$y
}gen_food() {let flag=1while [ $flag -eq 1 ]; dolet randomx=$(($RANDOM%18+1))let randomy=$(($RANDOM%18+1))for ((i=0;i<len;i++)); doif [[ ${x_arr[$i]} -eq $randomx && ${y_arr[$i]} -eq $randomy ]]; thenbreakfidoneif [ $i -eq $len ]; thenflag=0fidone
}init
set_show
while :; doolddir=$dirif ! read -n 1 -t 0.5 -s dir; thendir=$olddirfisleep 0.0001moveset_show
doneecho -e "\033[0m"

更多推荐

shell小游戏

本文发布于:2024-03-23 02:10:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小游戏   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!