开发者

Snake Game following segments (non-grid based)

I'm trying to create a Snake Game where the movement of the snake isn't grid based but freeform and I'm having troubles figuring out how to get the trailing segments to follow the head.

The easiest solution is to loop from tail to head and set the position of the current to the next segment's position, ie: segment[i].position = segment[i - 1].position. This works great but I end up with all the segments clustered together by only a pixel apart (or so). It's not very attractive.

In an effort to beat that, I created an array for the last 15 positions and push the latest position to the end. Then every frame I pop the first element from the array and use that, giving me a snapshot of where the segment was 15 frames ago. This w开发者_如何学编程orks perfectly but performance is really dragging by constantly calling new() and shuffling the array around. The Flash garbage collector is giving me hell.

Can anyone think of any other solutions?

I'm working on Flash but I don't think the solution is really based on any language.

Thanks!


A deque tends to be the way these things are implemented, but if one isn't available then a ring buffer implemented in an array can serve as a substitute if the queue size has an upper bound.


The easiest solution is to loop from tail to head and set the position of the current to the next segment's position, ie: segment[i].position = segment[i - 1].position. This works great but I end up with all the segments clustered together by only a pixel apart (or so). It's not very attractive.

This shouldn't cause the bunching-up you describe. Could we see the code? Maybe you're doing something that results in the segments sharing position objects.

That said, the slick approach (assuming it doesn't mess things up with your art, and that you can move one segment-width at a time) is to move the head to its new position, and then move the tail to where the head was, reinserting it into the list in the appropriate position.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜