-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroids.pde
306 lines (271 loc) · 7.29 KB
/
Asteroids.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
/* This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/
Nave nave;
float naveVel = 2;
float anguloRotacion = .2;
float balaVel = 15;
int numAsteroids = 1;
int darkMostrador;
int darkMostradorLimite = 24*2;
int MAX_VIDAS = 3;
int vidas;
int escenario = -1;
int diffCurva = 2;
int inicioRadio = 50;
PImage[] asteroidesImagenes = new PImage[3];
PImage rocket, img, logo;
//float bgColor = 0;
ArrayList<Escape> escape;
ArrayList<Escape> fuego;
ArrayList<Bala> balas;
ArrayList<Asteroid> asteroids;
boolean arribaPresion = false;
boolean abajoPresion = false;
boolean aPresion = false;
boolean dPresion = false;
PFont font;
void setup(){
//background(bgColor);
size(856, 527);
font = createFont("data/Fonts/BookAntiqua-Bold-48.vlw", 32);
asteroidesImagenes[0] = loadImage("data/Images/Asteroid0.png");
asteroidesImagenes[1] = loadImage("data/Images/Asteroid1.png");
asteroidesImagenes[2] = loadImage("data/Images/Asteroid2.png");
rocket = loadImage("data/Images/Rocket.png");
logo = loadImage("data/Images/logo.png");
frameRate(30);
vidas = 3;
asteroids = new ArrayList<Asteroid>(0);
img = loadImage("data/Images/Fondo.png");
}
void draw(){
image(img, 0, 0);
if( vidas >= 0 && asteroids.size()>0){
float theta = T2D(nave.rotacion)+PI/2;
nave.actualizar(escape, fuego);
nave.bordes();
nave.hacer();
if(nave.verColision(asteroids)){
vidas--;
nave = new Nave();
}
if(aPresion){
rotate2D(nave.rotacion,-anguloRotacion);
//ellipse(100,100,100,100);
}
if(dPresion){
rotate2D(nave.rotacion, anguloRotacion);
}
if(arribaPresion){
nave.acceleracion = new PVector(0, naveVel);
rotate2D(nave.acceleracion, theta);
}
for(Escape e: escape){
e.actualizar();
e.render();
}
for(Escape e: fuego){
e.actualizar();
e.render();
}
for(int i = 0; i < balas.size(); i++){
balas.get(i).bordes();
if(balas.get(i).actualizar()){
balas.remove(i);
i--;
}
if(i < 0){
break;
}
balas.get(i).hacer();
if(balas.get(i).verColision(asteroids)){
balas.remove(i);
i--;
}
}
while(escape.size() > 20){
escape.remove(0);
}
while(fuego.size()>6){
fuego.remove(0);
}
while(balas.size() > 30){
balas.remove(0);
}
for(Asteroid a : asteroids){
a.actualizar();
a.bordes();
a.hacer();
}
for(int i = 0; i < vidas; i++){
image(rocket,40*i + 10,nave.r*1.5,2*nave.r,3*nave.r);
}
} else if(vidas < 0){
if(darkMostrador < darkMostradorLimite){
// background(0);
img = loadImage("data/Images/Fondo.png");
darkMostrador++;
for(Asteroid a : asteroids){
a.actualizar();
a.bordes();
a.hacer();
}
fill(0, 255-(darkMostradorLimite-darkMostrador)*3);
rect(0,0,width,height);
} else {
//background(0);
img = loadImage("data/Images/Fondo.png");
for(Asteroid a : asteroids){
a.actualizar();
a.bordes();
a.hacer();
}
image(rocket,width/2 - 5 * nave.r,height/2-7.5*nave.r,10*nave.r,15*nave.r);
textFont(font, 33);
fill(0, 200);
text("\nPERDISTE", width/2-80-2, height*.75-1);
fill(0, 200);
textFont(font, 32);
fill(#FC0303);
text("\nPERDISTE", width/2-80, height*.75);
fill(#F0F0F0);
text("Version 1", width/2-90-2, height*.75-1);
textFont(font, 16);
fill(0, 200);
text("\nHaga click para jugar de nu7evo", width/2-80-2, height*.9-1);
textFont(font, 15);
fill(255);
text("\nHaga click para jugar nuevamente", width/2-80, height*.9);
}
} else {
//background(0);
img = loadImage("data/Images/Fondo.png");
nave = new Nave();
nave.hacer();
textFont(font, 32);
fill(255);
if(escenario > -1){
text("Escenario " + (escenario + 1) + " completo", width/2-120, height/2);
} else {
image(logo, 120, 50);
}
textFont(font, 15);
fill(255);
textSize(30);
text("Haga click para jugar el nivel " + (escenario + 2), 240, 300);
}
}
void mousePressed(){
if(vidas < 0){
escenario = -1;
vidas = 3;
asteroids = new ArrayList<Asteroid>(0);
} else if (asteroids.size()==0){
escenario++;
reset();
}
}
void reset(){
nave = new Nave();
escape = new ArrayList<Escape>();
fuego = new ArrayList<Escape>();
balas = new ArrayList<Bala>();
asteroids = new ArrayList<Asteroid>();
for(int i = 0; i <numAsteroids + diffCurva*escenario; i++){
PVector position = new PVector((int)(Math.random()*width), (int)(Math.random()*height-100));
asteroids.add(new Asteroid(position, inicioRadio, asteroidesImagenes, escenario));
}
darkMostrador = 0;
}
void disparoBalas(){
PVector pos = new PVector(0, nave.r*2);
rotate2D(pos,T2D(nave.rotacion) + PI/2);
pos.add(nave.posicion);
PVector vel = new PVector(0, balaVel);
rotate2D(vel, T2D(nave.rotacion) + PI/2);
balas.add(new Bala(pos, vel));
}
void keyPressed(){
if(key==CODED){
if(keyCode==UP){
arribaPresion=true;
} else if(keyCode==DOWN){
abajoPresion=true;
} else if(keyCode == LEFT){
aPresion = true;
}else if(keyCode==RIGHT){
dPresion = true;
}
}
if(key == 'a'){
aPresion = true;
}
if(key=='d'){
dPresion = true;
}
if(key=='w'){
arribaPresion=true;
}
if(key=='s'){
abajoPresion=true;
}
}
void keyReleased(){
if(key==CODED){
if(keyCode==UP){
arribaPresion=false;
nave.acceleracion = new PVector(0,0);
} else if(keyCode==DOWN){
abajoPresion=false;
nave.acceleracion = new PVector(0,0);
} else if(keyCode==LEFT){
aPresion = false;
} else if(keyCode==RIGHT){
dPresion = false;
}
}
if(key=='a'){
aPresion = false;
}
if(key=='d'){
dPresion = false;
}
if(key=='w'){
arribaPresion=false;
nave.acceleracion = new PVector(0,0);
}
if(key=='s'){
abajoPresion=false;
nave.acceleracion = new PVector(0,0);
}
if(key == ' '){
disparoBalas();
}
}
float T2D(PVector pvect){
return (float)(Math.atan2(pvect.y, pvect.x));
}
void rotate2D(PVector v, float theta) {
float xTemp = v.x;
v.x = v.x*cos(theta) - v.y*sin(theta);
v.y = xTemp*sin(theta) + v.y*cos(theta);
}