-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLadybug.java
More file actions
69 lines (61 loc) · 1.64 KB
/
Ladybug.java
File metadata and controls
69 lines (61 loc) · 1.64 KB
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Ladybugs are enemies of the ambulance in the space.
* But there is a 20 percent chance that they are peaceful.
*
* @author Manuel Kienlein
*/
public class Ladybug extends Actor
{
private boolean aggressive;
private int speed = 3;
public Ladybug()
{
setAggressive(true);
}
public void act()
{
randomTurn();
lookForAmbulance();
}
private void randomTurn()
{
move(speed);
int percent;
percent=Greenfoot.getRandomNumber(100);
if(percent<30)
{
int winkel;
winkel=Greenfoot.getRandomNumber(31);
winkel=winkel-15;
turn(winkel);
}
if(this.isAtEdge())
{
turn(15);
}
}
private void lookForAmbulance()
{
if(!aggressive) return;
if(isTouching(Ambulance.class))
{
Ambulance[] ambulanceArray = getIntersectingObjects(Ambulance.class).toArray(new Ambulance[getIntersectingObjects(Ambulance.class).size()]);
if(ambulanceArray[0].isGod()) return;
removeTouching(Ambulance.class);
Greenfoot.playSound("au.wav");
Game.gameOver();
}
}
private boolean isAggressive(){
return aggressive;
}
public void setAggressive(boolean aggressive){
this.aggressive = aggressive;
if(!aggressive){
this.setImage(new GreenfootImage("ladybug1.png"));
}else{
this.setImage(new GreenfootImage("ladybug_02.png"));
}
}
}