-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvader.pde
72 lines (63 loc) · 1.4 KB
/
Invader.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
class Invader {
int x, w, h;
float y, moveToY;
int counter = 0;
int xSpeed = 1;
float ySpeed;
int value = 30;
Bullet currentBullet;
boolean randDir;
boolean dead = false;
Player p1;
Invader (int xPos, float yPos, int iWidth, int iHeight, Player player, float yspeed, boolean randomDirection) {
x = xPos;
y = yPos;
w = iWidth;
h = iHeight;
randDir = randomDirection;
if (randDir) {
if (int(random(2)) == 0) {
xSpeed *= -1;
}
}
ySpeed = yspeed;
p1 = player;
moveToY = yPos;
}
void update() {
x += xSpeed;
if (y < moveToY) {
y += ySpeed;
if (y >= height -200) {
p1.lives = 0;
}
}
}
void moveDown() {
moveToY += 30;
}
void shoot() {
if (currentBullet != null){ // if bullet exists, update bullet
currentBullet.update();
currentBullet.show();
if (currentBullet.y > height){ // out off the screen
currentBullet = null;
}
} else { // no bullet exsists, random chance at shooting
int r = int(random(600));
if (r == 0) {
currentBullet = new Bullet(x +w/2, int(y) + h);
}
}
}
void show() {
if (!dead) {
stroke(255, 0, 0);
if (currentBullet != null) { // blue stroke when shooting
stroke( 0, 0, 255);
}
fill(100, 0, 0);
rect(x, y, w, h);
}
}
}