一个由typescript写的贪吃蛇简单小游戏
1.首先进行项目搭建,将webpack和TS的相关依赖下载安装好
2.然后进行项目创建,将所需要的主界面搭建好,包括边界,蛇,食物,记分牌等css样式
3.开始进入到每个类的具体编写,除了基本属性还有可能会用到的方法
食物(Food): 每次被吃掉后会生成随机定位改变食物的位置
记分牌(scorePanel): 蛇每吃掉一个食物分数会加1,每增加一个分数段会加一个等级,等级有上线且等级和分数段可修改
蛇(snack):蛇每吃一个食物会增加一节,且会和蛇头移动,蛇头不能触碰到自己的身子,蛇不能调头移动
4.编写键盘事件,可以控制蛇的运行情况
通过键盘的上下左右移动,可以改变蛇的行动方向,当蛇碰到墙壁时要结束游戏并停止运行,
5.最后编译运行,调试结果。