【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

相关推荐
云飞云共享云桌面32 分钟前
8位机械工程师如何共享一台图形工作站算力?
linux·服务器·网络
逐·風34 分钟前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
Devil枫1 小时前
Vue 3 单元测试与E2E测试
前端·vue.js·单元测试
Yaml42 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
小小小妮子~2 小时前
Spring Boot详解:从入门到精通
java·spring boot·后端
hong1616882 小时前
Spring Boot中实现多数据源连接和切换的方案
java·spring boot·后端
尚梦2 小时前
uni-app 封装刘海状态栏(适用小程序, h5, 头条小程序)
前端·小程序·uni-app
aloha_7892 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
GIS程序媛—椰子2 小时前
【Vue 全家桶】6、vue-router 路由(更新中)
前端·vue.js
记录成长java3 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet