-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiPong5.pde
More file actions
282 lines (237 loc) · 6.68 KB
/
miPong5.pde
File metadata and controls
282 lines (237 loc) · 6.68 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
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
import processing.sound.*;
SoundFile gol;
SoundFile pala;
float ph, pw; //alto y ancho de las palas
int puntos1, puntos2; //contadores de puntuación
float inc; //incremento de velocidad (= dificultad)
float dedo; //espacio para el dedo en la pantalla
int maxP=5; //puntuación máxima (fin de la partida)
float radio;
float vmin, vmax;
float pvinit; //velocidad inicial de la pala IA
float difvb,difvp; //diferenciales de velocidad bola y pala
Bola bola;
Pala palaIA;
Pala palaPlayer;
void setup() {
//size(1024,640); //tamaño pantalla
fullScreen();
//orientation(LANDSCAPE);
//noSmooth()
//noCursor(); //desactivado para ANDROID
dedo = 60*displayDensity;
ph = 30*displayDensity;
pw = 5*displayDensity;
radio = 6*displayDensity;
inc = 0.5*displayDensity; //incremento de velocidades (dificultad)
vmin = 3.5*displayDensity/3; //velocidad inicial de la bola
vmax = 4.5*displayDensity;
pvinit = 2*displayDensity; //velocidad inicial de la pala
difvb = 0.07*displayDensity; //diferencial de la velocidad de la bola
difvp = 0.1*displayDensity; //diferencial de la velocidad de la pala
gol = new SoundFile(this, "gol1.wav"); //mp3 ANDROID
pala = new SoundFile(this, "pala1.wav"); //wav windows
//noStroke();
//fill(255);
//color(255,255,255);
stroke(255);
rectMode(RADIUS);
frameRate(45);
bola = new Bola();
palaIA = new Pala(pw, true);
palaPlayer = new Pala(width-pw-dedo, false);
strokeWeight(displayDensity);
background(0, 0, 0);
textSize(20*displayDensity);
fill(255);
text("Touch to start", width/4, height/3);
text("(swipe finger\nover grey zone)", width/4, height/2);
noLoop();
}
void mousePressed() {
loop();
}
void draw() {
background(0, 0, 0);
//zona para el dedo
noStroke();
fill(128);
rect(width-pw, height/2, dedo-pw, height);
//stroke(255);
fill(200);
triangle(width-dedo/2, 20*displayDensity,
width-dedo/6, 80*displayDensity,
width-dedo/6*5, 80*displayDensity);
triangle(width-dedo/2, height-20*displayDensity,
width-dedo/6, height-80*displayDensity,
width-dedo/6*5, height-80*displayDensity);
stroke(255);
line(width-dedo/2, 80*displayDensity,
width-dedo/2, height-80*displayDensity);
fill(255);
//límites del campo
line(0, 0, width-dedo, 0);
line(0, height-1, width-dedo, height-1);
//red
line((width-dedo)/2, 0, (width-dedo)/2, height);
//println(round(frameRate) + "fps");
//movimiento de la bola
bola.mover();
palaIA.tocar(bola);
palaPlayer.tocar(bola);
palaIA.mover();
palaPlayer.mover();
bola.checkPunto();
//mostrar puntuación
text(nf(puntos1, 2), (width-dedo)/2-50*displayDensity, 20*displayDensity);
text(nf(puntos2, 2), (width-dedo)/2+30*displayDensity, 20*displayDensity);
if (puntos1==maxP || puntos2==maxP) ganador();
}
class Bola { //propiedades
float x;
float y;
float r = radio;
float vx;
float vy;
boolean reset=false;
int signo;
Bola() { //constructor
//posición inicial de la bola
x = (width-dedo)/2;
y = height/2;
//velocidad inicial de la bola
signo = ceil(random(-1, 1));
if (signo == 0) {
signo = -1;
}
vx = random(vmin-1, vmax-1)*signo;
signo = ceil(random(-1, 1));
if (signo == 0) {
signo = -1;
}
vy = random(vmin-1, vmax-1)*signo;
}
//métodos
void mover() {
ellipse(x, y, r*2, r*2);
x += vx;
y += vy;
//rebotes arriba y abajo
if ( y > height-r || y < r ) {
vy *= -1; //cambiamos la dirección
x += vx;
y += vy;
}
}
void checkPunto() {
int check = 0;
if (x < 0) {
puntos2++;
palaIA.pv += difvp*3; //la IA mejora con cada gol que le metemos
check=1;
}
if (x > (width-dedo)) {
puntos1++;
palaIA.pv += difvp; //la IA mejora, pero menos, con cada gol que mete
check=-1;
}
if (check != 0) {
gol.play();
//posición inicial de la bola
x = (width-dedo)/2;
y = height/2;
//reiniciamos la dificultad pero a la vez la aumentamos
inc = 1*displayDensity/3 + inc/2;
//velocidad de la bola
vx = (random(vmin, vmax)+inc/2)*check; //en x signo no aleatorio,saca el que pierde
check = ceil(random(-1, 1));
if (check == 0) {
check = -1;
}
vy = (random(vmin, vmax)+inc/2)*check;
//noLoop();
}
}
}
void ganador() {
textSize(18*displayDensity);
if (puntos1 > puntos2) {
fill(0, 255, 0, 180);
text("Winner", (width-dedo)/6, height/4);
//fill(255, 0, 0, 180);
//text("Tu pierdes", 4*(width-dedo)/6, height/4);
} else {
//fill(255, 0, 0, 180);
//text("IA Pierde", (width-dedo)/6, height/4);
fill(0, 255, 0, 180);
text("Winner", (width-dedo)/2+(width-dedo)/6, height/4);
}
noLoop();
fill(255);
text("Touch to\nstart again", (width-dedo)/2+10, height/2+135);
puntos1=0;
puntos2=0;
}
class Pala { //propiedades
float x;
float y;
float w = pw;
float h = ph;
boolean IA;
float pv=pvinit; //velocidad inicial de la pala IA
int check;
float dif;
Pala(float px, boolean pIA) { //constructor
x = px;
y = height/2;
IA = pIA;
}
//métodos
void mover() {
rect(x, y, w, h);
if (IA) { //movimiento de la pala IA
if (y < bola.y && bola.y > h ) {
y += pv;
}
if (y > bola.y && bola.y < height-h ) {
y -= pv;
}
} else { //moviemiento de la pala del jugador
y = mouseY;
}
//la dibujamos
}
void tocar(Bola bola) {
check=0;
dif=bola.y-y; //para ver por qué parte de la pala golpeamos la bola
if (IA) {
//rebotes en la pala de la IA
if (bola.x < bola.r+2*w && abs(dif)<h) {
check = -1;
}
} else {
//rebotes en la pala del judador
if (bola.x > width-bola.r-2*w-dedo && abs(dif)<h) {
check = 1;
}
}
if (check!=0) {
pala.play();
bola.vx += inc*check; //aumentamos la velocidad horizontal (= dificultad)
bola.vx *= -1; //y la invertimos
/*
if (check==-1) {
bola.vy = map(dif,-h,h,-abs(bola.vx),abs(bola.vx)); //para darle efecto a la bola
} else {
bola.vy = map(dif,-h,h,-abs(bola.vx),abs(bola.vx)); //para darle efecto a la bola
}
*/
bola.vy = map(dif, -h, h, -abs(bola.vx), abs(bola.vx)); //para darle efecto a la bola
bola.x += bola.vx;
bola.y += bola.vy;
inc += difvb; //incrementamos el diferencial de dificultad
//println("1: ",dif1,h, bola.vx, map(dif1,-h,h,-abs(bola.vx),abs(bola.vx)));
//println("2: ",dif2,h, bola.vx, map(dif2,-h,h,-abs(bola.vx),abs(bola.vx)));
}
}
}