I am writing a snake program in java that paints a series of rectangles. I don't understand how to delay my direction changes for blocks in order so that the snake rectangles change direction all at the same spot that the arrow key was hit. I need to wait within one method while the rest of the program continues to run. When an arrow key is pressed, it triggers this method to change the direction of my rectangles(u for up, d for down...)
`public void changeDirection(String dir)
{
for(int i = 0; i<snakearray.size(); i++)
{
//checks to make sure youre not changing straight from l to r or u to d
if(dir.equals("r")||dir.equals("l"))
{
if(//direction of currect element is up or down)
//makes the current element of the array change to the new direction
{snakearray.set(i, snakearray.get(i).newDirection(dir));}
}
else if(dir.equals("u")||dir.equals("d"))
{
if(//current element's direction is right or left)
{snakearray.set(i, snakearray.get(i).newDirection(dir));}
}
//pause method but not game method needed here????
}
}`
is there a way to implement the pause i need to? i've tried the thread.sleep(20) method but that pauses my entire program...