This week we were asked to create an object and code it in such a way that when a certain key is pressed, the object will move in that specific direction
Here's a screenshot of it:
Here is the code used to make this thing move:
In void main
glutKeyboardFunc(Keyboard);
glutKeyboardFunc(Keyboard);
Declared as global are:
int x= 0;
int y=0;
int x= 0;
int y=0;
In the KeyboardFunc(Keyboard)
void Keyboard(unsigned char key, int x, int y)
{
switch(key) //check which key is pressed
{
case 'd':
::x++; //add 1 to global x.
break;
case 'a':
::x--; //minus 1 to global x.
break;
case 'w':
::y++;
break;
case 's':
::y--;
break;
}
}
In my display()
glTranslatef(::x,::y,0);
glTranslatef(::x,::y,0);
No comments:
Post a Comment