-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFood.java
More file actions
59 lines (51 loc) · 2.25 KB
/
Copy pathFood.java
File metadata and controls
59 lines (51 loc) · 2.25 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
56
57
58
59
public class Food {
private final int[] x;
private final int[] y;
public Food() {
x = new int[10];
y = new int[10];
}
public int getFoodX(int n) { return x[n]; }
public int getFoodY(int n) { return y[n]; }
public void newFood() {
do{
if (GamePanel.getRunning()) {
x[0] = (int) (Math.random() * (GamePanel.getScreenWidth() / GamePanel.getUnitSize())) * GamePanel.getUnitSize();
y[0] = (int) (Math.random() * (GamePanel.getScreenHeight() / GamePanel.getUnitSize())) * GamePanel.getUnitSize();
}
else if (GamePanel.getMenu()) {
x[0] = (int) (Math.random() * (375 / GamePanel.getUnitSize())) * GamePanel.getUnitSize() + 300;
y[0] = (int) (Math.random() * (375 / GamePanel.getUnitSize())) * GamePanel.getUnitSize() + 100;
}
} while (checkFoodSpawn());
if (GamePanel.getMods() == 'F') {
for (int i = 1; i < x.length; i++) {
do {
x[i] = (int) (Math.random() * (GamePanel.getScreenWidth() / GamePanel.getUnitSize())) * GamePanel.getUnitSize();
y[i] = (int) (Math.random() * (GamePanel.getScreenHeight() / GamePanel.getUnitSize())) * GamePanel.getUnitSize();
} while (checkFoodSpawn());
}
}
}
public void newFood(int index) {
do{
x[index] = (int) (Math.random() * (GamePanel.getScreenWidth() / GamePanel.getUnitSize())) * GamePanel.getUnitSize();
y[index] = (int) (Math.random() * (GamePanel.getScreenHeight() / GamePanel.getUnitSize())) * GamePanel.getUnitSize();
} while (checkFoodSpawn());
}
public void resetFood() {
for (int i = 0; i < x.length; i++) {
x[i] = - GamePanel.getUnitSize();
y[i] = - GamePanel.getUnitSize();
}
}
public boolean checkFoodSpawn() {
for (int i = 0; i < Snake.getBodyParts(); i++) {
if (Snake.getSnakeX(i) == x[0] && Snake.getSnakeY(i) == y[0]) return true;
}
for (int j = 1; j < x.length; j++) {
if (x[j] == x[0] && y[j] == y[0]) return true;
}
return false;
}
}