Search code examples
javagraphicspixel

Speed in a java 2d game


I got a game where every 15 milliseconds, a ball moves 1 pixel in a direction.

If I wanted to cut it's speed in half, what would I do? Pixels can't be floats, can they?

Thanks


Solution

  • You would normally store the ball's precise position as a float, but round the coordinates to integers when you are drawing.

    So: give the ball class some kind of float-coordinate member (eg. Point2D.Float or two floats), update your paint method to round this coordinates and draw the ball there.

    The result enables you to give the ball any speed you like.

    If you give us some code, we might also help you with implementation.