forked from cohfe/ChloeW-AsteroidsGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsteroidsGame.pde
More file actions
65 lines (57 loc) · 1.33 KB
/
AsteroidsGame.pde
File metadata and controls
65 lines (57 loc) · 1.33 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
Spaceship ship;
Star[] myStars;
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
int screenSize = 1000;
public void setup()
{
size(1000, 1000);
ship = new Spaceship(screenSize/2, screenSize/2);
myStars = new Star[200];
for (int i = 0; i < myStars.length; i++) {
myStars[i] = new Star((int)(Math.random()*screenSize), (int)(Math.random()*screenSize));
}
for (int i = 0; i < 10; i++) {
asteroids.add(new Asteroid());
}
}
public void draw()
{
background(0, 0, 0);
for (int i = 0; i < myStars.length; i++) {
myStars[i].show();
}
for (int i = 0; i < asteroids.size(); i++) {
Asteroid a = asteroids.get(i);
a.show();
a.move();
}
ship.show();
ship.move();
checkAsteroidCollisions();
}
public void keyPressed()
{
if (key == 'r') {
ship.hyperspace((int)(Math.random()*screenSize), (int)(Math.random()*screenSize));
}
if (key == 'w') {
ship.accelerate(1);
}
if (key == 'd') {
ship.turnLeft();
}
if (key == 'u') {
ship.turnRight();
}
}
void checkAsteroidCollisions() {
for (int i = asteroids.size() - 1; i >= 0; i--) {
Asteroid a = asteroids.get(i);
double dx = ship.getCenterX() - a.getCenterX();
double dy = ship.getCenterY() - a.getCenterY();
double dista = Math.sqrt(dx*dx + dy*dy);
if (dista < 25) {
asteroids.remove(i);
}
}
}