-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pollito_Pong.pde
538 lines (461 loc) · 14.1 KB
/
Pollito_Pong.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/********* POLLITO PONG *********/
// 0: PANTALLA INICIAL
// 1: PANTALLA DE JUEGO(IZQUIERDA USUARIO, DERECHA IA)
// 2: PANTALLA PUNTUACIÓN
// 3: PANTALLA RESUMEN
// IMAGEN DEL POLLITO
PImage pollito;
// VARIABLES DEL JUEGO
//para el usuario
int gameScreen = 0;
Pelota pltuser = new Pelota();
Raqueta rckuser = new Raqueta();
Escenario escuser = new Escenario(color(153, 54, 54));
boolean gameOverUser = false;
//para la IA (JR)
Pelota pltjr = new Pelota();
Raqueta rckjr = new Raqueta();
Escenario escjr = new Escenario(color(44, 62, 80));
boolean gameOverJr = false;
// AJUSTES DEL JUEGO
float gravity = .3;
float airfriction = 0.00001;
float friction = 0.1;
// DIVISIÓN DE LAS PANTALLAS
PGraphics user, jr, div;
/*****************************************************************************************************
* CREACIÓN DE LAS CLASES
*****************************************************************************************************/
/************************* CLASE PELOTA ***********************************/
public class Pelota{
// Configuración inicial
float ballX = width/4;
float ballY = 0;
float ballSpeedVert = 0;
float ballSpeedHorizon = 0;
float ballSize = 30;
color ballColor = color(0);
// Puntaje
int score = 0;
int maxHealth = 100;
float health = maxHealth;
float healthDecrease = 1;
int healthBarWidth = 60;
void drawBall(PGraphics screen) {
screen.imageMode(CENTER);
screen.image(pollito, ballX, ballY, ballSize, ballSize);
}
void applyGravity() {
ballSpeedVert += gravity;
ballY += ballSpeedVert;
ballSpeedVert -= (ballSpeedVert * airfriction);
}
void applyHorizontalSpeed() {
ballX += ballSpeedHorizon;
ballSpeedHorizon -= (ballSpeedHorizon * airfriction);
}
// Mantener la pelota en pantalla
void keepInScreen() {
// Inferior
if (ballY+(ballSize/2) > height) {
makeBounceBottom(height);
}
// Superior
if (ballY-(ballSize/2) < 0) {
makeBounceTop(0);
}
// Izquierda
if (ballX-(ballSize/2) < 0) {
makeBounceLeft(0);
}
// Derecha
if (ballX+(ballSize/2) > width/2) {
makeBounceRight(width/2);
}
}
// ball falls and hits the floor (or other surface)
void makeBounceBottom(float surface) {
ballY = surface - (ballSize/2);
ballSpeedVert *= -1;
ballSpeedVert -= (ballSpeedVert * friction);
}
// ball rises and hits the ceiling (or other surface)
void makeBounceTop(float surface) {
ballY = surface + (ballSize/2);
ballSpeedVert *= -1;
ballSpeedVert -= (ballSpeedVert * friction);
}
// ball hits object from left side
void makeBounceLeft(float surface) {
ballX = surface + (ballSize/2);
ballSpeedHorizon *= -1;
ballSpeedHorizon -= (ballSpeedHorizon * friction);
}
// ball hits object from right side
void makeBounceRight(float surface) {
ballX = surface - (ballSize/2);
ballSpeedHorizon *= -1;
ballSpeedHorizon -= (ballSpeedHorizon * friction);
}
// noStroke no tener bordes
// fill = color de relleno
//rectMode(corner) dibujar el rextangulo desde la esquina
//health = vida
void drawHealthBar(PGraphics screen) {
screen.noStroke();
screen.fill(189, 195, 199);
screen.rectMode(CORNER);
screen.rect(this.ballX-(healthBarWidth/2), ballY - 30, healthBarWidth, 5);
if (health > 60) {
screen.fill(46, 204, 113);
} else if (health > 30) {
screen.fill(230, 126, 34);
} else {
screen.fill(231, 76, 60);
}
screen.rect(ballX-(healthBarWidth/2), ballY - 30, healthBarWidth*(health/maxHealth), 5);
}
//
void decreaseHealth() {
health -= healthDecrease;
if (health <= 0) {
gameOver(); //Llamar al método en la clase principal
}
}
}
/*************************** CLASE RAQUETA ***********************************/
class Raqueta{
// Configuración incial
float racketX = 0;
float racketY = height;
color racketColor = color(0);
float racketWidth = 100;
float racketHeight = 10;
int IASpeed = 25;
void drawRacket(PGraphics screen) {
screen.fill(racketColor);
screen.rectMode(CENTER);
if (screen == user){
if(mouseX < 600){
screen.rect(mouseX, mouseY, racketWidth, racketHeight, 5);
}else{
screen.rect(600, mouseY, racketWidth, racketHeight, 5);
}
}else{
screen.rect(racketX, racketY, racketWidth, racketHeight, 5);
}
}
void watchRacketBounce(Pelota ball) {
float x;
float y;
float overhead;
if(ball == pltuser){
overhead = mouseY - pmouseY;
x = mouseX;
y = mouseY;
}else{
overhead = -1;
x = racketX;
y =racketY;
}
if ((ball.ballX+(ball.ballSize/2) > x-(racketWidth/2)) && (ball.ballX-(ball.ballSize/2) < x+(racketWidth/2))) {
if (dist(ball.ballX, ball.ballY, ball.ballX, y)<=(ball.ballSize/2)+abs(overhead)) {
ball.makeBounceBottom(y);
ball.ballSpeedHorizon = (ball.ballX - x)/10;
// Si se mueve hacia arriba
if (overhead<0) {
ball.ballY+=(overhead/2);
ball.ballSpeedVert+=(overhead/2);
}
}
}
}
void IARaqueta(int posx, int miny, int ancho, Pelota ball){
if(racketX<ball.ballX+ball.ballSize/2){
racketX += IASpeed;
}
if(racketX>ball.ballX+ball.ballSize/2){
racketX -= IASpeed;
}
if(racketX+racketWidth/2 < posx+ancho && racketX+racketWidth/2 > posx-60){
if (racketY < miny-10){
racketY += IASpeed;
}
if (racketY > miny-10){
racketY -= IASpeed;
}
}
if(racketY < ball.ballY){
racketY = ball.ballY - 5;
}
}
}
/************ CLASE ESCENARIO ******************/
class Escenario{
// wall settings
int wallSpeed = 5;
int wallInterval = 1000;
float lastAddTime = 0;
int minGapHeight = 200;
int maxGapHeight = 300;
int wallWidth = 80;
color wallColors = color(44, 62, 80);
// This arraylist stores data of the gaps between the walls. Actuals walls are drawn accordingly.
// [gapWallX, gapWallY, gapWallWidth, gapWallHeight, scored]
ArrayList<int[]> walls = new ArrayList<int[]>();
Escenario(color col){
wallColors = col;
}
void wallAdder(PGraphics screen) {
if (millis()-lastAddTime > wallInterval) {
int randHeight = round(random(minGapHeight, maxGapHeight));
int randY = round(random(0, height-randHeight));
// {gapWallX, gapWallY, gapWallWidth, gapWallHeight, scored}
int[] randWall = {width/2, randY, wallWidth, randHeight, 0};
walls.add(randWall);
lastAddTime = millis();
}
}
void wallHandler(PGraphics screen, Pelota ball) {
for (int i = 0; i < walls.size(); i++) {
wallRemover(i);
wallMover(i);
wallDrawer(i, screen);
watchWallCollision(i, ball);
}
}
void wallRemover(int index) {
int[] wall = walls.get(index);
if (wall[0]+wall[2] <= 0) {
walls.remove(index);
}
}
void wallMover(int index) {
int[] wall = walls.get(index);
wall[0] -= wallSpeed;
}
void wallDrawer(int index, PGraphics screen) {
int[] wall = walls.get(index);
// Obtener la configuración del obstáculo
int gapWallX = wall[0];
int gapWallY = wall[1];
int gapWallWidth = wall[2];
int gapWallHeight = wall[3];
// Dibujar el obstáculo
screen.rectMode(CORNER);
screen.noStroke();
screen.strokeCap(ROUND);
screen.fill(wallColors);
screen.rect(gapWallX, 0, gapWallWidth, gapWallY, 0, 0, 15, 15);
screen.rect(gapWallX, gapWallY+gapWallHeight, gapWallWidth, height-(gapWallY+gapWallHeight), 15, 15, 0, 0);
if(screen == jr){
rckjr.IARaqueta(gapWallX, gapWallY+gapWallHeight, gapWallWidth, pltjr);
}
}
void watchWallCollision(int index, Pelota ball) {
int[] wall = walls.get(index);
// get gap wall settings
int gapWallX = wall[0];
int gapWallY = wall[1];
int gapWallWidth = wall[2];
int gapWallHeight = wall[3];
int wallScored = wall[4];
int wallTopX = gapWallX;
int wallTopY = 0;
int wallTopWidth = gapWallWidth;
int wallTopHeight = gapWallY;
int wallBottomX = gapWallX;
int wallBottomY = gapWallY+gapWallHeight;
int wallBottomWidth = gapWallWidth;
int wallBottomHeight = height-(gapWallY+gapWallHeight);
if (
(ball.ballX+(ball.ballSize/2)>wallTopX) &&
(ball.ballX-(ball.ballSize/2)<wallTopX+wallTopWidth) &&
(ball.ballY+(ball.ballSize/2)>wallTopY) &&
(ball.ballY-(ball.ballSize/2)<wallTopY+wallTopHeight)
) {
ball.decreaseHealth();
}
if (
(ball.ballX+(ball.ballSize/2)>wallBottomX) &&
(ball.ballX-(ball.ballSize/2)<wallBottomX+wallBottomWidth) &&
(ball.ballY+(ball.ballSize/2)>wallBottomY) &&
(ball.ballY-(ball.ballSize/2)<wallBottomY+wallBottomHeight)
) {
ball.decreaseHealth();
}
if (ball.ballX > gapWallX+(gapWallWidth/2) && wallScored==0) {
wallScored=1;
wall[4]=1;
ball.score++;
}
}
}
/***************************************************************************************************
* PROGRAMACIÓN DEL JUEGO
***************************************************************************************************/
/********* SETUP *********/
void setup() {
size(1210, 500);
user = createGraphics(width/2 -5, height);
jr = createGraphics(width/2 -5, height);
div = createGraphics(10, height);
pollito = loadImage("assets/pollito.png");
smooth();
}
/********* DRAW BLOCK *********/
void draw() {
image(user, 0, 0);
image(jr, width/2 +5, 0);
image(div, width/2 -5, 0);
// Display the contents of the current screen
if (gameScreen == 0) {
initScreen();
} else if (gameScreen == 1) {
gameScreen();
} else if (gameScreen == 2) {
if(pltuser.health == 0){
gameOverScreen(user, pltuser);
}
if(pltjr.health == 0){
gameOverScreen(jr, pltjr);
}
} else if (gameScreen == 3) {
resumeScreen();
}
}
/********* SCREEN CONTENTS *********/
void initScreen() {
background(177, 240, 190);
textAlign(CENTER);
fill(52, 73, 94);
textSize(70);
text("Pollito Pong", width/2, height/2);
textSize(15);
text("Click para iniciar", width/2, height-30);
}
void gameScreen() {
// DIBUJAR PANTALLA DEL USUARIO
gameUser();
// DIBUJAR PANTALLA DE IA
gameIA();
//Barra en el centro de la Pantalla
drawDiv();
}
void gameOverScreen(PGraphics screen, Pelota ball) {
screen.beginDraw();
screen.textAlign(CENTER);
screen.fill(236, 240, 241);
screen.textSize(12);
if(screen==user){
screen.background(44, 62, 80);
screen.text("Tu puntuación:", width/4, height/2 - 120);
gameOverUser = true;
}else{
screen.background(153, 54, 54);
screen.text("Puntuación con IA:", width/4, height/2 - 120);
gameOverJr = true;
}
screen.textSize(130);
screen.text(ball.score, width/4, height/2);
screen.textSize(15);
screen.text("Click para finalizar partida", width/4, height-30);
screen.endDraw();
if (!gameOverUser){
gameUser();
}
if (!gameOverJr){
gameIA();
}
drawDiv();
}
void resumeScreen(){
background(44, 62, 80);
textAlign(CENTER);
fill(236, 240, 241);
textSize(100);
text("GAME OVER", width/2, height/4);
textSize(25);
text("Tu puntuación", width/4, height/2.2);
text("Puntuación con IA", width-width/4, height/2.2);
textSize(50);
text(pltuser.score, width/4, height/1.6);
text(pltjr.score, width-width/4, height/1.6);
textSize(20);
text("Click para reiniciar", width/2, height-30);
}
/******** JUEGO ************/
void gameIA(){
jr.beginDraw();
jr.background(222, 193, 193);
rckjr.drawRacket(jr);
rckjr.watchRacketBounce(pltjr);
pltjr.drawBall(jr);
pltjr.applyGravity();
pltjr.applyHorizontalSpeed();
pltjr.keepInScreen();
pltjr.drawHealthBar(jr);
printScore(pltjr, jr);
escjr.wallAdder(jr);
escjr.wallHandler(jr, pltjr);
jr.endDraw();
}
void gameUser(){
user.beginDraw();
user.background(211, 217, 227);
rckuser.drawRacket(user);
rckuser.watchRacketBounce(pltuser);
pltuser.drawBall(user);
pltuser.applyGravity();
pltuser.applyHorizontalSpeed();
pltuser.keepInScreen();
pltuser.drawHealthBar(user);
printScore(pltuser, user);
escuser.wallAdder(user);
escuser.wallHandler(user, pltuser);
user.endDraw();
}
void drawDiv(){
div.beginDraw();
div.background(0,0,0);
div.endDraw();
}
/****************************** INPUTS ****************************/
public void mousePressed() {
// if we are on the initial screen when clicked, start the game
if (gameScreen==0) {
startGame();
}else if (gameScreen==2) {
resumeGame();
}else if (gameScreen==3) {
restart();
}
}
/************************ FUNCIONES DE CONTROL *******************/
void printScore(Pelota ball, PGraphics screen) {
screen.textAlign(CENTER);
screen.fill(0);
screen.textSize(30);
screen.text(ball.score, height/2, 50);
}
void restart() {
pltuser = new Pelota();
pltjr = new Pelota();
escuser.lastAddTime = 0;
escjr.lastAddTime = 0;
escuser.walls.clear();
escjr.walls.clear();
gameScreen = 1;
gameOverUser = false;
gameOverJr = false;
}
/************************* OTRAS FUNCIONES ************************/
void startGame() {
gameScreen=1;
}
void gameOver() {
gameScreen=2;
}
void resumeGame(){
gameScreen=3;
}