forked from APCSLowell/AsteroidsGame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsteroidsGame.pde
79 lines (67 loc) · 1.61 KB
/
AsteroidsGame.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
Spaceship bob = new Spaceship();
Star[] nightSky = new Star[200];
ArrayList <Asteroid> Bunch;
ArrayList <Bullet> Ammo = new ArrayList <Bullet>();
public void setup() {
size(500,500);
background(0);
Bunch = new ArrayList <Asteroid>();
bob.accelerate(0.2);
for (int i = 0; i < nightSky.length; i++) {
nightSky[i] = new Star();
}
for (int i = 0; i < 20; i++) {
Bunch.add(new Asteroid());
}
}
public void draw() {
background(0);
bob.move();
bob.show();
for (int i = 0; i < nightSky.length; i++) {
nightSky[i].show();
}
for (int i = 0; i < Bunch.size(); i++) {
Bunch.get(i).move();
Bunch.get(i).show();
if (dist((int)Bunch.get(i).getCenterX(), (int)Bunch.get(i).getCenterY(), (int)bob.getCenterX(), (int)bob.getCenterY()) <20){
Bunch.remove(i);
i--;
}
}
for (int i = 0; i < Ammo.size(); i++){
Ammo.get(i).move();
Ammo.get(i).show();
}
for (int i = 0; i < Ammo.size(); i++){
for (int j = 0; j < Bunch.size(); j++){
if (dist((int)Bunch.get(j).getCenterX(), (int)Bunch.get(j).getCenterY(), (int)Ammo.get(i).getCenterX(), (int)Ammo.get(i).getCenterY()) <20){
Bunch.remove(j);
Ammo.remove(i);
j--;
break;
}
}
}
}
public void keyPressed() {
if(key == 'w') {
bob.accelerate(0.2);
bob.accelerate(0.2);
}
if(key == 'r') {
bob.setXspeed(0);
bob.setYspeed(0);
bob.setCenterX((int)(Math.random()*500));
bob.setCenterY((int)(Math.random()*500));
}
if(key == 'd') {
bob.turn(20);
}
if(key == 'a') {
bob.turn(-20);
}
if (key == ' ') {
Ammo.add(new Bullet(bob));
}
}