贪吃蛇风格游戏的简约方法
大约两天前,我收到了 TI-82 STATS 可编程计算器(实际上更像是 TI-83),并且想使用内置 TI-BASIC 语言编写贪吃蛇游戏。
尽管我必须找出:TI-BASIC极其慢。我的第一次实现非常慢,对玩家来说甚至不是一个挑战!对我来说,主要瓶颈在于包含蛇体坐标的列表(数组)的管理。
我尝试了两件事:
- 当蛇移动时,更新头部位置,然后从尾部位置循环遍历数组,并将
myList[ N ]
设置为myList[ N - 1 ]
code>,为了让蛇看起来是在移动。
然而,当列表长度达到大约 4 部分后,就无法播放了。 (太慢)
- 然后,我尝试使用 TI-BASIC 的列表操作功能来实现某种队列/双端队列,例如弹出末尾并在数组的前面添加一些内容。
这效果更好一点,但随着时间的推移也会变得太慢。
TL;DR / 实际问题:
- 你知道一个技巧吗,这样游戏就不会随着蛇变长而变慢?我已经看到这在 TI-BASIC 制作的其他游戏中是可能的
I received my TI-82 STATS programmable calculator (which is in fact more of a TI-83) about two days ago - and wanted to program a Snake game with the builtin TI-BASIC language.
Although I had to find out: TI-BASIC is extremely slow. My first implementation was so slow, that it wasn't even a challenge for the player! The main bottleneck for me lies in the management of the list (array) containing the coordinates of the snake body.
I have tried two things:
- When the snake moves, update head position, then loop through the array from the tail position, and set
myList[ N ]
tomyList[ N - 1 ]
, in order to make the snake appear to be moving.
This however, gets unplayable after the list gets about 4 parts long. (too slow)
- Then, I tried implementing some sort of queue/deque using TI-BASIC's list manipulation features, like popping off the end and adding something at the front of the array.
This worked a bit better, but also gets too slow over time.
TL;DR / actual question:
- Do you know a trick so the game doesn't slow down with the snake getting longer? I have seen that this is possible in other games made in TI-BASIC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用循环缓冲区。详细说明:
获取一个足够大的数组以容纳最大的蛇。建立两个指针,一个用于头部,一个用于尾部。
开始时,尾部位于单元格#1 中,头位于单元格#3 中。当蛇移动时,将头指针向右移动并写入新的坐标。然后,如果没有吃食物,则将尾部指针也向右移动。如果任何一个指针试图超出数组的最右端,则将它们包装到开头。
Use a circular buffer. To elaborate:
Get an array, sufficiently big to hold the maximum snake. Establish two pointers, one for the head, one for the tail.
At the beginning, the tail would be in cell #1, the head in cell #3. As the snake moves, move the head pointer to the right and write the new coordinate. Then, if there's no food eaten, move the tail pointer to the right as well. If either of the pointers tries to go beyond the rightmost end of the array, wrap them over to the beginning.
最有可能起作用的一个技巧是用 [ N - 1 ] 代替 [ N - 2 ] 或更高的数字,这样它可以通过数学上更快地移动来弥补时间(您还必须调整头部的大小以更快地移动)
A trick that most likely will work is instead of [ N - 1 ] do [ N - 2 ] or a higher number that way it makes up time by mathematically moving faster (you also have to adjust the size of the head to go faster
使用列表来提高速度的一个简单技巧是充分利用
LIST
菜单下提供的功能。特别是,与实现相同目标的 for 循环相比,seq
可以提供显着的性能优势。我发现有用的其他函数是 cumSum 和 ΔList。A simple trick when working with lists to improve speed is to make full use of the functions provided under the
LIST
menu. In particular,seq
can provide significant performance benefits over a for loop that accomplishes the same goal. Other functions that I find useful arecumSum
andΔList
.