Anonymous user
Snake/Java: Difference between revisions
m
→Code: Use enum constructor rather than EnumMap
mNo edit summary |
m (→Code: Use enum constructor rather than EnumMap) |
||
Line 10:
public class Snake extends JPanel implements Runnable {
enum Dir {
up(0, -1), right(1, 0), down(0, 1), left(-1, 0);
Dir(int x, int y) {
}▼
final int[] xy;
};
static final Random rand = new Random();
static final int WALL = -1;
static final int MAX_ENERGY = 1500;
▲ dirs.put(Dir.up, new int[]{0, -1});
▲ }
volatile boolean gameOver = true;
Line 161 ⟶ 159:
boolean hitsWall() {
Point head = snake.get(0);
int nextCol = head.x +
int nextRow = head.y +
return grid[nextRow][nextCol] == WALL;
}
Line 168 ⟶ 166:
boolean hitsSnake() {
Point head = snake.get(0);
int nextCol = head.x +
int nextRow = head.y +
for (Point p : snake)
if (p.x == nextCol && p.y == nextRow)
Line 178 ⟶ 176:
boolean eatsTreat() {
Point head = snake.get(0);
int nextCol = head.x +
int nextRow = head.y +
for (Point p : treats)
if (p.x == nextCol && p.y == nextRow) {
Line 200 ⟶ 198:
}
Point head = snake.get(0);
head.x +=
head.y +=
}
void growSnake() {
Point tail = snake.get(snake.size() - 1);
int x = tail.x +
int y = tail.y +
snake.add(new Point(x, y));
}
|