【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

相关推荐
q***61417 分钟前
Spring中Aware的用法以及实现
java·数据库·spring
e***877023 分钟前
windows配置永久路由
android·前端·后端
代码or搬砖28 分钟前
SpringMVC的执行流程
java·spring boot·后端
大柏怎么被偷了1 小时前
【Linux】进程等待
linux·运维·服务器
Appreciate(欣赏)1 小时前
JAVA使用poi类读取xlxs文件内容拼接成添加数据SQL
java·开发语言·sql
Dorcas_FE1 小时前
【tips】动态el-form-item中校验的注意点
前端·javascript·vue.js
小小前端要继续努力1 小时前
前端新人怎么更快的融入工作
前端
极光代码工作室1 小时前
基于SpringBoot的流浪狗管理系统的设计与实现
java·spring boot·后端
毕设源码-朱学姐1 小时前
【开题答辩全过程】以 基于JAVA的恒星酒店客房管理系统为例,包含答辩的问题和答案
java·开发语言
四岁爱上了她1 小时前
input输入框焦点的获取和隐藏div,一个自定义的下拉选择
前端·javascript·vue.js