所选题目:可交互的贪吃蛇游戏
代码使用 Matlab 完成了一个三维贪吃蛇的游戏,玩家可以通过键盘上的方向键控制蛇的移动,蛇吃到食物后会变长,当蛇碰到自己的身体时游戏结束。
- 谁说 Matlab 只能做科学计算?我反手就搞个贪吃蛇出来!
得益于 Matlab 简单的三维画图函数 scatter3,基于 Matlab 制作出一个三维的空间并不难。不过由于 Matlab 缺少像 Pygame 一样可以读取键盘的库,所以需要将.m 函数放入 Matlab 的终端上面(也就是一开始下载 Matlab 时候的默认排列方式)不然需要一直 alt+tab 来回切屏
由于 3D 空间,控制方向键也从 4 个增加为 6 个了,采用小键盘的数字 4 6 8 2 分别作为 X,Y 轴的移动(8 和 6 为正向),控制 Z 轴的为 w 和 s 键。同时为了方便瞄准,视角会不断转动。(如果视角固定,几乎瞄准不了果子)
MATLAB 打开.m 函数即可运行。若要终止代码,请摁 x 或者 ctrl+c
win11
MATLAB R2022b
无