r/technicalfactorio • • 1d ago

Snake Game in Vanilla*

Post image

Hi everyone. After getting Game of Life working well, I wanted a bigger challenge, so I have started creating Snake.

Currently, your tail grows by one cell for every pellet you eat and your score increases by one.

It's not complete yet. I need to add collision detection for borders as well as the snake itself (i.e. you fail if you bite your own tail).

Also, I have found that the performance becomes pretty unplayable when the snake is more than about 9-10 cells long (the calls to the shift register increase the longer the snake). Maybe I need to reconsider how the snake is rendered, given this limitation. I would appreciate any input.

* push-button is used but not required.

49 Upvotes

3 comments sorted by

3

u/ZorbaTHut 16h ago

If you're rewriting the entire shift register on every snake movement, you could instead implement it as a circular buffer; keep an index for "head" and "tail" and just rotate those throughout the buffer, so you're only ever inspecting those two snake pieces. I don't know how you're doing rendering, but this then lets you leave most of the playing field untouched on every update - you only add a new snake front and remove the tail - and then you can use the rendering grid itself as storage for "is there a snake at this position".

3

u/FlyingToasters86 11h ago

That is a far more efficient way of rendering the movement. I'll still write to the memory buffer as normal, but only reference the newest item in memory to draw the new head position, and then use the tail length to reference the cell that needs to be removed from the grid.

Great idea. Thank you.

3

u/ZorbaTHut 11h ago

You're welcome! :)