I want to get a circle to move around the screen. I've got that down. But how do I get it to keep moving until you make it move in another direction(you guessed it... yup the games snake)?
I want to get a circle to move around the screen. I've got that down. But how do I get it to keep moving until you make it move in another direction(you guessed it... yup the games snake)?
you don't need multithreading for that, just handle possible keyboard input in your animation loop.
after you hit the right arrow key say
then, if another key is pressed, like left,Code:right = true; //make sure you have bool right=false; //code code code while(right == true) { //do snake code }
hope i helpedCode:left = true; right = false; down = false; up = false; while(left == true) { //do snake code that makes the snake go down }
No, you just need some clever thinking. Someone posted a solution to this before... if I remember correctly, it went like this.
Code:int iKeyPress; do { while (!kbhit()) { moveSnake(); } iKeyPress = getch(); switch (iKeyPress) { case 'A': moveSnakeLeft(); break; case 'S': moveSnakeDown(); break; case 'W: moveSnakeUp(); break; case 'D': moveSnakeRight(); break; } } while (!gameIsOver());
what does the function, moveSnake()?
what does it do?
??? hello, are you there?
try giving people more than 15 minutes to respond to your question? maybe like 15 hours? or 30 hours? and i'm serious btw; people have other things to do in life.
anyway, movesnake() moves the snake.
hello, internet!
stop spamming and think about it for a second.
You want the snake to keep moving in whatever direction it was already going in. so....
function MoveSnake() will check the direction the snakes going in and move it accordingly.
functions MoveSnakeLeft() etc will only set the direction variable that is checked within the MoveSnake() function.
For gods sake think about things a little yourself first.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
I'm attaching a snake game I wrote quite a while back. The source and its exe is included.
Your game loop is the place where you want to move the snake to its next position. Before each next movement, you just want to check if a direction key was hit and if so, set a direction flag to that which corresponds.
Here's my main game loop:
So to put it plainly, just set an enumerated type to the direction and move() will just move wherever it's set to.Code:void snake::loop() { initialize(); while (alive) { if (!paused) delay(speed); if (kbhit()) { int key = getch(); switch (key) { case 'F': case 'f': paused = paused ? false : true; froze = true; scoreboard(); break; case 'Q': case 'q': case 27: gameover(); return; case 'S': case 's': changegamesize(S); break; /* S or s */ case 'R': // 'm' == 77 && 77 == LARROW (so we're using 'R' for medium - for 'Regular') case 'r': changegamesize(M); break; /* R or r */ case 'L': case 'l': changegamesize(L); break; /* L or l */ case 33: case 49: speed = LOW; scoreboard(); break; /* 1 */ case 64: case 50: speed = MED; scoreboard(); break; /* 2 */ case 35: case 51: speed = HI; scoreboard(); break; /* 3 */ case 36: case 52: speed = HI2; scoreboard(); break; /* 4 */ //only check for movement if we aren't paused case 72: if (dir == DOWN || paused) break; dir=UP; move(); break; case 77: if (dir == LEFT || paused) break; dir=RIGHT; move(); break; case 80: if (dir == UP || paused) break; dir=DOWN; move(); break; case 75: if (dir == RIGHT || paused) break; dir=LEFT; move(); break; } } else if (!paused) move(); } }
Thanks! Sorry, I was such a loser... I gotta headache... anyway it worked, its just a little flickery... i'm going to work on that, but thanks to everyone who posted...
I got the flickering down, too. I'll post a zip of the game when they let me... the option won't come up... I'll try again in 30 or an hour... Thanks again for the help.
I think it's not coming up because you're not registered? Maybe?
Anyway, glad to help.
i'm registered... but it came up just now so here goes...
any replies to the game in the making?