I am developing a small game on a microprocessor, and I'm trying to get a function which changes the direction a sprite is moving by 90 degrees either left or right dependent on the left button press, or right button press.
Currently this is my code. for changing direction, doesn't perform exactly the task desired..
void change_direction(Sprite * sprite) {
sprite->x += sprite->dx;//these two lines basically tell the sprite to move in whatever direction the button presses tell it to. by default it moves in a straight line in a northern direction.
sprite->y += sprite->dy;
if ( pressed( SW1 ) ) {
sprite->dx = (sprite->dy) ? -sprite->dy : 1;//this code is very broken, what it does is it moves it either diagonally upwards, or right. it should turn the sprite 90 degrees left everytime the switch is pressed.
sprite->dy = (sprite->dy) ? 0 : -sprite->dx;
}
else if( pressed( SW0 ) ) {//this code turns the sprite right once from the default direction, but not again. what is should do is turn the sprite 90 degrees everytime.
sprite->dx = -1;
sprite->dy = 0;
}
}
I had an idea of how to approach this; a switch press doesn't change the new direction through changing the dx dy values locally, but instead increments or decrements an integer which controls the direction. I don't know how i would implement such a thing however.