-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamePanel.java
221 lines (191 loc) · 8.14 KB
/
GamePanel.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* This GamePanel class acts as the main game loop, continuously running
* the game, listening to keyboard input, updating the graphics, and
* checking if any win conditions are met. It uses threading to multitask.
*
* @author Anthony Chen
* @version 1.0
* @since 2022-05-27
*/
/* GamePanel class acts as the main "game loop" - continuously runs the game and calls whatever needs to be called
Child of JPanel because JPanel contains methods for drawing to the screen
Implements KeyListener interface to listen for keyboard input
Implements Runnable interface to use "threading" - let the game do two things at once
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class GamePanel extends JPanel implements Runnable, KeyListener{
// dimensions of window
public static final int GAME_WIDTH = 1024;
public static final int GAME_HEIGHT = 512;
public Thread gameThread;
public Image image;
public Graphics graphics;
public PlayerBall ball;
public PlayerPaddle paddleOne;
public PlayerPaddle paddleTwo;
public int scoreOne = 0;
public int scoreTwo = 0;
public boolean oneWin;
public boolean twoWin;
public GamePanel(){
ball = new PlayerBall(GAME_WIDTH/2, GAME_HEIGHT/2, getRandomSpeed() * 2, getRandomSpeed()); // create a player controlled ball, set start location to middle of screen
paddleOne = new PlayerPaddle(0, 224, 'w', 's'); // creates two player paddles with controls
paddleTwo = new PlayerPaddle(1016, 224, 'k', 'm');
this.setFocusable(true); // make everything in this class appear on the screen
this.addKeyListener(this); // start listening for keyboard input
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT)); // set window size
// make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time!
gameThread = new Thread(this);
gameThread.start();
}
// paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself
public void paint(Graphics g){
// we are using "double buffering here" - if we draw images directly onto the screen, it takes time and the human eye can actually notice flashes of lag as each pixel on the screen is drawn one at a time. Instead, we are going to draw images OFF the screen, then simply move the image on screen as needed.
image = createImage(GAME_WIDTH, GAME_HEIGHT); // draw off screen
graphics = image.getGraphics(); // draw scores with custom font
graphics.setFont(new Font("Roboto", Font.PLAIN, 64));
graphics.setColor(Color.white);
graphics.drawString(scoreOne + "", 402, 64);
graphics.drawString(scoreTwo + "", 542, 64);
if (scoreOne >= 8 && scoreTwo < 8) { // give players super paddle
if (paddleOne.speed == 8) { // speeds up paddle
paddleOne.speed = 16;
System.out.println("Player 1 gets super paddle!");
}
} else if (scoreTwo >= 8 && scoreOne < 8) {
if (paddleTwo.speed == 8) { // speeds up other paddle
paddleTwo.speed = 16;
System.out.println("Player 2 gets super paddle!");
}
}
if (scoreOne == 12) { // check if player one or two wins
System.out.println("Player 1 wins!");
oneWin = true;
} else if (scoreTwo == 12) {
System.out.println("Player 2 wins!");
twoWin = true;
}
if (oneWin) { // show winning message for either player
image = createImage(GAME_WIDTH, GAME_HEIGHT); // draw off screen
graphics = image.getGraphics();
graphics.setFont(new Font("Roboto", Font.PLAIN, 64));
graphics.setColor(Color.white);
graphics.drawString("Player 1 wins!", 32, 64);
} else if (twoWin) {
image = createImage(GAME_WIDTH, GAME_HEIGHT); // draw off screen
graphics = image.getGraphics();
graphics.setFont(new Font("Roboto", Font.PLAIN, 64));
graphics.setColor(Color.white);
graphics.drawString("Player 2 wins!", 32, 64);
}
draw(graphics); // update the positions of everything on the screen
g.drawImage(image, 0, 0, this); // move the image on the screen
}
// call the draw methods in each class to update positions as things move
public void draw(Graphics g){
for (int i = 0; i < 512; i += 16) { // draw central line of squares
g.setColor(Color.white);
g.fillRect(508, i, 8, 8);
}
ball.draw(g); // draw balls and paddles
paddleOne.draw(g);
paddleTwo.draw(g);
}
// call the move methods in other classes to update positions
// this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy
public void move(){
ball.move();
paddleOne.move();
paddleTwo.move();
}
// handles all collision detection and responds accordingly
public void checkCollision(){
// bounce ball off top and bottom of screen
if(ball.y <= 0){
ball.y = 0;
ball.yVelocity = -ball.yVelocity;
}
if(ball.y >= GAME_HEIGHT - PlayerBall.BALL_DIAMETER){
ball.y = GAME_HEIGHT - PlayerBall.BALL_DIAMETER;
ball.yVelocity = -ball.yVelocity;
}
// increase score if ball gets past paddle
if(ball.x <= -8){ // add offset to properly track score
scoreTwo += 1;
ball = new PlayerBall(GAME_WIDTH/2, GAME_HEIGHT/2, PlayerBall.speed, getRandomSpeed()); // re-create a ball since someone just scored
}
if(ball.x + PlayerBall.BALL_DIAMETER >= GAME_WIDTH){
scoreOne += 1;
ball = new PlayerBall(GAME_WIDTH/2, GAME_HEIGHT/2, -PlayerBall.speed, getRandomSpeed()); // re-create a ball since someone just scored
}
if (paddleOne.y <= 0) { // stop paddles from moving off screen
paddleOne.y = 0;
}
if (paddleOne.y >= 448) {
paddleOne.y = 448;
}
if (paddleTwo.y <= 0) {
paddleTwo.y = 0;
}
if (paddleTwo.y >= 448) {
paddleTwo.y = 448;
}
if (ball.intersects(paddleOne)) { // bounce balls off paddle
ball.xVelocity = -ball.xVelocity;
}
if (ball.intersects(paddleTwo)) {
ball.xVelocity = -ball.xVelocity;
}
}
// run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){
// the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
long lastTime = System.nanoTime();
double amountOfTicks = 60;
double ns = 1000000000/amountOfTicks;
double delta = 0;
long now;
while(true){ // this is the infinite game loop
now = System.nanoTime();
delta = delta + (now-lastTime)/ns;
lastTime = now;
// only move objects around and update screen if enough time has passed
if(delta >= 1){
move();
checkCollision();
repaint();
delta--;
if (oneWin || twoWin) { // pause the game for a long time if someone wins
try {
TimeUnit.SECONDS.sleep(86400);
} catch (InterruptedException e) {
}
}
}
}
}
// pass key events to paddles
public void keyPressed(KeyEvent e){
paddleOne.keyPressed(e);
paddleTwo.keyPressed(e);
}
public void keyReleased(KeyEvent e){
paddleOne.keyReleased(e);
paddleTwo.keyReleased(e);
}
// we have to include keytyped
public void keyTyped(KeyEvent e){
}
// method to get random speed and direction that's not zero
public int getRandomSpeed() {
int result = 0;
while (result == 0) {
result = ThreadLocalRandom.current().nextInt(-PlayerBall.speed, PlayerBall.speed);
}
return result;
}
}