【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\]} {SnakeXi}

echo "*"

done

#Food

tput cup {Food\[0\]} {Food1}

echo "0"

tput cup 20 0

}

ArrayMove()

{

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

do

SnakeXi=${SnakeXi+1}

SnakeYi=${SnakeYi+1}

done

}

FoodGenerate()

{

Food1=(({RANDOM}%${MapHeight}))

Food0=(({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 ${Food0} ]; 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

相关推荐
风骏时光牛马几秒前
生成一个移动端标题,只需要一个标题,不需要其他的任何文本。
前端
IT_陈寒25 分钟前
Redis雪崩把我坑惨了,这是5年经验的教训
前端·人工智能·后端
SameX34 分钟前
HarmonyOS ArkUI List 的侧滑删除 用 ListItem 的 swipeAction + endIcon 的做法
前端
爱莉希雅&&&41 分钟前
GitLab 完整学习笔记
linux·运维·服务器
露天赏雪1 小时前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
天天被压力1 小时前
【A股分钟级高频数据完全手册 #06】北交所分钟级数据接口:京市股票Pro版最新分时
java·人工智能·python
天天被压力1 小时前
【A股分钟级高频数据完全手册 #05】指数历史分钟数据接口:Pro版沪深指数分时回放
java·人工智能·python
captain3762 小时前
▲统一功能——DispatcherServlet源码分析
java·spring·java-ee·log4j·maven
金銀銅鐵2 小时前
JDK 21 中 List.of 的实现细节:从 List12 到 ListN
java·后端·ai编程
陈希1233 小时前
Linux 服务器性能排查手册(2026):CPU、内存、磁盘IO、网络四大瓶颈定位
linux·服务器·网络