We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › help with motion
Page Index Toggle Pages: 1
help with motion (Read 770 times)
help with motion
May 23rd, 2009, 3:27pm
 
I'm trying to make a snake game in which the snake is controlled by the arrow keys.  The only tutorials i can find only offer example code where motion of objects only follows the mouse cursor.  Does anyone know how to make the motion dictated by the arrow keys?
Re: help with motion
Reply #1 - May 23rd, 2009, 4:26pm
 
there is a keyPressed() function you use to get keyboard input

for the snake just use the arrows input to set a direction:

UP arrow would set yvel = -1;
DOWN arrow would set yvel = 1;
LEFT arrow would set xvel = -1;
RIGHT arrow would set xvel = 1;

so,

void keyPressed()
{
 if( keyCode == UP )
   yvel = -1;
 // do the same for other directions
}

your main loop:
xpos += xvel;
ypos += yvel;

enjoy
Re: help with motion
Reply #2 - May 24th, 2009, 1:02am
 
Just a word: don't take blindly the first sub-forum you find, take time to read their descriptions and find the right one to ask questions...
Page Index Toggle Pages: 1