-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rocket.java
79 lines (75 loc) · 2.55 KB
/
Rocket.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Rocket actor class
* Spawn when the game has started
*
* @author github.com/fiekzz
* @version 4 June 2022
*/
public class Rocket extends Actor
{
// set the gravity to 0.5
final double Gravity = 0.5;
// movement speed downwards
double dy = 0;
// screen height
private int screenHeight;
// check if it is still alive
private boolean isAlive = true;
// assets getter
GreenfootSound jumpSound = new GreenfootSound("./assets/sounds/jump2.wav");
GreenfootSound crashSound = new GreenfootSound("./assets/sounds/explosion.mp3");
GreenfootSound theme = new GreenfootSound("./assets/sounds/rocketTheme.mp3");
// get current height
private double currentHeight;
// rocket constructor
public Rocket(int screenHeight) {
this.screenHeight = screenHeight;
GreenfootImage avatar = new GreenfootImage("./assets/rocket2.png");
avatar.scale(100,100);
setImage(avatar);
}
public void act()
{
// play the song when the game has started
theme.play();
// get current height of rocket
currentHeight = getY() + dy;
// set current height location
setLocation(getX(), (int)currentHeight);
// jump if the space button has been pressed
if(Greenfoot.isKeyDown("space") == true) {
jumpSound.play();
dy = -5;
}
// velocity increase along with the gravity
dy += Gravity;
// check if the rocket is out of bounds
if(currentHeight >= getWorld().getHeight() || currentHeight < 0) {
isAlive = false;
}
// check if the rocket has collided with the distractor
getIntersect();
// end the game when game has over
if(!isAlive) {
// play crash sound
crashSound.play();
// stop game song
theme.stop();
// change the page
Greenfoot.setWorld(new EndGame());
}
}
// collider detector
public void getIntersect() {
// create distraction class to check if the rocket has been collided
// distraction will return an object if it has collided
Actor distraction = getOneObjectAtOffset(0,0,entity.class);
// check if the distraction has been occured
// if the distraction class is null == not collided yet
if(distraction != null) {
getWorld().removeObject(distraction);
isAlive = false;
}
}
}