-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.pde
98 lines (89 loc) · 2.3 KB
/
plane.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
public class Plane{
// variables
public boolean flyUp;
private float x, y, yVelo;
private int nextPipe = 0;
private PImage img;
private float planeSize = 10;
// plane constructor
public Plane(){
// load plane image and set drawing image to center
img = loadImage("img/p1.png");
imageMode(CENTER);
}
// reset plane before game with new coordinates
public void resetPlane(float startX, float startY){
x = startX;
y = startY;
yVelo = 0;
flyUp = false;
}
// return y position
public float getY(){
return y;
}
// return angle of plane for trail
public float getAngle(){
return - (yVelo / (4 *PI));
}
// draw plane graphics
public void drawPlane(){
fill(100,100,100);
// rotate plane based on speed then draw
translate(x+10, height-y);
rotate(- (yVelo / (4 *PI)));
image(img, 0, 0);
resetMatrix();
}
// change plane's velocity
public void flyPlane(){
// if space bar held, slowly increase the velocity
if(flyUp){
yVelo += 0.3;
// set max velocity
if(yVelo > 15){
yVelo = 15;
}
}
// otherwise, slowly decrease velo
else {
yVelo -= 0.3;
if(yVelo < -15){
yVelo = -15;
}
}
// smoothly minimise flying at the top of the screen
if(height - y -(2*planeSize)< 120 && yVelo > (height-(2*planeSize)-y)/8){
yVelo = (height-y-(2*planeSize))/8;
}
// change y value vy velocity
y += yVelo;
}
// check collisions
public boolean checkCollisions(){
// if in the water
if(y<0){
// play sound effect and return true
splash.play();
return true;
}
// check if colliding with closest pipe on screen
if(pipes[nextPipe%pipes.length].getX() < x){
// check if collides (120 is gapWidth, 25 is strokeWidth
if( y + planeSize > pipes[nextPipe%pipes.length].getY()+(gap/2) -25
|| y - planeSize < pipes[nextPipe%pipes.length].getY()-(gap/2) +20 ){
crash.play();
return true;
}
// increment to next pipe and increase score
nextPipe++;
score++;
// change type of pipes in hardMode every 10 points
if(score%10==0){
pipeMode = (pipeMode + 1)%3;
}
}
// else the plane is safe
return false;
}
}