forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMole.java
More file actions
43 lines (36 loc) · 879 Bytes
/
Mole.java
File metadata and controls
43 lines (36 loc) · 879 Bytes
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
import java.awt.Color;
/**
* A polygon that represents a small burrowing mammal.
*/
public class Mole extends BlinkingPolygon {
public static final Color BROWN = new Color(153, 102, 0);
private boolean alive;
/**
* Creates a mole at the given location.
*
* @param x the X coordinate
* @param y the Y coordinate
*/
public Mole(int x, int y) {
super(10, 30, BROWN);
translate(x, y);
alive = true;
}
@Override
public void step() {
// blink on/off at random times
if (alive && Math.random() < 0.5) {
super.step();
}
}
/**
* Updates the state of the mole when clicked.
*/
public void whack() {
// ignore whack when invisible
if (visible) {
color = Color.LIGHT_GRAY;
alive = false;
}
}
}