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 (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-22T05:09:42.000Z (over 2 years ago)
- Last Synced: 2025-01-31T06:08:27.740Z (3 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 GameMain Menu
![]()
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 callupdate
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
andupdate
methods to check the frames per second(fps). - Import
Graphics2D
to create Graphicsg2
inWindow
method and update g2 inupdate
method.
3-Handling User Input
- Create new class
KL
to implement KeyListener. Create methodskeyPressed
,keyReleased
andisKeyPressed
. - Initialize new KL
keyListener
in Window class and use usingthis.addKeyListener(keyListener)
inpublic Window
. - Add
keyListener
to listen forKeyEvents
inupdate
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
inpublic Window
. Draw them inupdate
method.
5-Making the Player Controller
- Implement the PlayerController to move the player and ai.
- Create new class
PlayerController
. - Initialize
PlayerController
inWindow
. - For smoother gameplay:
- Changed all int values to double values.
- In
update
method, create an imagedbImage
for double buffer image. Then create a Graphicsdbg
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
andINSETS_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
, initializeBall ball
and make sure it is updated inupdate
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 aAIController
foraiController
. 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
/rightScoreText
as 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
Texts
forstartGame
,exitGame
andpongTitle
and draw them. - Create new class
ML
for Mouse Listener to listen for mouse hover/click on the texts. Create and initializeML
inMainMenu
. - In
update
inMainMenu
add code to change color when hovering over startGame and exitGame. - Update
Main
for GameWindow(1) or MainMenu(0) states. Create methodchangeState
to change states. - In
MainMenu
, update mouse hover withMain.changeState(1);
. Addthis.dispose();
torun
method when game state is changed to close MainMenu window. - In
Window
, addthis.dispose();
torun
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.