forked from APCSLowell/AsteroidsGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsteroidsGame.pde
155 lines (131 loc) · 3.48 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
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
Star[] nightSky = new Star[100];
Spaceship spaceship = new Spaceship();
ArrayList <Asteroid> AsteroidsGroup = new ArrayList <Asteroid>();
ArrayList <Bullet> bullets = new ArrayList <Bullet>();
private double MaxBullets = 10;
private int health = 100;
private int level = 1;
private int numOfAsteroids = 10;
private double numOfBullets = 10;
private int score = 0;
private int MaxHealth = 100;
public void setup()
{
size(800,600);
background(0);
for(int i = 0; i < 100; i++) {
nightSky[i] = new Star();
}
for(int i = 0; i < numOfAsteroids; i++) {
Asteroid asteroids = new Asteroid();
AsteroidsGroup.add(i,asteroids);
}
}
public void draw()
{
background(0);
for(int i = 0; i < 100; i++) {
nightSky[i].show();
}
spaceship.show();
spaceship.move();
if(numOfAsteroids == 0) {
level += 1;
MaxHealth += 10;
health = MaxHealth;
numOfAsteroids += 3 * level + 10;
MaxBullets += 10;
for(int i = 0; i <= numOfAsteroids; i++) {
Asteroid asteroids = new Asteroid();
AsteroidsGroup.add(i,asteroids);
}
}
for(int i = 0; i < numOfAsteroids; i++) {
if(dist((float)spaceship.myCenterX,(float)spaceship.myCenterY, (float)AsteroidsGroup.get(i).myCenterX,(float)AsteroidsGroup.get(i).myCenterY) <= 20) {
AsteroidsGroup.remove(i);
numOfAsteroids --;
score ++;
health -= 10;
break;
}
else {
AsteroidsGroup.get(i).show();
AsteroidsGroup.get(i).move();
}
}
numOfBullets += 0.01 + 0.01*level;
fill(192);
rect(10,10, 200, 20); //Health Bar
fill(255,0,0);
rect(10,10,(200 * health/MaxHealth),20);
fill(192);
rect(10,40, 200,20);
fill(0,0,255); //bullet bar;
rect(10,40, (int)(200* (numOfBullets/MaxBullets)),20);
textSize(25);
fill(192);
text("Level : " + level, 10,90);
text("Score : " + score, 10, 120);
if(numOfBullets >= MaxBullets) {
numOfBullets = MaxBullets;
}
if(numOfBullets <= 0) {
numOfBullets = 0;
}
if(health <= 0) {
textSize(100);
fill(255,0,0);
text("GAME OVER", 100 ,340);
health = 0;
numOfBullets = 0;
}
for(int i = 0; i < bullets.size(); i++) {
bullets.get(i).show();
bullets.get(i).move();
if(bullets.get(i).myCenterX == 800 || bullets.get(i).myCenterX == 0) {
bullets.remove(i);
break;
}
if(bullets.get(i).myCenterY == 600 || bullets.get(i).myCenterY == 0) {
bullets.remove(i);
break;
}
for(int j = 0; j < AsteroidsGroup.size(); j++) {
if(dist((float)bullets.get(i).getMyCenterX(),(float)bullets.get(i).getMyCenterY(), (float)AsteroidsGroup.get(j).myCenterX,(float)AsteroidsGroup.get(j).myCenterY) <= 20) {
AsteroidsGroup.remove(j);
bullets.remove(i);
score ++;
numOfAsteroids --;
break;
}
}
}
}
double degrees = 0;
public void keyPressed() {
if(key == 'A' || key == 'a') {
spaceship.turn(10);
}
if(key == 'D' || key == 'd') {
spaceship.turn(-10);
}
if(key == 'W' || key == 'w'){
spaceship.accelerate(0.1);
}
if(key == 'S' || key == 's'){
if(spaceship.myXspeed >= 0 && spaceship.myYspeed >= 0){
spaceship.accelerate(-0.1);
}
}
if(key == 'Q' || key == 'q') {
spaceship.myCenterX = Math.random()*800;
spaceship.myCenterY = Math.random()*600;
spaceship.myPointDirection = Math.random()*360;
spaceship.myXspeed = 0;
spaceship.myYspeed = 0;
}
if(key == ' ' && numOfBullets > 1) {
bullets.add(new Bullet(spaceship));
numOfBullets --;
}
}