-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bus.java
63 lines (62 loc) · 1.72 KB
/
Bus.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bus here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bus extends Actor
{
/**
* Act - do whatever the Bus wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
GreenfootSound bombSound = new GreenfootSound("bomb.mp3");
public void act()
{
if(!isOnBlock() && !isDead())
{
drive();
hit();
}
else if(isDead())
{
setImage("explosion.png");
bombSound.setVolume(40);
bombSound.play();
}
}
public void drive()
{
if(Greenfoot.isKeyDown("left"))
move(-2);
if(Greenfoot.isKeyDown("right"))
move(2);
}
public void hit()
{
tree myTree = (tree) getOneObjectAtOffset(0,0,tree.class);
GreenfootSound hitTree = new GreenfootSound("hit_tree.mp3");
if(myTree != null && myTree.image == "tree.png")
{
hitTree.setVolume(30);
hitTree.play();
myTree.setImage("blank.png");
World world = getWorld();
HealthBar bar = world.getObjects(HealthBar.class).get(0);
bar.health -= 5;
}
}
public boolean isOnBlock()
{
World world = getWorld();
Block block = (Block) getOneObjectAtOffset(0, 70, Block.class);
return ((block != null) && block.display);
}
public boolean isDead()
{
World world = getWorld();
HealthBar bar = world.getObjects(HealthBar.class).get(0);
return (bar.health < 1);
}
}