/
Balloon.java
50 lines (43 loc) · 1.18 KB
/
Balloon.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Balloon here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Balloon extends Actor
{
private int speed;
public Balloon()
{
// Randomly pick a speed for the balloon to float to the top.
speed = Greenfoot.getRandomNumber(5) + 1;
}
public void act()
{
// Move UP the screen using the speed value.
setLocation(getX(), getY() - speed);
// See if we hit the top (this is bad). Pop the balloon
if (getY() < getImage().getHeight() / 2)
{
popBad();
}
}
public void popBad()
{
BalloonWorld world = (BalloonWorld)getWorld();
world.loseLife();
pop("uhoh.wav");
}
public void popGood()
{
BalloonWorld world = (BalloonWorld)getWorld();
world.scorePoint();
pop("pop.wav");
}
public void pop(String soundName)
{
Greenfoot.playSound(soundName);
getWorld().removeObject(this);
}
}