How to delay within one method without delaying the whole program in java?
I am writing a snake program in java that draws a series of rectangles. I don't understand how to defer the direction changes for the blocks so that the snake rectangles change direction at the same place as the arrow key. I need to wait within one method while the rest of the program continues to run. When the arrow key is pressed, it calls 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 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 that I need? I tried the thread.sleep (20) method, but this pauses my whole program ...
source to share
Forget about creating a new thread for just one simple method. You want the time that is running to move, for example, once a second, and you have to change the way the snake moves.
Time first:
swing.Timer: https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html It is very easy to use and has a lot of functionality.
Or you can implement very basic time by measuring the time since the last move.
You will need the variables lastTime
and now
.
now = currentTime(); if(now - lastTime > delay){ moveSnakeParts(); lastTime = currentTime(); }
Second, you do not want to hold back the snake parts. The snake has a head that you can control and a list of body parts. All you have to do is move the head while maintaining the previous position, moving the first body part to the previous head position and maintaining the previous position of the previous body parts and so on ... Hope you get this.
source to share
You can use the so called ScheduledExecutorService . This will allow you to execute your code in a separate thread at a specified delay.
Example:
@Test
public void test() throws ExecutionException, InterruptedException {
final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> future = service.schedule(() -> changeDirection("r"), 2, TimeUnit.SECONDS);
// Just for the test to wait until the direction change has been executed
// In your code you would not need this if you want your program to continue
future.get();
}
private void changeDirection(String dir) {
System.out.println("Changing direction to " + dir);
// Your code
}
Explanation:
The above code executes your method with a delay of 2 seconds. The return value future
is available here to track the completion of the execution. If you use it, your program will wait for execution to complete.
Since you don't want your main program to wait, just ignore / don't use the call future.get()
and your main code will continue to execute without delay, only the scheduled method will be delayed.
source to share