-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakeConfg.pde
85 lines (68 loc) · 1.83 KB
/
snakeConfg.pde
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
74
75
76
77
78
79
80
81
82
83
84
85
class Snake {
PVector snakeLoc = new PVector((floor(random(b.boardWidth))), (floor(random(b.boardHeight))));
float xDir = 1;
float yDir = 0;
int total = 0;
ArrayList<PVector> body = new ArrayList<PVector>();
void render() {
fill(theme.snake); //HEX# 47FFE8
stroke(theme.snakeStroke);
rect(snakeLoc.x*scl, snakeLoc.y*scl, scl, scl);
for (int i = 0; i < body.size(); i++) {
rect(body.get(i).x*scl, body.get(i).y*scl, scl, scl);
}
}
boolean eat() {
float d = dist(snakeLoc.x, snakeLoc.y, f.food.x, f.food.y);
if (d < 1) {
total++;
return true;
} else {
return false;
}
}
void death() {
for (int i = 0; i < body.size(); i++) {
PVector pos = body.get(i);
float d = dist(snakeLoc.x, snakeLoc.y, pos.x, pos.y);
if (d < 1) {
gameOver();
}
}
if (level > 0) {
float p;
for (int i = 0; i < f.poison.size(); i++) {
p = dist(snakeLoc.x, snakeLoc.y, f.poison.get(i).x, f.poison.get(i).y);
if (p < 1) {
gameOver();
}
}
}
}
void gameOver() {
fill(255, 0, 0);
textSize(30);
textAlign(CENTER);
text("GAME OVER", b.boardWidth/2*scl, b.boardHeight/2*scl);
textAlign(CENTER, TOP);
text("PRESS 'R' TO RESTART", b.boardWidth/2*scl, b.boardHeight/2*scl);
body.clear();
noLoop();
if (score > highScore) {
highScore = score;
}
level = 0;
}
void update() {
if (total > 0) {
if (total == body.size()) {
body.remove(0);
}
body.add(new PVector(snakeLoc.x, snakeLoc.y));
}
snakeLoc.x += (snakeSpeed * xDir);
snakeLoc.y += (snakeSpeed * yDir);
snakeLoc.x = constrain(snakeLoc.x, 0, b.boardWidth-1);
snakeLoc.y = constrain(snakeLoc.y, 0, b.boardHeight-1);
}
}