Recently I was working on a small game in which I had to move a vector of points continuously in one direction until the object needed to change directions. (it was a snake game similar to that found on a Nokia cell phone if that helps your visual) In order to accomplish the animation I moved the x and y coordinates of each point in the vector accordingly in the paint method, and then called repaint recursively until the game was over.
So the idea goes something like this:
public void paint(Graphics g)
{
snake.move(direction);
snake.copyInto(snakeArray);
// the above line moves the points in the vector so that
// they will be repainted correctly by the following loop
for (int i = 0; i < snakeArray.length; i++)
{
g.fillRect(snakeArray[i].x, snakeArray[i].y, 5, 5);
}
// make a call to Thread.sleep() to slow down the movement
// make a check for game over
repaint();
}
I have the JPanel listening for the key presses and changing the direction of the snake. Unfortunately there is a slight lag in the movement of my snake so that the control is a little behind the key presses of the user. How can I improve the efficiency of my code so that the control of the snake is more responsive?
Thanks,
Chad
[This message has been edited by Jim Yingst (edited January 17, 2001).]