Jump to content
  • Advertisement
Sign in to follow this  
Grantyt3

Snake

This topic is 4414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm going to make a snake game, and I wanted your guys' opinion. It's going to be a grid-based snake game. My plan for movement is to have a vector for the snake pieces. When the player's head is on the same grid point as a piece of food, it will add a new piece to the vector. For movement, each piece would move to the piece in front of it. I know I wasn't very specific, but is this a good way to do it, or how is it usually done?

Share this post


Link to post
Share on other sites
Advertisement
Personally, rather than 'move' each segment of the snake. Id save myself a lot of work and use a RingQueue of snake segments, with a running count of how long it is, and which slot contains the head.
Thus rather than 'move' (change tons of parameters for each segment); they individually remain static, with only the memory mapping order changing. As the snake moves forward, correspondingly the head position in the queue changes, and the length counter makes the tail end of the queue expire. Only one segment actually needed to be updated (head) the intermediate body segments remain as is, and the tail is basically ignored(you'll automatically deallocate it as the ringqueue wraps around in the future).

Share this post


Link to post
Share on other sites
It may have several different names depending on what school you went to.
I've known it as RingQueue
but Ring Buffer
Circular Buffer
Circular Queue
etc
may also be valid

wikipedia: http://en.wikipedia.org/wiki/Circular_buffer

basically you make a Big Array, big enough to hold the Max length of your snake.
and you have a pointer to the 'head'(insertion end) of the Queue (also the literal head of the snake) As your snake moves forward, rather than change the x,y coodinates of each body segment, you simply create a new head, and add it to the ringqueue. The old snakehead, stays in the ringqueue, and keeps its old x,y coords, but it gets treated as body segment #1 instead of a head. similarly, old body segment #1 is now treated as #2, and so on down to the end of the queue (and end of the snake).
None of the segments in the Array actually move, you simply draw them to screen in a different order (one slot higher each time)
after a while, the front of the array will be full of 'dead' body segments that are no longer used, and the 'head' will reach the end of the array. To handle this issue, you wrap the array into a 'ring'(hence RingQueue) and reset the 'head' to the front of the array ontop of the dead slots.
(the drawing order of body segments also needs to be able to travel off the end of the array and onto the other side)

The advantage of doing this, instead of a Vector or LinkedList approach, is that memory access is minimal for the snake segments. You are not constantly allocating and deallocating memory as segments are added and expired. And there is no modification to segments after creation.

Share this post


Link to post
Share on other sites
I'm 99% sure that the orginal Snake arcade game used a RingQueue, since on that old hardware performance was paramount.
On a modern computer however, Snake is a relatively simple task, and your Vector/LinkList approach should still work.
So if your goal is just to get a game running, Vector is fine.

It is good to know about various data structures though, and this project does sound like a good opportunity to learn RingQueue. So it depends what you want to focus on. If you still want to know, I can write some psuedocode for ringqueue/snake when I get home later...

Share this post


Link to post
Share on other sites
I'd like to use Ring Queue since this is just for learning purposes anyway, and I might as well learn as much as I can in the process. So yes, example would be great when you get home.

Share this post


Link to post
Share on other sites
Here is an ilistration of a circular array

http://lcm.csa.iisc.ernet.in/dsa/node24.html

This should also help in creating a circular array
http://www.cs.hmc.edu/claremont/keller/webBook/ch07/sec10.html

Hope this helps in creating the circular array datatype.

Share this post


Link to post
Share on other sites
Well, I guess I understand how it works, but I don't understand how I could implement it into the game.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!