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.
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.
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.
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.
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.
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:
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;
}