Program Snake (short version) in C

Posted 27 October 2013 - 02:29 AM

Hi!
I'm starting to build a simple version of snake. And I need som hints to start out. I building it i the console, and i want no complex menu at the beginning. Only a snake running around eating food, and keep track of highscore.

I don't want any code. Just hints and clues about how to build and where to start

I think im supposed to build a 2dim array to keep track of the game. And a snake head to eat food... And some sort of time to keep track of how fast he is moving..

Replies To: Program Snake (short version) in C

Re: Program Snake (short version) in C

Posted 27 October 2013 - 04:02 AM

1) download a copy of the ascii chart (including the extended one), so you can choose a good set of char's for your snake.

2) yes, 2D array.

3) include time.h so you can use clock_t start and stop and look up clock() to see how to tell elapsed time (it's usually (double)(stop-start) divided by CLOCKS_PER_SEC, and prints out as a %f in printf. (good for greater than 1/1,000th time difference, only)

difftime() is another way to go. Using the high performance timers is still another way to go.

4) you want the snake to head toward the food. So you'll need the snake head's x and y position, as well as the x and y of the food. I'd use a struct snakes that has int x and int y as struct members. Same with the food. Whether they use the same struct or not doesn't matter, but you have to know where each is, at all times.

Based on the x and y of snake and food, you have to work out a way for the snake head to get to the food, by always trying to get one x or y value closer to the goal. Be sure not to allow the snake to go our of bounds. Putting a value like -1 all around the game area, is a good idea. It makes the code simpler to keep the snake in bounds.

Re: Program Snake (short version) in C

Posted 27 October 2013 - 08:10 AM

In a snake program, you should not be wiping the screen. />

The very end of the snake - the last segment of his tail, ONLY needs to be erased. All the other segments of the snakes body will simply move up and take the position the segment before it had. The head of the snake moves to a new position, but it's the only part of the snake that does. All the other segments just play "follow the leader". You'll want a little serpentine shaping in the segments, so the snake doesn't look perfectly straight. When the snake makes a turn, make it something less than a perfectly square 90° turn, in just one char's radius.

Re: Program Snake (short version) in C

Posted 27 October 2013 - 09:55 AM

Adak, on 27 October 2013 - 03:10 PM, said:

In a snake program, you should not be wiping the screen. />/>

The very end of the snake - the last segment of his tail, ONLY needs to be erased. All the other segments of the snakes body will simply move up and take the position the segment before it had. The head of the snake moves to a new position, but it's the only part of the snake that does. All the other segments just play "follow the leader". You'll want a little serpentine shaping in the segments, so the snake doesn't look perfectly straight. When the snake makes a turn, make it something less than a perfectly square 90° turn, in just one char's radius.

|
|
|
-------> Doesn't look like a snake in shape or in movement.

That would be the Advanced way to do it yes,

but the OP has said he wants to do it the easiest way which would indeed consist of screen wiping.

You would still need some sort of gotoxy and color routines in your version too.

Re: Program Snake (short version) in C

Posted 27 October 2013 - 12:00 PM

Yes, at 80 chars position, the terminal driver will automatically move the cursor down to the next row, for you. At 60, the terminal won't do it automatically. You must add your own newline at 60 chars position, if you want one.