Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Efficient Animation  RSS feed

 
Chad LaVigne
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like some for loop code got lost when you posted, but here are two general ideas, both based on the fact that copying arrays is slow:
1) If you do need to copy an array, try to use the System.arraycopy method. It's faster than most anything you can implement in native Java.
2) In snake, and many other games, the delta of the screen is relatively low. Rather than change the whole screen, you can change only what's different. For example, if a line is moving across a screen, instead of redrawing the whole line, you can simply cut off the tail off the line (set it to the background), and add to the head--this assumes the line/snake/object is uniform. This is tricker when it bounces and turns, but similar ideas apply.
--Mark
hershey@vaultus.com
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited the code to restore the missing for loop - because there was no space after the <, our browsers end up interpreting it as the beginning of an HTML tag, which is interpreted rather than displays. So be sure to surround comparison operators with spaces to avoid this problem. "<=" is OK, but "<" followed by an alphanumeric is not.
Incidentally, your initial code doesn't seem to erase the area behind the tail as you move forward. If this is ok, the whole thing is even easier. Drop the snake array, and just keep track of the head of the snake. Just redraw the head once in each new position it moves to - you'll get an ever-lengthening "snake body" of rectangles left behind. Although in this method we can't easily tell if the snake collides with itself, as we've forgotten where the body is. Probably Mark's suggestion is best - just wanted to point out another possibility.

[This message has been edited by Jim Yingst (edited January 17, 2001).]
 
Chad LaVigne
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for fixing the loop, it really threw me when it didn't show up. Actually, I do have to erase the tail, I just left out that code, along with some other stuff in an effort to keep the posting a little smaller. Thanks for all of the help.
 
Marius Holm
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chad!
Also, you can specify the area to redraw (with a method called clipRect()..in the Graphics class(?)) That way you save yourself the work of repainting the part of the screen that do not change. Speeds up a lot!
Marius
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!