-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
178 lines (147 loc) · 3.42 KB
/
sketch.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
let x = 0;
let boop;
const CANVAS_WIDTH = 300;
const CANVAS_HEIGHT = 300;
const DIRECTION_UP = "ArrowUp";
const DIRECTION_DOWN = "ArrowDown";
const DIRECTION_RIGHT = "ArrowRight";
const DIRECTION_LEFT = "ArrowLeft";
let frameRatePerSec = 12;
let environment;
let food;
class Element {
constructor(x, y) {
this.x = x;
this.y = y;
}
distance(other) {
return Math.sqrt(
Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2)
);
}
}
class Food extends Element {
constructor(x, y) {
super(x, y);
}
draw() {
stroke(255);
fill(0);
rect(this.x, this.y, 10, 10);
}
}
class SnakeBodyPart extends Element {
constructor(x, y) {
super(x, y);
this.size = 10;
}
draw() {
stroke(0);
fill(255);
rect(this.x, this.y, this.size, this.size);
}
isOutOfBounds() {
return (
this.x >= CANVAS_WIDTH ||
this.x <= 0 ||
this.y <= 0 ||
this.y >= CANVAS_HEIGHT
);
}
}
class Snake {
constructor() {
this.body = [new SnakeBodyPart(40, 40)];
this.direction = DIRECTION_RIGHT;
}
getHead() {
return this.body[0];
}
getTail() {
return this.body[this.body.length - 1];
}
nextHead() {
const head = this.getHead();
return {
[DIRECTION_UP]: new SnakeBodyPart(head.x, head.y - head.size),
[DIRECTION_DOWN]: new SnakeBodyPart(head.x, head.y + head.size),
[DIRECTION_RIGHT]: new SnakeBodyPart(head.x + head.size, head.y),
[DIRECTION_LEFT]: new SnakeBodyPart(head.x - head.size, head.y)
}[this.direction];
}
advance() {
const nextHead = this.nextHead();
this.body.pop();
this.body.unshift(nextHead);
}
draw() {
for (let part of this.body) {
part.draw();
}
}
onDirectionChanged() {
let _this = this;
// Handle only arrow presses
if (!isArrowKey() || isReverse()) return;
this.direction = key;
function isReverse() {
return (
(_this.direction == DIRECTION_UP && key == DIRECTION_DOWN) ||
(_this.direction == DIRECTION_DOWN && key == DIRECTION_UP) ||
(_this.direction == DIRECTION_RIGHT && key == DIRECTION_LEFT) ||
(_this.direction == DIRECTION_LEFT && key == DIRECTION_RIGHT)
);
}
function isArrowKey() {
return [
DIRECTION_UP,
DIRECTION_DOWN,
DIRECTION_RIGHT,
DIRECTION_LEFT
].includes(key);
}
}
isTouchingFood() {
let distance = this.getHead().distance(food);
return distance < 10;
}
isOutOfBounds() {
return this.getHead().isOutOfBounds();
}
eat() {
const tail = this.getTail();
const newTail = new SnakeBodyPart(tail.x, tail.y);
this.body.push(newTail);
frameRatePerSec += 2;
frameRate(frameRatePerSec);
food = null;
}
}
let snake = new Snake();
function setup() {
const canvas = createCanvas(CANVAS_WIDTH, CANVAS_HEIGHT);
canvas.parent("container1");
frameRate(frameRatePerSec);
}
function draw() {
background(204);
// Update
snake.advance();
if (!food) generateFood();
// Draw
snake.draw();
food.draw();
if (snake.isTouchingFood()) snake.eat();
if (keyIsDown) snake.onDirectionChanged();
if (snake.isOutOfBounds()) this.restart();
}
function restart() {
snake.body = [new SnakeBodyPart(40, 40)];
snake.direction = DIRECTION_RIGHT;
food = null;
}
function generateFood() {
const x = random(5, CANVAS_WIDTH - 5);
const y = random(5, CANVAS_HEIGHT - 5);
food = new Food(x, y);
}