-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.java
More file actions
55 lines (42 loc) · 1.38 KB
/
Snake.java
File metadata and controls
55 lines (42 loc) · 1.38 KB
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
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import java.util.*;
public class Snake {
private Direction direction;
private Point head;
private ArrayList<Point> tail;
public Snake(int x, int y) {
this.head = new Point(x, y);
this.direction = Direction.RIGHT;
this.tail = new ArrayList<Point>();
this.tail.add(new Point(0, 0));
this.tail.add(new Point(0, 0));
this.tail.add(new Point(0, 0));
}
public void move() {
ArrayList newTail = new ArrayList<Point>();
for (int i = 0, size = tail.size(); i < size; i++) {
Point current = tail.get(i);
Point previous = i == 0 ? head : tail.get(i - 1);
newTail.add(new Point(previous.getX(), previous.getY()));
}
this.tail = newTail;
this.head.move(this.direction, 10);
}
public void addTail() {
Point last = this.tail.get(this.tail.size() - 1);
this.tail.add(new Point(-10, -10));
}
public void turn(Direction d) {
if (d.isX() && direction.isY() || d.isY() && direction.isX()) {
direction = d;
}
}
public ArrayList<Point> getTail() {
return this.tail;
}
public Point getHead() {
return this.head;
}
}