【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

相关推荐
古月居GYH6 分钟前
在C++上实现反射用法
java·开发语言·c++
烦躁的大鼻嘎16 分钟前
【Linux】深入理解GCC/G++编译流程及库文件管理
linux·运维·服务器
乐大师16 分钟前
Deepin登录后提示“解锁登陆密钥环里的密码不匹配”
运维·服务器
ac.char22 分钟前
在 Ubuntu 上安装 Yarn 环境
linux·运维·服务器·ubuntu
敲上瘾23 分钟前
操作系统的理解
linux·运维·服务器·c++·大模型·操作系统·aigc
Myli_ing29 分钟前
HTML的自动定义倒计时,这个配色存一下
前端·javascript·html
dr李四维1 小时前
iOS构建版本以及Hbuilder打iOS的ipa包全流程
前端·笔记·ios·产品运营·产品经理·xcode
儿时可乖了1 小时前
使用 Java 操作 SQLite 数据库
java·数据库·sqlite