-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBaby.java
47 lines (42 loc) · 1.22 KB
/
Baby.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;
/**
* Write a description of class Baby here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Baby extends Actor
{
private static final Random _rand = new Random(37L);
private int id;
private int x;
private int y;
private int xDelta;
private int yDelta;
public Baby(int id, int x, int y) {
this.id = id;
this.x = x;
this.y = y;
this.xDelta = _rand.nextDouble() < 0.5d ? -1 : 1;
this.yDelta = _rand.nextDouble() < 0.5d ? -1 : 1;
}
public int getId() { return id; }
public int getX() { return x; }
public int getY() { return y; }
/**
* Act - do whatever the Baby wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act() {
x = x + xDelta;
y = y + yDelta;
setLocation(x, y);
if (x == 0 || x == getWorld().getWidth()) {
xDelta = xDelta * -1;
}
if (y == 0 || y == getWorld().getHeight()) {
yDelta = yDelta * -1;
}
}
}