-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPongGameGUI.java
More file actions
136 lines (110 loc) · 3.79 KB
/
PongGameGUI.java
File metadata and controls
136 lines (110 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* 🎮 Pong Game GUI
*
* A classic 2-player Pong game implemented using Java Swing.
* Players control paddles to bounce the ball and score points.
*
* Features:
* - Two-player mode (Left paddle: W/S, Right paddle: Up/Down arrows)
* - Ball movement and collision detection
* - Score tracking
* - Simple restart functionality
*
* Complexity:
* - Time: O(1) per frame
* - Space: O(1) for ball and paddles
*
* Author: Pradyumn Pratap Singh (Strange)
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PongGameGUI extends JPanel implements KeyListener, ActionListener {
private int ballX = 250, ballY = 150, ballDX = 2, ballDY = 2, ballSize = 20;
private int paddle1Y = 100, paddle2Y = 100, paddleWidth = 10, paddleHeight = 80;
private int score1 = 0, score2 = 0;
private Timer timer;
public PongGameGUI() {
JFrame frame = new JFrame("🎮 Pong Game");
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.addKeyListener(this);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Timer for ball movement (refresh every 10ms)
timer = new Timer(10, this);
timer.start();
}
// Paint the game components
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Background
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
// Ball
g.setColor(Color.WHITE);
g.fillOval(ballX, ballY, ballSize, ballSize);
// Paddles
g.fillRect(10, paddle1Y, paddleWidth, paddleHeight);
g.fillRect(getWidth() - 20, paddle2Y, paddleWidth, paddleHeight);
// Scores
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Player 1: " + score1, 50, 30);
g.drawString("Player 2: " + score2, getWidth() - 150, 30);
}
// Move the ball and check collisions
@Override
public void actionPerformed(ActionEvent e) {
ballX += ballDX;
ballY += ballDY;
// Bounce off top/bottom walls
if (ballY <= 0 || ballY >= getHeight() - ballSize) ballDY = -ballDY;
// Paddle collision (left paddle)
if (ballX <= 20 && ballY + ballSize >= paddle1Y && ballY <= paddle1Y + paddleHeight) {
ballDX = -ballDX;
}
// Paddle collision (right paddle)
if (ballX + ballSize >= getWidth() - 20 && ballY + ballSize >= paddle2Y && ballY <= paddle2Y + paddleHeight) {
ballDX = -ballDX;
}
// Score for Player 2
if (ballX <= 0) {
score2++;
resetBall();
}
// Score for Player 1
if (ballX >= getWidth() - ballSize) {
score1++;
resetBall();
}
repaint();
}
// Reset ball to center
private void resetBall() {
ballX = getWidth() / 2 - ballSize / 2;
ballY = getHeight() / 2 - ballSize / 2;
ballDX = -ballDX; // Change direction
ballDY = 2;
}
// Key controls for paddles
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
// Player 1 (W/S)
if (key == KeyEvent.VK_W && paddle1Y > 0) paddle1Y -= 10;
if (key == KeyEvent.VK_S && paddle1Y < getHeight() - paddleHeight) paddle1Y += 10;
// Player 2 (Up/Down)
if (key == KeyEvent.VK_UP && paddle2Y > 0) paddle2Y -= 10;
if (key == KeyEvent.VK_DOWN && paddle2Y < getHeight() - paddleHeight) paddle2Y += 10;
repaint();
}
@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }
public static void main(String[] args) {
SwingUtilities.invokeLater(PongGameGUI::new);
}
}