Freitag Software
The Joy of Writing Software
  • Home
    • Links
  • My Software
    • JavaScript Programs >
      • Dots
      • The Code Cracker
      • The Deserted Ship
      • Flying Text
    • Java Programs >
      • Dot Animation
      • Operation Rescue >
        • Mazes for Programmers
      • Battleship!
      • Blackjack
      • Draw Poker
      • State Pattern Demo
      • Falling Blocks
    • Android Programs >
      • The Oracle
      • RPSLS
      • Gemini Falcon: Asteroid Miner
      • Gemini Falcon: All Boxed In >
        • Gemini Falcon >
          • Privacy Policy
          • A Game Oddity
      • Ay Caramba
      • Dots vs Dots
      • Ants vs Ants
    • Twine Stories
  • Random Thoughts
  • Book Reviews
  • Teaching
    • Real-Life Stories
    • Flying Text
  • About
    • Contact

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: 
dot_animation_v1.jar
File Size: 1928 kb
File Type: jar
Download File

Note: Requires Java on your computer to run
Picture
Picture
Picture


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. 
Picture
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()); 
Powered by Create your own unique website with customizable templates.