-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceInvaders.pde
146 lines (127 loc) · 2.64 KB
/
SpaceInvaders.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
Ship ship;
SpawnDirector director;
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Alien> aliens = new ArrayList<Alien>();
ArrayList<AlienBullet> alienBullets = new ArrayList<AlienBullet>();
final int scaleFactor = 10;
final int alienSpawnRate = 50;
int score = 0;
boolean gameOver = false;
void setup()
{
background(000);
size(600, 800);
ship = new Ship();
director = new SpawnDirector(alienSpawnRate);
}
void draw()
{
if(gameOver == false)
{
background(000);
ship.act();
director.act();
for(AlienBullet ab : alienBullets)
{
ab.act();
if(checkAlienBulletHit(ship, ab) == true)
{
alienBullets.remove(ab);
gameOver();
break;
}
}
//gets the aliens in the horde to act
for(Alien a : aliens)
{
a.act();
if(checkAlienHit(ship, a) == true)
{
aliens.remove(a);
gameOver();
break;
}
}
if(bullets.size() > 0)
{
Bullet b = bullets.get(bullets.size() - 1);
b.act();
for(Alien a : aliens)
{
if(checkBulletHit(b, a) == true)
{
score++;
bullets.remove(b);
aliens.remove(a);
break;
}
}
}
}
}
void mousePressed()
{
Bullet shipBullet = new Bullet(ship.getcx(), ship.getcy(), 10);
bullets.add(shipBullet);
}
boolean checkBulletHit(Bullet bullet, Alien alien)
{
int bx = bullet.getcx();
int by = bullet.getcy();
int ax = alien.getcx();
int ay = alien.getcy();
int distance = (int) (Math.sqrt(Math.pow((bx - ax), 2) + Math.pow((by - ay), 2)));
if(distance <= (alien.getCollisionRadius() + bullet.getCollisionRadius()))
{
return true;
}
else
{
return false;
}
}
boolean checkAlienHit(Ship ship, Alien alien)
{
int sx = ship.getcx();
int sy = ship.getcy();
int ax = alien.getcx();
int ay = alien.getcy();
int distance = (int) (Math.sqrt(Math.pow((sx - ax), 2) + Math.pow((sy - ay), 2)));
if(distance <= (alien.getCollisionRadius() + ship.getCollisionRadius()))
{
return true;
}
else
{
return false;
}
}
boolean checkAlienBulletHit(Ship ship, AlienBullet alienBullet)
{
int sx = ship.getcx();
int sy = ship.getcy();
int abx = alienBullet.getcx();
int aby = alienBullet.getcy();
int distance = (int) (Math.sqrt(Math.pow((sx - abx), 2) + Math.pow((sy - aby), 2)));
if(distance <= (alienBullet.getCollisionRadius() + ship.getCollisionRadius()))
{
return true;
}
else
{
return false;
}
}
void gameOver()
{
ship.switchCanDraw();
director.switchCanSpawn();
aliens.clear();
alienBullets.clear();
bullets.clear();
System.out.println(score);
gameOver = true;
textSize(50);
fill(255, 255, 255);
text("Score: " + score, 100, 100);
}