Snake problem.

How can I clear the screen out and show the background? The problem is, when I press 1, it just shows background in less than 1 second and then goes back to the menu.. I need help. I just want to see the borders and the snake body to check if they were done correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4

int nScore;
int nGameDelay;
 
void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void snake(int x, int y)
{
  HANDLE hConsole;
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  gotoxy(x,y);
  printf("*******");
}

void moveSnake()
{
  
} 

void background()
{
  int i;
  gotoxy(0, 0);
  printf("_______________________________________________________________________________");
  for(i=1; i<=20; i++)
  {
    gotoxy(0,i);
    printf("|");
  }
  for(i=1; i<=20; i++)
  {
    gotoxy(78,i);
    printf("|");
  }
  gotoxy(0, 20);
  printf("_______________________________________________________________________________");
}

void display(int x, int y)
{  
  background();
  snake(x,y);
}


main()
{
  int pick;
  do
  {
    printf("\n\n\n\n\n\n\n");
    printf("\t\t _______  __    _  _______  ___   _  _______   \n");
    printf("\t\t|       ||  |  | ||   _   ||   | | ||       |  \n");
    printf("\t\t|  _____||   |_| ||  |_|  ||   |_| ||    ___|  \n");
    printf("\t\t| |_____ |       ||       ||      _||   |___   \n");
    printf("\t\t|_____  ||  _    ||       ||     |_ |    ___|  \n");
    printf("\t\t _____| || | |   ||   _   ||    _  ||   |___   \n");
    printf("\t\t|_______||_|  |__||__| |__||___| |_||_______|  \n");
    printf("\n\t\t              Press 1 to Play                \n");
    printf("\t\t              Press 2 to Quit                \n");
    printf("\t\t              Pick: "); scanf("%d", &pick);
    if (pick != 1 && pick != 2)
      printf("Invalid! Choose again!"); 
    if (pick == 1)
    {
      system("CLS");
      background();
      system("CLS");
    } 
  } while (pick != 2);
  fflush(stdin);
  getchar();
}
Include:
#include <conio.h>

and use something like this:
1
2
3
4
5
	char key = ' ';

	//the key will = the key pressed.
	//and will not pass this until a key IS pressed.
	key = _getch();


You could also use something like this to control the snake.
Thanks for the help Lynx. Will try that out when I'm already in the function how to move the snake. I can't even imagine where I would start. @-) thanks though.
I made a controlable robot-thing using _getch().

Look in to switch statements. http://www.cplusplus.com/forum/beginner/607/
Oh. I just want to let you know that I know how switch statement works. :D haha. But thanks for the info anyways. What I want to know is that how your code will work. I mean, how will I apply it to my program. When I researched about Snake games in C, I see switch statements for the body of the snake to move. Is that also the same thing as your code? If yes, would you kindly explain thoroughly to me how to create, apply, and use this function? Thank you very much for the help.
Yes, that's how my code worked.

I used a class for my robot with functions to move it, passing the function an x and y value.

But, if you don't know much about classes, that's ok, seeing as you're using a gotoxy function.

It would be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
char key = _getch();

switch( key )
{
	case 'w':
		eraseCurrentPosition();

		//move up
		move( 0, -1 );
		break;

	case 's':
		eraseCurrentPosition();

		//move down
		move( 0, 1 );
		break;

	case 'a':
		eraseCurrentPosition();

		//move left
		move( -1, 0 );
		break;

	case 'd':
		eraseCurrentPosition();

		//move right
		move( 1, 0 );
		break;

	default:
		break;
}

Erase function:
1
2
3
4
void eraseCurrentPosition()
{
	//cout a space ' ' to erase snake head from the console
}


Move function:
1
2
3
4
5
void move( int x, int y )
{
	gotoxy( x, y );
	//cout your char
}


You'll need some global variables for the current position, to save when a move occurs and to read from to erase.

And of course, you'll need to adapt this to only erase the last position of the snake, this would do for the head, or my robot which I made, which was only 1 character, whereas you'll have a longer body of chars.
Last edited on
To be honest, haven't heard of classes yet. What we're studying now is just the first half(simple) of C programming language WITHOUT the arrays, structures, etc. Only loops, if-else, switch, pointers, recursions, and nothing else and yet, we are asked to do something that involves the other half of C. @-) So I'm trying to learn these things by myself which is pretty hard to do. Anyway. I'm not yet starting with the move function as I can't even print out the snake because it doesn't print out and I don't know why.

is it necessary to put _ before getch? Or I should put something before it?
First, how could I produce the "current position" of the snake? So that I could erase the current and then just add/subtract the x/y coordinates of the snake.

Thank you so much for your help.
is it necessary to put _ before getch? Or I should put something before it?


getch() is deprecated. Now it's better to use _getch()
http://msdn.microsoft.com/en-us/library/078sfkak%28v=VS.80%29.aspx

First, how could I produce the "current position" of the snake? So that I could erase the current and then just add/subtract the x/y coordinates of the snake.


I went about it the wrong way before, but I'll leave my code there.
You can do it all with the gotoxy() function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
int main()
{
	int x, y = 10;
	char key = ' ';
	char snakeHead = '@';

	gotoxy( x, y );
	std::cout << snakeHead;

	key = _getch();

	switch( key )
	{
		case 'w':
			//erase at current x, y value
			std::cout << " ";

			//minus 1 to y( go up )
			//changing the y value
			gotoxy( x, --y );

			//then cout your head
			//at the new y value
			std::cout << snakeHead;

			break;

		case 's':
			/* code */
			break;

		case 'a':
			/* code */
			break;

		case 'd':
			/* code */
			break;

		default:
			break;
	}

	return 0;
}
Thank you so much sir. I will study this code of yours. Thank you again.
You're more than welcome. And good luck!
http://www.cplusplus.com/forum/beginner/56860/
similar problem and solution.
When i tried to compile, it seems to have errors. But i'll try to get the logic of the codes though.
What error you got anyway better to use logic other than source code.
Topic archived. No new replies allowed.