Dot Animation
When reading about programming concepts, I don't want to just read about them. I want use them. Sometimes I imagine a full featured game that uses the concepts and I happily spend several months creating that. At other times I don't have a full application in mind, but I still want to test out what I've been reading about. So I create a demo program as I try out the concepts.
These Dot Animation programs are all similar, but with some differences. I wrote these to work with JavaFX animation and then I had to create a method for controlling the red dot - first with the arrow keys and then with the mouse.
Here is the jar file:
These Dot Animation programs are all similar, but with some differences. I wrote these to work with JavaFX animation and then I had to create a method for controlling the red dot - first with the arrow keys and then with the mouse.
Here is the jar file:
![]()
|
Note: Requires Java on your computer to run
|
To the right is the scheme I came up with for the arrow key navigation. U=Up Arrow, D=Down, L=Left, R=Right The blue arrows indicate the direction the red dot is already moving when an arrow key is pressed. Note that the player cannot just change direction by 180 degrees. They have to turn right or left instead. For example, when the player is going Up and presses the Down arrow, the red dot slows but does not stop or start to go down. |
Here is some of the Java code I wrote to accomplish the arrow key movement.
if (keyEvent.getCode() == KeyCode.LEFT && !paused) {
ball = redBall;
switch (ballDirection) {
case UP:
ball.dx = -redBallSpeed / 1.5; // Slow the speed on the diagonal
ball.dy = -redBallSpeed / 1.5;
ballDirection = UP_LEFT;
break;
case UP_RIGHT:
ball.dx = 0;
ball.dy = -redBallSpeed; ;
ballDirection = UP;
break;
case RIGHT:
ball.dx = redBallSpeed / 3; // Slow down, but don't change direction
ball.dy = 0;
ballDirection = RIGHT;
break;
case DOWN_RIGHT:
ball.dx = 0;
ball.dy = redBallSpeed;
ballDirection = DOWN;
break;
case DOWN:
ball.dx = -redBallSpeed / 1.5; // Slow the speed on the diagonal
ball.dy = redBallSpeed / 1.5;
ballDirection = DOWN_LEFT;
break;
case DOWN_LEFT:
ball.dx = -redBallSpeed; ;
ball.dy = 0;
ballDirection = LEFT;
break;
case LEFT:
ball.dx = -redBallSpeed; ;
ball.dy = 0;
ballDirection = LEFT;
break;
case UP_LEFT:
ball.dx = -redBallSpeed; ;
ball.dy = 0;
ballDirection = LEFT;
break;
}
// Change the ball's coordinates
ball.setCenterX(ball.dx + ball.getCenterX());
ball.setCenterY(ball.dy + ball.getCenterY());
if (keyEvent.getCode() == KeyCode.LEFT && !paused) {
ball = redBall;
switch (ballDirection) {
case UP:
ball.dx = -redBallSpeed / 1.5; // Slow the speed on the diagonal
ball.dy = -redBallSpeed / 1.5;
ballDirection = UP_LEFT;
break;
case UP_RIGHT:
ball.dx = 0;
ball.dy = -redBallSpeed; ;
ballDirection = UP;
break;
case RIGHT:
ball.dx = redBallSpeed / 3; // Slow down, but don't change direction
ball.dy = 0;
ballDirection = RIGHT;
break;
case DOWN_RIGHT:
ball.dx = 0;
ball.dy = redBallSpeed;
ballDirection = DOWN;
break;
case DOWN:
ball.dx = -redBallSpeed / 1.5; // Slow the speed on the diagonal
ball.dy = redBallSpeed / 1.5;
ballDirection = DOWN_LEFT;
break;
case DOWN_LEFT:
ball.dx = -redBallSpeed; ;
ball.dy = 0;
ballDirection = LEFT;
break;
case LEFT:
ball.dx = -redBallSpeed; ;
ball.dy = 0;
ballDirection = LEFT;
break;
case UP_LEFT:
ball.dx = -redBallSpeed; ;
ball.dy = 0;
ballDirection = LEFT;
break;
}
// Change the ball's coordinates
ball.setCenterX(ball.dx + ball.getCenterX());
ball.setCenterY(ball.dy + ball.getCenterY());