-
Notifications
You must be signed in to change notification settings - Fork 0
/
PONG_Game.pde
371 lines (320 loc) · 8.56 KB
/
PONG_Game.pde
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Set the width and height of the screen
int width = 800;
int height = 600;
// Declare variables for the ball and bats
Ball b1;
Bat s1, s2;
int player1Score = 0;
int player2Score = 0;
int rounds = 0;
int maxRounds = 10; // Maximum number of rounds per level
boolean gameOver = false; // Creating a boolean to track game over state
// Variables for power-ups
boolean powerUpActive = false;
int powerUpX;
int powerUpY;
int powerUpType; // 0: Increased paddle size, 1: Faster ball speed, 2: Temporary invincibility
int powerUpDuration = 500; // Duration of power-up effect in frames
int powerUpTimer = 0;
void settings() {
size(width, height);
}
void setup() {
// Create a new ball at the center of the screen
b1 = new Ball(width / 2, height / 2, 20); // speedX and speedY
// Create bats for player 1 and player 2
s1 = new Bat(0, height / 2 - 50, 20, 100); // y position
s2 = new Bat(width - 20, height / 2 - 50, 20, 100); // y position
// Initialize power-up
resetPowerUp();
}
void draw() {
background(0);
if (!gameOver) { // Check if the game is not over
b1.move();
checkPowerUpCollision();
}
// Bat1 Movement
s1.move();
s2.move();
s1.plot();
s2.plot();
// Draw a dotted line to separate the players' sides
drawDottedLine();
// Display the ball
b1.draw();
// Display player scores
displayScores();
// Check if 10 rounds have passed
if (rounds >= maxRounds) {
displayWinner(); // Display the winner
gameOver = true; // Set game over state to true
}
// Display "Game Over" if the game is over
if (gameOver) {
displayGameOver(); // Display "Game Over" after 10 rounds
}
// Display power-up if active
if (powerUpActive) {
drawPowerUp();
}
// Update power-up timer
if (powerUpTimer > 0) {
powerUpTimer--;
if (powerUpTimer == 0) {
resetPowerUp();
}
}
}
// Draw a dotted line to separate the players' sides
void drawDottedLine() {
stroke(255); // Set stroke color to white
strokeWeight(2); // Set stroke weight
for (int i = 0; i < height; i += 10) {
line(width / 2, i, width / 2, i + 5); // Draw the dotted line
}
}
// Display player scores
void displayScores() {
fill(255); // Set fill color to white
textSize(32); // Set text size
textAlign(CENTER, CENTER); // Set text alignment to center
text("Player 1: " + player1Score, width / 4, 50); // Display player 1 score
text("Player 2: " + player2Score, 3 * width / 4, 50); // Display player 2 score
}
// Display the winner of the game
void displayWinner() {
String winner;
if (player1Score > player2Score) {
winner = "Player 1 wins!";
} else if (player2Score > player1Score) {
winner = "Player 2 wins!";
} else {
winner = "It's a draw!";
}
fill(255); // Set fill color to white
textSize(48); // Set text size
textAlign(CENTER, CENTER); // Set text alignment to center
text(winner, width / 2, height / 2); // Display the winner
}
// Display "Game Over"
void displayGameOver() {
fill(255); // Set fill color to white
textSize(48); // Set text size
textAlign(CENTER, CENTER); // Set text alignment to center
text("Game Over", width / 2, height / 2 + 50); // Display "Game Over"
}
// Draw power-up
void drawPowerUp() {
fill(255, 255, 0); // Set fill color to yellow
noStroke(); // Remove stroke
ellipse(powerUpX, powerUpY, 20, 20); // Draw power-up as a circle
}
// Check collision with power-up
void checkPowerUpCollision() {
float distance = dist(b1.x, b1.y, powerUpX, powerUpY);
if (distance < 20) {
applyPowerUpEffect();
resetPowerUp();
}
}
// Apply power-up effect
void applyPowerUpEffect() {
// Choose a random power-up type
powerUpType = int(random(3));
// Apply power-up effect based on type
switch (powerUpType) {
case 0: // Increased paddle size
s1.h += 20;
s2.h += 20;
break;
case 1: // Faster ball speed
b1.dx *= 1.5;
b1.dy *= 1.5;
break;
case 2: // Temporary invincibility
powerUpTimer = powerUpDuration;
break;
}
}
// Reset power-up
void resetPowerUp() {
powerUpActive = true;
powerUpX = int(random(20, width - 20));
powerUpY = int(random(20, height - 20));
}
void keyPressed() {
if (key == 'w') {
s1.dy = -5;
} else if (key == 's') {
s1.dy = 5;
}
if (key == 'o') {
s2.dy = -5;
} else if (key == 'k') {
s2.dy = 5; // Fixed to positive value
}
if (key == 'a') {
s2.dx = -5;
} else if (key == 'd') {
s2.dx = 5;
}
if (key == 'i') {
s1.dx = -5;
} else if (key == 'l') {
s1.dx = 5;
}
}
void keyReleased() {
if (key == 'w' || key == 's') {
s1.dy = 0;
}
if (key == 'o' || key == 'k') {
s2.dy = 0;
}
if (key == 'a' || key == 'd') {
s2.dx = 0;
}
if (key == 'i' || key == 'l') {
s1.dx = 0;
}
}
// Class for the Ball
class Ball {
float x; // x-coordinate of the ball
float y; // y-coordinate of the ball
float dx; // Speed of movement in the x-direction for the ball
float dy; // Speed of movement in the y-direction for the ball
float d; // Dimensions of the ball
// Constructor
Ball(float x, float y, float d) {
this.x = x;
this.y = y;
this.d = d;
this.dx = 5; // speed of ball horizontally
this.dy = 5; // speed of ball vertically
}
// Method to update the position of the ball
void move() {
x = x + dx;
y = y + dy;
// Ball collision with top/bottom of screen
if (y <= 0 || y >= height - d) {
dy = -dy;
}
// Ball collision with bats
if ((x + d / 2 >= s2.x && x + d / 2 <= s2.x + s2.w && y >= s2.y && y <= s2.y + s2.h) ||
(x - d / 2 <= s1.x + s1.w && x - d / 2 >= s1.x && y >= s1.y && y <= s1.y + s1.h)) {
dx = -dx;
}
// Ball out of bounds
if (x < 0) {
player2Score++; // Player 2 scores
resetBall();
rounds++; // Increment round count
} else if (x > width) {
player1Score++; // Player 1 scores
resetBall();
rounds++; // Increment round count
}
// Increase ball speed if it stays within the borders
if (x > 0 && x < width && y > 0 && y < height) {
dx *= 1.001; // Increase speed in the x-direction
dy *= 1.001; // Increase speed in the y-direction
}
}
// Reset the ball position and speed
void resetBall() {
x = width / 2;
y = height / 2;
dx = 5;
dy = 5;
}
// Method to draw the ball
void draw() {
fill(255, 255, 255); // Set fill color to white
ellipse(x, y, d, d);
}
}
// Class for the Bat
class Bat {
float x;
float y;
float dx;
float dy;
float w;
float h;
// Constructor
Bat(float x, float y, float w, float h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.dx = 0; // Initialize bat movement speed in the x-direction
this.dy = 0; // Initialize bat movement speed in the y-direction
}
// Method to update the position of the bat
void move() {
x = x + dx;
y = y + dy;
// Ensure the bat stays within the boundaries of the screen
if (x < 0) {
x = 0;
}
if (x > width - w) {
x = width - w;
}
if (y < 0) {
y = 0;
}
if (y > height - h) {
y = height - h;
}
}
// Method to draw the bat
void plot() {
fill(255, 255, 255); // Set fill color to white
rect(x, y, w, h); // Draw the bat
}
}
// Class for the Obstacle
class Obstacle {
float x;
float y;
float dx;
float dy;
float w;
float h;
// Constructor
Obstacle(float x, float y, float w, float h, float dx, float dy) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.dx = dx; // Horizontal speed of obstacle
this.dy = dy; // Vertical speed of obstacle
}
// Method to move the obstacle
void move() {
x += dx;
y += dy;
// If obstacle goes out of screen, reset its position
if (x > width) {
x = -w; // Reset to the left side of the screen
}
if (x + w < 0) {
x = width; // Reset to the right side of the screen
}
if (y > height) {
y = -h; // Reset to the top of the screen
}
if (y + h < 0) {
y = height; // Reset to the bottom of the screen
}
}
// Method to draw the obstacle
void draw() {
fill(255, 0, 0); // Set fill color to red
rect(x, y, w, h); // Draw the obstacle
}
}