-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monkey.java
68 lines (64 loc) · 1.77 KB
/
Monkey.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The monkey is the main character. It must navigate the world and eat the bananas
* while avoiding the snakes and hawks. Getting eaten by a snake or hawk will end the
* game. Eat all 5 bananas and win the game.
*
* @author (Benjamin Presley)
* @version (2.0)
*/
public class Monkey extends Actor
{
/**
* Act - do whatever the Monkey wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
isKeyDown();
lookForBanana();
endGame();
}
/**
* Move monkey using "Up","Down","Left","Right" keys
*/
public void isKeyDown()
{
//Move up
if(Greenfoot.isKeyDown("Up"))
setLocation(getX(), getY()-2);
//Move Down
if(Greenfoot.isKeyDown("Down"))
setLocation(getX(), getY()+2);
//Move Left
if(Greenfoot.isKeyDown("Left"))
move(-2);
//Move Right
if(Greenfoot.isKeyDown("Right"))
move(2);
}
/**
* Collect the bananas when touching
*/
public void lookForBanana()
{
Actor banana = getOneIntersectingObject(Banana.class);
if(banana!=null)
{
World myWorld = getWorld();
Forest forest = (Forest)myWorld;
removeTouching(Banana.class);
Greenfoot.playSound("introbeep.wav");
Score score = forest.getScore();
score.addScore();
}
}
public void endGame()
{
if (getWorld().getObjects(Banana.class).isEmpty())
{
Greenfoot.stop();
Greenfoot.setWorld(new Winner());
}
}
}