【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

相关推荐
工业互联网专业4 分钟前
基于springboot+vue的高校社团管理系统的设计与实现
java·vue.js·spring boot·毕业设计·源码·课程设计
九圣残炎6 分钟前
【ElasticSearch】 Java API Client 7.17文档
java·elasticsearch·搜索引擎
Channing Lewis14 分钟前
python生成随机字符串
服务器·开发语言·python
黯然~销魂41 分钟前
root用户Linux银河麒麟服务器安装vnc服务
linux·运维·服务器
资深设备全生命周期管理1 小时前
以Python 做服务器,N Robot 做客户端,小小UI,拿捏
服务器·python·ui
Channing Lewis1 小时前
如何实现网页不用刷新也能更新
前端
m0_748251521 小时前
Ubuntu介绍、与centos的区别、基于VMware安装Ubuntu Server 22.04、配置远程连接、安装jdk+Tomcat
java·ubuntu·centos
Bro_cat1 小时前
深入浅出JSON:数据交换的轻量级解决方案
java·ajax·java-ee·json
等一场春雨1 小时前
Java设计模式 五 建造者模式 (Builder Pattern)
java·设计模式·建造者模式
hunzi_11 小时前
Java和PHP开发的商城系统区别
java·php