• Advertisement
Sign in to follow this  

Snake in C

This topic is 1900 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

Hi, I have goal to create snake in C using SDL for graphics and input. I have already experience with C/++, C# and SDL. So I want to know where I have to start. How to know what data structures and algorithms to use to program Snake. It would be nice if you can point me to some resources of doing it. I also need refresh on SDL, because I haven't used it for some quite time already. Also it can be for games like PacMan, Pong or Tetris. Thanks in advance. I have already created these games in C++/SDL, but it was about 2 years ago, and now I need to use just C and SDL. I would be greatful for any reply.

Share this post


Link to post
Share on other sites
Advertisement
How to know what data structures and algorithms to use to program Snake.[/quote]
A linked list with head and tail could be appropriate, as each step you could just remove the tail if the snake hasn't grown, and add a new head depending on the direction the snake is facing, I guess. And then use an ordinary list to store the apples on the map and check if the snake's head is on any apple or on any part of its body (by iterating through the linked list - this might not be ideal). At least that's what springs to mind at first - there are possibly better ways to do it.

A simple 2D array which keeps track of the snake's head and tail would probably work better here anyway, in fact. Edited by Bacterius

Share this post


Link to post
Share on other sites
I'd say you only need to check the apples against the head once the head enters a new tile.
Just don't spawn apples on the snake's tail.
That's fewer checks upon snake movement, at least.

Share this post


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

  • Advertisement