-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.java
71 lines (70 loc) · 1.82 KB
/
Tree.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
import greenfoot.*;
import java.lang.*;
/**
* Tree: Randomizes fruits on trees and drops fruit.
*
* Kat Nguyen
* 5/29/2020
*/
public class Tree extends Actor
{
private Fruit fruits[] = new Fruit[3];
private boolean fruitsDropped = false;
Fruit fruit = null;
public Tree()
{
GreenfootImage trunk = getImage();
trunk.scale(52, 125);
setImage(trunk);
}
public void addFruits()
{
int randomNum = (int)(Math.random() * 5);
for (int i = 0; i < fruits.length; i++)
{
if (randomNum == 0)
fruit = new Apple();
else if (randomNum == 1)
fruit = new Orange();
else if (randomNum == 2)
fruit = new Cherry();
else if (randomNum == 3)
fruit = new Pear();
else if (randomNum == 4)
fruit = new Peach();
fruits[i] = fruit;
}
getWorld().addObject(fruits[0], getX()+5, getY()-150);
getWorld().addObject(fruits[1], getX()-54, getY()-95);
getWorld().addObject(fruits[2], getX()+60, getY()-95);
}
public void addedToWorld(World world)
{
Treetop treetop = new Treetop();
world.addObject(treetop, this.getX(), this.getY()-55);
addFruits();
}
public void dropFruits()
{
if (fruitsDropped)
{
return;
}
int bottom = getY() + 90;
for (int i = 0; i < fruits.length; i++)
{
if (fruits[i].isOnTree())
{
fruits[i].dropFruit(bottom);
}
}
fruitsDropped = true;
}
public void deleteFruit(Fruit fruit)
{
getWorld().removeObject(fruit);
}
public void act()
{
}
}