-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asteroids.pde
199 lines (184 loc) · 4.6 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
import java.util.function.Predicate;
Ship ship;
ArrayList<FlyingThing> flyingThings = new ArrayList<FlyingThing>();
boolean keyLeft, keyRight, keyUp, keyDown;
GameState state = GameState.START;;
int minAsteroids = 1;
int nextAsteroidWave;
int score;
void setup() {
size(500, 500);
}
void initGame() {
flyingThings = new ArrayList<FlyingThing>();
ship = new Ship(new PVector(width/2, height/2),
new PVector());
flyingThings.add(ship);
nextAsteroidWave = 3;
score = 0;
genAsteroids(nextAsteroidWave);
state = GameState.PLAYING;
}
void draw() {
background(0);
noFill();
stroke(255);
switch (state) {
case GAMEOVER:
textAlign(CENTER);
textSize(40);
text("GAME OVER!\n(press enter)", width/2, height/2);
break;
case START:
textAlign(CENTER);
textSize(60);
text("ASTEROIDS", width/2, 100);
textSize(30);
text("Press (Enter) to start a new game", width/2, height/2);
text("(O) To open saved game", width/2, height/2+80);
text("(S) To save game", width/2, height/2+110);
text("(ARROWS) for movement", width/2, height/2+140);
text("(SPACE) to fire", width/2, height/2+170);
text("(P) to pause", width/2, height/2+200);
break;
case PAUSE:
textAlign(CENTER);
textSize(60);
text("ASTEROIDS", width/2, 100);
textSize(30);
text("Press (P) to unpause game", width/2, height/2);
break;
case PLAYING:
textAlign(LEFT);
textSize(18);
text("SCORE: "+score, 5, 20);
for (FlyingThing thing: flyingThings) {
if (thing.alive) {
thing.update();
thing.render();
}
}
checkKeys();
checkCollision();
if (countAsteroids() < minAsteroids) {
nextAsteroidWave += 1;
genAsteroids(nextAsteroidWave);
}
break;
}
}
int countAsteroids() {
int count = 0;
for (FlyingThing thing: flyingThings) {
if (thing instanceof Asteroid) count++;
}
return count;
}
void checkCollision() {
for (FlyingThing thing: flyingThings) {
if (thing.alive) {
if (thing instanceof Asteroid && ship.checkCollision((Asteroid)thing)) {
state = GameState.GAMEOVER;
}
if (thing instanceof Bullet) {
for (FlyingThing thing2: flyingThings) {
if (thing2.alive && thing2 instanceof Asteroid) {
Bullet b = (Bullet)thing;
Asteroid a = (Asteroid)thing2;
if (b.pos.dist(a.pos) < (a.size+10)/2) {
a.alive = false;
score += Math.round(a.size);
}
}
}
}
}
}
flyingThings.removeIf((new Predicate<FlyingThing>() {
public boolean test(FlyingThing thing) {
return !thing.alive;
}
}));
}
public void genAsteroids(int numAsteroids) {
for (int i = 0; i < numAsteroids; i++) {
flyingThings.add(new Asteroid());
}
}
void checkKeys() {
if (keyLeft) {
ship.angle -= radians(2);
}
if (keyRight) {
ship.angle += radians(2);
}
if (keyUp) {
ship.thrustForward(.05);
}
if (keyDown) {
ship.thrustForward(-.05);
}
}
void keyPressed() {
if (key == CODED) {
switch (keyCode) {
case UP:
keyUp = true;
break;
case DOWN:
keyDown = true;
break;
case LEFT:
keyLeft = true;
break;
case RIGHT:
keyRight = true;
break;
}
} else {
if (key == ' ') {
Bullet b = ship.fire();
flyingThings.add(b);
} else if ((key == ENTER || key == RETURN) && (state==GameState.GAMEOVER || state==GameState.START)) {
initGame();
} else if (key == 'p' && state!=GameState.GAMEOVER && state==GameState.PAUSE) {
state = GameState.PLAYING;
} else if (key == 'p' && state!=GameState.GAMEOVER && state==GameState.PLAYING) {
state = GameState.PAUSE;
} else if (key == 's') {
saveGame();
} else if (key == 'o') {
loadGame();
}
}
}
void keyReleased() {
switch (keyCode) {
case UP:
keyUp = false;
break;
case DOWN:
keyDown = false;
break;
case LEFT:
keyLeft = false;
break;
case RIGHT:
keyRight = false;
break;
}
}
void saveGame() {
System.out.println("saving...");
GameWriter writer = new GameWriter();
writer.writeGame(flyingThings, nextAsteroidWave, score);
}
void loadGame() {
System.out.println("loading...");
GameReader reader = new GameReader();
reader.readGame();
flyingThings = reader.flyingThings;
ship = reader.ship;
nextAsteroidWave = reader.nextAsteroidWave;
score = reader.score;
}