-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFood.java
More file actions
73 lines (54 loc) · 1.23 KB
/
Food.java
File metadata and controls
73 lines (54 loc) · 1.23 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Vector;
import java.applet.*;
import java.awt.*;
import java.util.*; // for Random
public class Food {
// Instance vars
Coord pos;
boolean exists;
int numEaten;
int nTiles;
Random rand;
// Constructor
public Food(int tiles) {
nTiles = tiles;
rand = new Random();
pos = new Coord(0, 0);
generateFood();
}
// public void reset() {
// numEaten = 0;
// generateFood();
// }
// position accessor
public Coord position() {
return pos;
}
// Logic
// generate new food if eaten
public void checkExists() {
if(!exists)
generateFood();
}
// generate new food
void generateFood() {
int x = rand.nextInt(nTiles);
int y = rand.nextInt(nTiles);
pos.x = x;
pos.y = y;
exists = true;
}
// Consume food
public void eat() {
exists = false;
numEaten++;
}
// Drawing
public void drawFood(Graphics g, Main m) {
int tile = m.window.size.width / m.TILES;
int x = pos.x * tile;
int y = pos.y * tile;
g.setColor(Color.red);
g.fillRect(x, y, tile, tile);
}
}