-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakePart.java
52 lines (44 loc) · 1.33 KB
/
SnakePart.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
// multiple key press
// http://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time
public class SnakePart extends GraphicsItem {
private Direction direction;
// double prevX, prevY;
// or implement a moveTo with Point2D
// Point2D prevPosition
public SnakePart(String partImagePath, int x, int y) {
super(partImagePath, x, y);
direction = Direction.UP;
}
public SnakePart(String partImagePath) {
this(partImagePath, 0, 0);
}
public SnakePart() {
this("art/snake_part.png");
}
public void setDirection(Direction dir) {
direction = dir;
}
public Direction getDirection() {
return direction;
}
public void step() {
//step(5,5);
step(getWidth(), getHeight());
}
public void step(int horizontalStep, int verticalStep) {
switch(direction) {
case UP:
moveTo(getX(), getY() - verticalStep);
break;
case DOWN:
moveTo(getX(), getY() + verticalStep);
break;
case LEFT:
moveTo(getX() - horizontalStep, getY());
break;
case RIGHT:
moveTo(getX() + horizontalStep, getY());
break;
}
}
}