【LINUX】SHELL贪吃蛇

MapHeight=19

MapWidth=17

SnakeLength=3

SnakeX=(2 3 4)

SnakeY=(5 5 5)

MoveDirection="Right"

Food=(7 14)

Map=(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9)

Show()

{

#Map

for Cell in "${Map[@]}"

do

case ${Cell} in

  1. printf " " ;;

  2. printf "#" ;;

  3. printf "\n" ;;

esac

done

#Snake

for ((i=0;i<${SnakeLength};i++))

do

tput cup ${SnakeY[i]} ${SnakeX[i]}

echo "*"

done

#Food

tput cup ${Food[0]} ${Food[1]}

echo "0"

tput cup 20 0

}

ArrayMove()

{

for i in $(seq 0 $((SnakeLength-2)))

do

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

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

done

}

FoodGenerate()

{

Food[1]=(({RANDOM}%${MapHeight}))

Food[0]=(({RANDOM}%${MapWidth}))

}

Move()

{

read -n 1 -t 0.2 input

case $input in

"s") MoveDirection="Down" ;;

"w") MoveDirection="Up" ;;

"a") MoveDirection="Left" ;;

"d") MoveDirection="Right" ;;

esac

ArrayMove

if [ "$MoveDirection" = "Right" ]; then

((++SnakeX[$((SnakeLength-1))]))

elif [ "$MoveDirection" = "Left" ]; then

((--SnakeX[$((SnakeLength-1))]))

elif [ "$MoveDirection" = "Up" ]; then

((--SnakeY[$((SnakeLength-1))]))

elif [ "$MoveDirection" = "Down" ]; then

((++SnakeY[$((SnakeLength-1))]))

fi

if [ {SnakeX\[((SnakeLength-1))]} -eq ${Food[1]} -a {SnakeY\[((SnakeLength-1))]} -eq ${Food[0]} ]; then

SnakeX[{SnakeLength}\]={SnakeX[$((SnakeLength-1))]}

SnakeY[{SnakeLength}\]={SnakeY[$((SnakeLength-1))]}

if [ "$MoveDirection" = "Down" ]; then

SnakeY[{SnakeLength}\]={SnakeY[$((SnakeLength-1))]}+1

elif [ "$MoveDirection" = "Up" ]; then

SnakeY[{SnakeLength}\]={SnakeY[$((SnakeLength-1))]}-1

elif [ "$MoveDirection" = "Right" ]; then

SnakeX[{SnakeLength}\]={SnakeX[$((SnakeLength-1))]}+1

elif [ "$MoveDirection" = "Left" ]; then

SnakeX[{SnakeLength}\]={SnakeX[$((SnakeLength-1))]}-1

fi

((++SnakeLength))

FoodGenerate

fi

}

Main()

{

while true

do

clear

echo "w:up s:down a:left d:right"

Show

Move

sleep 0.2

done

}

Main

相关推荐
bitcsljl几秒前
Linux 命令行快捷键
linux·运维·服务器
禁默16 分钟前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood23 分钟前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
学代码的小前端24 分钟前
0基础学前端-----CSS DAY9
前端·css
Code哈哈笑25 分钟前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb421528728 分钟前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
joan_8528 分钟前
layui表格templet图片渲染--模板字符串和字符串拼接
前端·javascript·layui
程序猿进阶29 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
Youkiup31 分钟前
【linux 常用命令】
linux·运维·服务器