🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

nibbles

Started by
2 comments, last by mikael_j 24 years ago
I'm currently trying to write a nibbles style game under linux using c and svgalib, but I am having some problems with getting the snake to move, that is I want it to keep moving until the user presses a different key(right now it moves one "step" each time the user presses a key)
Advertisement
If you post the relevant section of code, or a link to it, that would help.

It sounds like you have something like this
------------------------
if (UserPressesKeyX){
PosX = PosX + 1;
}
------------------------

When maybe what you want is
------------------------
if (UserPressesKeyX){
Direction = X;
}
elsif (UserPressesKeyY){
Direction = Y;
}
...

if (Direction == Y){
PosY = PoxY + 1;
}
--------------------------

i.e. you need state variables for movement.
If this is not what your problem is (I know this is trivial - sorry if this is too simple). Is it related to some SVGALIB game api thing or something. Like I said relevant code would be good. Hope this helps.

Dane Jackson - zuvembi@mindless.com
The meek shall inherit what they're bloody well given.
And be thankful for it.
I know this is an old message, but there are some basic programming tips here... POLLING!

Another possible cause of the problem described is that the program is using a blocking fuction to read the state of the keyboard (such as fgetc), then processing it as soon as the key is pressed.

If this is the case, you need to poll the state of the keyboard before calling the funciton (example below uses fgetc() and stdin as the stream)

This can be achieved using the most useful function in the C library... select().

You pass it some file descriptors, and it returns the number of queued requests...

/* select()/fgetc() polling example */

fd_set *in_set;

FD_ZERO(in_set);
FD_SET(0, in_set);

while (1) {
if (select(1, in_set, NULL, NULL, 0) ) {
ch = fgetc(0);
}

process_char(ch);
}

/* end of example */


}

After careful deliberation, I have come to the conclusion that Nazrix is not cool. I am sorry for any inconvienience my previous mistake may have caused. We now return you to the original programming

As Zuvembi mentioned you need state variables, and in this case we need the directions in which the snake is moving...

...

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

....
int direction[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
...


switch (pressed_key)
{
case VK_LEFT: snake.direction = LEFT; break;
case VK_RIGHT: snake.direction = RIGHT; break;
case VK_UP : snake.direction = UP; break;
case VK_DOWN: snake.direction = DOWN; break;
}

....

// Move the snake in the specified direction
x += direction[snake.direction][0];
y += direction[snake.direction][1];

...

hope you''re starting to get the idea...

This topic is closed to new replies.

Advertisement