| Ferrous Moon http://www.ferrousmoon.com:80/forums/ |
|
| Please check my code http://www.ferrousmoon.com:80/forums/viewtopic.php?f=45&t=912 |
Page 1 of 1 |
| Author: | Azurozeta [Sat Aug 18, 2007 1:01 am ] |
| Post subject: | Please check my code |
well, i'm working on simple snake game, here's partial code that instruct the snake to move in the direction the want. The problem is, let's say i hit 'a', it just move left 1 square and then stop. Is there a way to make it move continuosly until i hit for another direction? ----------------------------------------------------------------------------------------------------------------------- do { input = getch (); switch (input) { case 'a' : moveLeft (); break; case 'w' : moveUp (); break; case 'd' : moveRight (); break; case 's' : moveDown (); break; } }while (input != 'k'); |
|
| Author: | Tycho [Sat Aug 18, 2007 1:18 am ] |
| Post subject: | Re: Please check my code |
Quote: well, i'm working on simple snake game, here's partial code that instruct the snake to move in the direction the want. The problem is, let's say i hit 'a', it just move left 1 square and then stop. Is there a way to make it move continuosly until i hit for another direction?
It's possible that the person is giving no input at that precise moment. You need to keep a variable that stores the current direction and then the switch statement would process that variable instead of what the input is. If there -is- some input, then have it assign the new input value to the variable if it's valid.----------------------------------------------------------------------------------------------------------------------- do { input = getch (); switch (input) { case 'a' : moveLeft (); break; case 'w' : moveUp (); break; case 'd' : moveRight (); break; case 's' : moveDown (); break; } }while (input != 'k'); I don't know if I can explain better than that. Anyone else? |
|
| Author: | Azurozeta [Sat Aug 18, 2007 1:21 am ] |
| Post subject: | |
still don't get it (excuse my newbieness). Perhaps you can change my code to match your meaning? |
|
| Author: | Tycho [Sat Aug 18, 2007 1:31 am ] |
| Post subject: | Re: Please check my code |
Code: char input = 0;
char currentDirection = 0;
do
{
input = getch ();
switch ( input )
{
case 'a':
case 'w':
case 's':
case 'd':
currentDirection = input;
break;
default:
printf ( "Invalid input ('%c')!", input );
break;
}
switch (currentDirection )
{
case 'a' : moveLeft (); break;
case 'w' : moveUp (); break;
case 'd' : moveRight (); break;
case 's' : moveDown (); break;
}
}while (input != 'k');
|
|
| Author: | Azurozeta [Sat Aug 18, 2007 2:08 am ] |
| Post subject: | |
it's not working tycho, unless i hit something, the snake would stop. Maybe there's another IO method you suggest? |
|
| Author: | Azurozeta [Sat Aug 18, 2007 6:01 am ] |
| Post subject: | |
nevermind then, i solved it :p thanks for the help though |
|
| Author: | frenchfrog [Sat Aug 18, 2007 11:51 am ] |
| Post subject: | |
So, what your final code looks like? |
|
| Author: | Azurozeta [Sun Aug 19, 2007 6:03 am ] |
| Post subject: | |
Well, in my case, the change applied to the 4 directional function i used. Well, because there are 4 directions, i'll just state one. So here goes: #include <constrea.h> // not sure if these libs are useful for my codes. #include <conio.h> #include <dos.h> #include <time.h> #include <stdlib.h> int x, y; // cartesian based positioning grid constream p; void main () { char input; randomize (); // invoke random time quantum << what the hell? x = rand () % 80; // make x value random from 0 to 80, 80 is the length of the screen, btw y = rand () % 50; // this is for random snake starting point do { input = getch (); switch (input) { case 'a' : moveLeft (); break; case 's' : moveDown (); break; case 'd' : moveRight (); break; case 'w' : moveUp (); break; } } while (input != 27) // define esc for bail out } //i'll just type one direction, the other's a bit similar. void moveLeft () { while (!kbhit ()) // << this is the solution for my problem { delay (200); // set delay, not sure how much, 200 milisecond? p << setxy (x,y) << "*"; x--; p << setxy (x,y) << " "; //for covering track } } |
|
| Page 1 of 1 | All times are UTC-05:00 |
| Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |
|