-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
106 lines (88 loc) · 2.45 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
const canvas_res = [800, 800];
const res = [64, 64];
const pixel_size = [canvas_res[0] / res[0], canvas_res[1] / res[1]];
const snake = new DoubleLinked.List();
// Create the snake's head
snake.push([Math.floor(res[0] / 3), Math.floor(res[1] / 2)]);
snake.push([snake.head.data[0] - 1, snake.head.data[1]]);
const apple = [Math.floor(res[0] * 2 / 3), Math.floor(res[1] / 2)];
let direction = [0, 0];
function draw_pixel(coords) {
rect(coords[0] * pixel_size[0], coords[1] * pixel_size[1], ...pixel_size);
}
function lose() {
alert("You Lost");
noLoop();
}
function setup() {
createCanvas(...canvas_res);
noSmooth();
noStroke();
}
const tick_end = 1e3 / 20;
let tick_clock = 0;
function draw() {
// Calculate every tick
tick_clock += deltaTime;
if (tick_clock >= tick_end && (direction[0] != 0 || direction[1] != 0)) {
tick_clock = 0;
// Calculate movement
const new_head = snake.tail;
snake.tail = snake.tail.before;
snake.tail.next = null;
new_head.before = null;
new_head.next = snake.head;
snake.head.before = new_head;
const old_tail = new_head.data;
new_head.data = [snake.head.data[0] + direction[0], snake.head.data[1] + direction[1]];
snake.head = new_head;
// Calculate out of bounds
for (const i in snake.head.data)
if (snake.head.data[i] < 0 || snake.head.data[i] >= res[i])
lose();
// Calculate self collision
if (get((snake.head.data[0] + 0.5) * pixel_size[0], (snake.head.data[1] + 0.5) * pixel_size[1])[1] == 255)
lose();
// Calculate consumption
if (snake.head.data[0] == apple[0] && snake.head.data[1] == apple[1]) {
snake.push(old_tail);
for (let i in apple)
apple[i] = Math.floor(Math.random() * res[i]);
}
}
background(0);
// Draw the snake
push();
{
fill(0, 255, 0);
for (let node = snake.head; node != null; node = node.next)
draw_pixel(node.data);
}
pop();
// Draw the apple
push();
{
fill(255, 0, 0);
draw_pixel(apple);
}
pop();
}
function keyPressed() {
const old_direction = direction;
switch(key) {
case "ArrowUp":
direction = [0, -1];
break;
case "ArrowDown":
direction = [0, 1];
break;
case "ArrowLeft":
direction = [-1, 0];
break;
case "ArrowRight":
direction = [1, 0];
break;
}
if (direction[0] == -old_direction[0] && direction[1] == -old_direction[1])
direction = old_direction;
}