-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (64 loc) · 1.19 KB
/
index.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
const width = innerWidth - 50;
const height = innerHeight - 50;
let x = 350;
let y = 350;
let foodY = y - 40;
let foodX = x - 40;
let h = 20;
let w = 20;
let lastX = 350 - w;
let lastY = 350;
let tail = [];
let marginOfError = h/2;
function setup () {
createCanvas(width, height);
let xc = constrain(x, 0, width);
let yc = constrain(y, 0, height);
tail.push({
x: lastX,
y: lastY
});
}
function addToTail () {
tail.push({
x: lastX - w,
y: lastY - h
})
console.log(tail)
}
function draw () {
clear();
background(21);
fill(150);
frameRate(10)
let xc = constrain(x, 0, width - w);
let yc = constrain(y, 0, height - h);
rect(xc, yc, h, w);
for (let i = 0; i < tail.length; i++) {
rect(tail[i].x, tail[i].y, h, w);
tail[i].y = tail[i].y +
tail[i].x = tail[i].x
}
if(keyIsPressed) {
switch (keyCode) {
case LEFT_ARROW:
x = x - w;
lastX = x;
break;
case RIGHT_ARROW:
x = x + w;
lastX = x;
break;
case UP_ARROW:
y = y - h;
lastY = y;
break;
case DOWN_ARROW:
y = y + h;
lastY = y;
break;
default:
break;
}
}
}