7 Replies - 791 Views - Last Post: 27 October 2013 - 12:00 PM Rate Topic: -----

#1 zebmastah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-October 13

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..

:)) /ZebMastaH

Is This A Good Question/Topic? 0
  • +

Replies To: Program Snake (short version) in C

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

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.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,360
  • Joined: 20-March 10

Re: Program Snake (short version) in C

Posted 27 October 2013 - 06:19 AM

You might want to add colors to your project always a good idea for snake games.

http://www.cplusplus...icles/2ywTURfi/

The above article will show you how to do this but it does use the dreaded void main() which is not right
its always int main() (imo).

a bit of c++ code on the headers use.
#include <iostream>

using namespace std;
#include "concol.h"

int main()
{
    setcolor(gray,black);
    cout << "snake body" << endl << endl;
    setcolor(red,red);
    cout << " ";
    setcolor(blue,blue);
    cout << "          " << endl << endl;
    setcolor(gray,black);
    cout << endl << "fruit" << endl;
    setcolor (green,black);
    char fruit = 0xcf;
    cout << fruit << endl;
    setcolor(gray,black);

    cin.get();
    return 0;
}




You also need as well as a grid (2d array) , a way of inserting the individual fruits and snake body segments.


One of our Alumni GabeHabe has an article written on how to do this entitled redefining gotoxy.

This article can be found here since our own snippets section is still down.

http://icodesnip.com/search/Gabehabe/

You simply use the function gotoxy( x, y );
to go directly to that square on the console window and vertically insert the character you need to lay down.

You also need a way to wipe the screen in between movements, luckily Gabehabe again comes to the rescue with his redefining clrscr function which is on the same page as the last link.

usage clrscr();

After that coupled with Adak's advice you are good to go.

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

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.

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

This post has been edited by Adak: 27 October 2013 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,360
  • Joined: 20-March 10

Re: Program Snake (short version) in C

Posted 27 October 2013 - 09:55 AM

View PostAdak, 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.

Thanks

Snoopy
Was This Post Helpful? 0
  • +
  • -

#6 zebmastah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-October 13

Re: Program Snake (short version) in C

Posted 27 October 2013 - 10:15 AM

Okay thanks! Ive been starting by building up the screen how it should look. I found in the ASCII table a nice way of putting a frame.. but then i got this stupid loop that i don't really get.

for (y = 0; y < MAX_Y; y++)
	{
		for (x = 0; x < MAX_X; x++)
		{
			printf("%c", base[y][x]);
		}
		printf("\n");
	}


Why on earth is that newline printed? Why does it have to be printed??
Was This Post Helpful? 0
  • +
  • -

#7 zebmastah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-October 13

Re: Program Snake (short version) in C

Posted 27 October 2013 - 10:22 AM

View Postzebmastah, on 27 October 2013 - 10:15 AM, said:

Okay thanks! Ive been starting by building up the screen how it should look. I found in the ASCII table a nice way of putting a frame.. but then i got this stupid loop that i don't really get.

for (y = 0; y < MAX_Y; y++)
	{
		for (x = 0; x < MAX_X; x++)
		{
			printf("%c", base[y][x]);
		}
		printf("\n");
	}


Why on earth is that newline printed? Why does it have to be printed??


And when i want the MAX_X to fit the whole console.. which means i change it from 60 to 80. And remove the Newline in the loops.. it works fine.
So if I have 60, i need newline, with 80 i dont.... ??
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1