-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPONG.java
More file actions
160 lines (129 loc) · 4.5 KB
/
Copy pathPONG.java
File metadata and controls
160 lines (129 loc) · 4.5 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PONG extends JPanel{
Ball ball;
Paddle paddleOne;
Paddle paddleTwo;
int playerOneScore = 0;
int playerTwoScore = 0;
Timer gameTimer;
ActionListener gameTimerListener;
KeyListener playerOneInput;
KeyListener playerTwoInput;
public static void main(String[] args) {
JFrame window = new JFrame("PONG");
window.setContentPane(new PONG());
window.setSize(3840, 2160);
window.setUndecorated(true);
window.setVisible(true);
}
PONG() {
ball = new Ball(1920-Ball.SIZE, 1080-Ball.SIZE);
paddleOne = new Paddle(0, 0);
paddleTwo = new Paddle(3840-Paddle.WIDTH, 0);
gameTimerListener = e -> {
ball.x+=ball.xVelocity;
ball.y+=ball.yVelocity;
if (ball.y>=2160-Ball.SIZE || ball.y<=0) {
ball.yVelocity *=-1;
}
if (ball.x>=3840-Paddle.WIDTH-Ball.SIZE || ball.x<Paddle.WIDTH) {
if ( (ball.y>=paddleOne.y && ball.y<=paddleOne.y+Paddle.HEIGHT-Ball.SIZE) || (ball.y>=paddleTwo.y && ball.y<=paddleTwo.y+Paddle.HEIGHT-Ball.SIZE) )
ball.xVelocity *= -1;
else
{
if (ball.x>=3840-Paddle.WIDTH-Ball.SIZE)
playerOneScore++;
else
playerTwoScore++;
ball = new Ball(1920-Ball.SIZE, 1080-Ball.SIZE);
ball.xVelocity = randomVelocity();
ball.yVelocity = randomVelocity();
}
}
repaint();
};
playerTwoInput = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (paddleTwo.y-Paddle.OFFSET>=0) {
paddleTwo.y-=Paddle.OFFSET;
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (paddleTwo.y+Paddle.OFFSET+Paddle.HEIGHT<=2160) {
paddleTwo.y+=Paddle.OFFSET;
}
}
}
};
playerOneInput = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_S) {
if (paddleOne.y+Paddle.OFFSET+Paddle.HEIGHT<=2160) {
paddleOne.y+=Paddle.OFFSET;
}
}
if (e.getKeyCode() == KeyEvent.VK_W) {
if (paddleOne.y-Paddle.OFFSET>=0) {
paddleOne.y-=Paddle.OFFSET;
}
}
}
};
addKeyListener(playerOneInput);
addKeyListener(playerTwoInput);
gameTimer=new Timer(5, gameTimerListener);
gameTimer.start();
}
private int randomVelocity() {
int direction = ( (int) (Math.random()*2) ) == 0 ? 1 : -1 ;
return direction*( (int) (Math.random()*10+1) )*2;
}
public void paintComponent(Graphics g) {
requestFocus();
g.setColor(Color.BLACK);
g.fillRect(0, 0, 3840, 2160);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 200));
g.drawLine(1870, 0, 1870, 150);
g.drawString(playerOneScore+"", 1700, 150);
g.drawString(playerTwoScore+"", 1940, 150);
g.drawString("PONG", 1600, 2150);
ball.draw(g);
paddleOne.draw(g);
paddleTwo.draw(g);
}
class Ball {
final static int SIZE = 40;
int x, y;
int xVelocity, yVelocity;
void draw(Graphics g) {
g.setColor(Color.RED);
g.fillOval(x, y, SIZE, SIZE);
}
Ball(int x, int y) {
this.x = x;
this.y = y;
xVelocity = randomVelocity();
yVelocity = randomVelocity();
}
}
class Paddle {
final static int WIDTH = 20;
final static int HEIGHT = 216;
final static int OFFSET = 40;
int x, y;
void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(x, y, WIDTH, HEIGHT);
}
Paddle(int x, int y) {
this.x = x;
this.y = y;
}
}
}