linux 贪吃蛇终极脚本,Shell编程练习 贪吃蛇

#!/bin/bash

#

declare -i x=5

declare -i y=5

declare -i len=4

declare -i x_arr=(1 2 3 4 5)

declare -i y_arr=(5 5 5 5 5)

dir='d'

init() {

clear

echo -e "\033[0m"

echo -e "\033[4${2}m"

echo -e "\033[4${1}m"

echo -e "\033[?25l"

gen_food

}

set_show() {

clear

for ((i = 1; i < $len; i++)); do

echo -e "\033[${y_arr[$i]};${x_arr[$i]}H*\033[0m"

done

echo -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++)); do

echo -e "\033[20;${i}H@\033[0m"

echo -e "\033[${i};20H@\033[0m"

done

echo -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 ;;

esac

if [[ $x -eq $randomx && $y -eq $randomy ]]; then

((len++))

x_arr[$len]=$x

y_arr[$len]=$y

gen_food

return

fi

if [[ $x -lt 1 || $y -lt 1 || $x -gt 19 || $y -gt 19 ]]; then

exit;

fi

for ((i = 2; i <= $len; i++)); do

if [[ $x -eq ${x_arr[$i]} && $y -eq ${y_arr[$i]} ]]; then

exit;

fi

done

for ((i = 0; i <= $len; i++)); do

x_arr[$i]=${x_arr[$i+1]}

y_arr[$i]=${y_arr[$i+1]}

done

x_arr[$len]=$x

y_arr[$len]=$y

}

gen_food() {

let flag=1

while [ $flag -eq 1 ]; do

let randomx=$(($RANDOM%18+1))

let randomy=$(($RANDOM%18+1))

for ((i=0;i

if [[ ${x_arr[$i]} -eq $randomx && ${y_arr[$i]} -eq $randomy ]]; then

break

fi

done

if [ $i -eq $len ]; then

flag=0

fi

done

}

init

set_show

while :; do

olddir=$dir

if ! read -n 1 -t 0.5 -s dir; then

dir=$olddir

fi

sleep 0.0001

move

set_show

done

echo -e "\033[0m"

1b6227f633364de8f9116b132b76e356.png

wsad移动 q退出

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值