功能简单的吞吃蛇(shell编程)
来源:互联网 发布:湖北汽车工业学院网络 编辑:程序博客网 时间:2024/05/11 18:15
天老师叫我们做了个吞吃蛇,只实现了很简单的功能,不废话,上马:
#!/bin/bash
trap input_key=0 20
trap input_key=1 21
trap input_key=2 22
trap input_key=3 23
trap input_key=4 24
high=15#界面长
width=50#界面宽
top=5#起始纵坐标
left=5#起始横坐标
for((i=0;i<high+2;i++))
do
for((j=0;j<width+2;j++))
do
((temp=i*(width+2)+j))
if((i==0 || j==0 || i==high+1 || j==width+1))
then
imap[$temp]=-1
else
imap[$temp]=0
fi
done
done
#画界面
drawbk()
{
for((i=0;i<high+2;i++))
do
for((j=0;j<width+2;j++))
do
((y=i+top))
((x=j+left))
((temp=i*(width+2)+j))
if((imap[temp]==-1))
then
echo -ne "/33[$y;${x}H/33[35;42m#"
fi
done
done
}
cur_y=0#蛇的纵坐标
cur_x=0#蛇的横坐标
to_y=0#蛋的纵坐标
to_x=0#蛋的横坐标
#随机蛇的坐标值
random_cur()
{
cur_y=$RANDOM
((cur_y=cur_y%high+1+top))
cur_x=$RANDOM
((cur_x=cur_x%width+1+left))
}
#随机蛋的坐标
random_to()
{
to_y=$RANDOM
((to_y=to_y%high+1+top))
to_x=$RANDOM
((to_x=to_x%width+1+top))
}
draw_cur()
{
echo -ne "/33[$cur_y;${cur_x}H/33[33;46m@"
}
draw_to()
{
echo -ne "/33[$to_y;${to_x}H/33[33;46m0"
}
#起始界面
init_set()
{
random_cur
random_to
drawbk
echo -ne "/33[$cur_y;${cur_x}H/33[33;46m@"
echo -ne "/33[$to_y;${to_x}H/33[33;46m0"
}
#按向上键‘w’
input_up()
{
temp=$cur_y
((cur_y--))
echo -ne "/33[$temp;${cur_x}H/33[0m "
echo -ne "/33[$cur_y;${cur_x}H/33[33;46m@"
}
#按向下键‘s’
input_down()
{
temp=$cur_y
((cur_y++))
echo -ne "/33[$temp;${cur_x}H/33[0m "
echo -ne "/33[$cur_y;${cur_x}H/33[33;46m@"
}
#按向左键‘a’
input_left()
{
temp=$cur_x
((cur_x--))
echo -ne "/33[$cur_y;${temp}H/33[0m "
echo -ne "/33[$cur_y;${cur_x}H/33[33;46m@"
}
#按向右键‘d’
input_right()
{
temp=$cur_x
((cur_x++))
echo -ne "/33[$cur_y;${temp}H/33[0m "
echo -ne "/33[$cur_y;${cur_x}H/33[33;46m@"
}
input_key=0#输入的键值0 1 2 3分别为上下左右
over_key=0#游戏是否结束 0则结束
#判断游戏是否结束
is_gameover()
{
case $input_key in
0)
if((cur_y-1==top))
then
over_key=1
fi
;;
1)
if((cur_y==top+high))
then
over_key=1
fi
;;
2)
if((cur_x-1==left))
then
over_key=1
fi
;;
3)
if((cur_x==left+width))
then
over_key=1
fi
;;
esac
}
tosid=0
#运行游戏
run_game()
{
init_set
while :
do
is_gameover
if((over_key!=0))
then
echo -ne "/33[$((top-1));${left}Hgameover!!"
echo -ne "/33[0m"
kill -9 $tosid &> /dev/tty2
exit
fi
case $input_key in
0)
input_up ;;
1)
input_down ;;
2)
input_left ;;
3)
input_right ;;
4)
exit ;;
esac
usleep 500000
if((cur_x==to_x && cur_y==to_y))
then
random_to
draw_to
fi
done
}
#接收输入
input()
{
while :
do
read -s -n 1 var
case $var in
'w')
kill -20 $1 ;;
's')
kill -21 $1 ;;
'a')
kill -22 $1 ;;
'd')
kill -23 $1 ;;
'q')
kill -9 $1 ;;
esac
done
}
#开始运行
if [ "$1" != "--show" ]
then
sh $0 --show $ & #另运行本程序到后台
input $!#发送消息
else
tosid=$2
run_game
fi
以后写注释
- 功能简单的吞吃蛇(shell编程)
- 功能简单的吞吃蛇(shell编程)
- 简单的shell脚本编程
- 利用linux下的c语言编程来简单的实现一个shell功能实现!
- shell编程之环境变量配置文件的功能
- Linux下shell的简单编程
- Linux下shell的简单编程
- Shell 编程的两个简单小例子
- Linux编程实现一个简单的Shell
- 几个简单的shell编程题
- 简单的shell编程和运行
- Linux下简单的shell编程(1)
- Linu Shell 编程基础--一个简单的Shell脚本
- 初识Shell Scripts编程--最最简单的shell例子
- linux系统-shell编程-一个简单的shell程序
- 编程实现简单的计算器功能
- 一道简单shell编程
- Shell 编程简单例子
- VC通用控件编程之CList控件
- 派生类到基类的转换(c++学习笔记)
- 自动化基础概念之“COM组件与接口”
- C/C++中回调函数初探
- 智能指针auto_prt的使用(c++学习笔记)
- 功能简单的吞吃蛇(shell编程)
- c++中cerr和cout的区别
- Linux学习笔记(一)
- Linux学习笔记(二)
- Linux学习笔记(三)
- 烦
- 转发:一个总经理的11个经典面试问题
- 转:八大排序算法总结
- 人生草稿