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
- Host: GitHub
- URL: https://github.com/timothyjan/java-pong
- Owner: TimothyJan
- Created: 2023-01-19T09:14:41.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-22T05:09:42.000Z (almost 3 years ago)
- Last Synced: 2025-03-26T04:30:13.025Z (8 months ago)
- Topics: java, jframe
- Language: Java
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Java-Pong
Java Pong Game
Main Menu
Gameplay
1-Creating the Window
- Create Window using JFrame methods in
Window.java. - Create
Constants.javato hold constant values. - Create thread to run window.
2-The Game Loop
- In
Window.java, we want to callupdatemethod every frame of our game and we want to pass in how long it took to do the last frame. - Create
Time.javato call Java's system get nano time to get the best time measurement on the OS we are running on. - Added code to
runandupdatemethods to check the frames per second(fps). - Import
Graphics2Dto create Graphicsg2inWindowmethod and update g2 inupdatemethod.
3-Handling User Input
- Create new class
KLto implement KeyListener. Create methodskeyPressed,keyReleasedandisKeyPressed. - Initialize new KL
keyListenerin Window class and use usingthis.addKeyListener(keyListener)inpublic Window. - Add
keyListenerto listen forKeyEventsinupdatemethod.
4-Drawing the Player
- Create paddles and ball on the screen and will use this to detect collisions.
- Create class
Rectfor paddle and ball. - Create new Constants for width/height/color for paddle/ball and padding.
- Initialize playerOne, ai, and ball as new
Rectsinpublic Window. Draw them inupdatemethod.
5-Making the Player Controller
- Implement the PlayerController to move the player and ai.
- Create new class
PlayerController. - Initialize
PlayerControllerinWindow. - For smoother gameplay:
- Changed all int values to double values.
- In
updatemethod, create an imagedbImagefor double buffer image. Then create a Graphicsdbgfor 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
drawmethod 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_HEIGHTandINSETS_BOTTOMto prevent paddles going off screen. - Create new constant
PADDLE_SPEEDfor use in paddle movement.
6a - Moving the Ball
- Create new class
Ballto handle Ball movement and collisions. - In
Window.java, initializeBall balland make sure it is updated inupdatemethod. 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 aAIControllerforaiController. 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 initializeText leftScoreText/rightScoreText. - In
Ball, addText leftScoreText/rightScoreTextas arguments toBall. - 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
TextsforstartGame,exitGameandpongTitleand draw them. - Create new class
MLfor Mouse Listener to listen for mouse hover/click on the texts. Create and initializeMLinMainMenu. - In
updateinMainMenuadd code to change color when hovering over startGame and exitGame. - Update
Mainfor GameWindow(1) or MainMenu(0) states. Create methodchangeStateto change states. - In
MainMenu, update mouse hover withMain.changeState(1);. Addthis.dispose();torunmethod when game state is changed to close MainMenu window. - In
Window, addthis.dispose();torunmethod when game state is changed to close gameWindow. - In
Ball, change the game state to 2 when player/ai wins and kill all threads.