An open API service indexing awesome lists of open source software.

https://github.com/timothyjan/java-pong

Java Pong Game
https://github.com/timothyjan/java-pong

java jframe

Last synced: about 1 month ago
JSON representation

Java Pong Game

Awesome Lists containing this project

README

        

# Java-Pong
Java Pong Game

Main Menu


Main Menu

Gameplay


Gameplay

1-Creating the Window


  • Create Window using JFrame methods in Window.java.

  • Create Constants.java to hold constant values.

  • Create thread to run window.

2-The Game Loop


  • In Window.java, we want to call update method every frame of our game and we want to pass in how long it took to do the last frame.

  • Create Time.java to call Java's system get nano time to get the best time measurement on the OS we are running on.

  • Added code to run and update methods to check the frames per second(fps).

  • Import Graphics2D to create Graphics g2 in Window method and update g2 in update method.

3-Handling User Input


  • Create new class KL to implement KeyListener. Create methods keyPressed, keyReleased and isKeyPressed.

  • Initialize new KL keyListener in Window class and use using this.addKeyListener(keyListener) in public Window.

  • Add keyListener to listen for KeyEvents in update method.

4-Drawing the Player


  • Create paddles and ball on the screen and will use this to detect collisions.

  • Create class Rect for paddle and ball.

  • Create new Constants for width/height/color for paddle/ball and padding.

  • Initialize playerOne, ai, and ball as new Rects in public Window. Draw them in update method.

5-Making the Player Controller


  • Implement the PlayerController to move the player and ai.

  • Create new class PlayerController.

  • Initialize PlayerController in Window.

  • For smoother gameplay:

    • Changed all int values to double values.

    • In update method, create an image dbImage for double buffer image. Then create a Graphics dbg for double buffer graphics.

    • We will draw everything to this dbg which will not be displayed to the user.

    • Then we're going to return this dbg to our draw method and draw the image to the screen.

    • This way everything gets drawn incrementally on an off screen and then we swap buffers and show this buffer to the user.



  • Create new constants TOOLBAR_HEIGHT and INSETS_BOTTOM to prevent paddles going off screen.

  • Create new constant PADDLE_SPEED for use in paddle movement.

6a - Moving the Ball


  • Create new class Ball to handle Ball movement and collisions.

  • In Window.java, initialize Ball ball and make sure it is updated in update method. Update and draw ball.

6-AI Controller


  • In PlayerController,create a constructor for the AI that doesn't use the keyListener.

  • Create new class AIController. AI paddle will respond based on the ball being below or above the AI paddle.

  • In Window, initialize a AIController for aiController. Update and draw ai.

7-Keeping Score


  • Create new class Text.java.

  • Create new constants for x/y positions for scores and score font size.

  • In Window, create and initialize Text leftScoreText/rightScoreText.

  • In Ball, add Text leftScoreText/rightScoreText as arguments to Ball.

  • Create new constant for win score and add code to determine win condition after ball passes either paddle.

8-MainMenu


  • Create new class MainMenu. Create Texts for startGame, exitGame and pongTitle and draw them.

  • Create new class ML for Mouse Listener to listen for mouse hover/click on the texts. Create and initialize ML in MainMenu.

  • In update in MainMenu add code to change color when hovering over startGame and exitGame.

  • Update Main for GameWindow(1) or MainMenu(0) states. Create method changeState to change states.

  • In MainMenu, update mouse hover with Main.changeState(1);. Add this.dispose(); to run method when game state is changed to close MainMenu window.

  • In Window, add this.dispose(); to run method when game state is changed to close gameWindow.

  • In Ball, change the game state to 2 when player/ai wins and kill all threads.