Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 7 developers from Canada and 18 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!


Snake in C


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 kaktusas2598   Members   -  Reputation: 944

Like
0Likes
Like

Posted 08 November 2012 - 07:38 AM

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.

Deltron Zero and Automator.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 11740

Like
0Likes
Like

Posted 08 November 2012 - 08:01 AM

How to know what data structures and algorithms to use to program Snake.

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, 08 November 2012 - 08:11 AM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


#3 SuperVGA   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 08 November 2012 - 09:51 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS