forked from 2giosangmitom/fylo-landing-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
151 lines (133 loc) · 3.57 KB
/
snake.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
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
window.onload = () => {
setInterval(show, 1000 / 10);
};
let cw = window.innerWidth;
let ch = window.innerHeight;
canvas.width = cw;
canvas.height = ch;
function show() {
update();
draw();
}
// rome-ignore format: easier to read
window.addEventListener("resize", () => {
cw = window.innerWidth;
ch = window.innerHeight;
canvas.width = cw;
canvas.height = ch;
},
true,
);
function hitWall() {
const snakeHead = snake.tail[snake.tail.length - 1];
// rome-ignore format: easier to read
if (snakeHead.x < 0 || snakeHead.x > canvas.width || snakeHead.y < 0 || snakeHead.y > canvas.height) {
location.reload();
}
}
function update() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
snake.move();
eatFood();
hitWall();
}
function eatFood() {
// rome-ignore format: easier to read
if (snake.tail[snake.tail.length - 1].x === food.x && snake.tail[snake.tail.length - 1].y === food.y) {
snake.tail[snake.tail.length] = { x: food.x, y: food.y };
food = new Food();
}
}
function draw() {
createRect(0, 0, canvas.width, canvas.height, "black");
createRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < snake.tail.length; ++i) {
// rome-ignore format: easier to read
createRect(snake.tail[i].x + 2.5, snake.tail[i].y + 2.5, snake.size - 5, snake.size - 5, "red");
}
createRect(food.x, food.y, food.size, food.size, food.color);
}
function createRect(x, y, width, height, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
}
window.addEventListener("keydown", (event) => {
setTimeout(() => {
if (event.key === "ArrowLeft" && snake.rotateX !== 1) {
snake.rotateX = -1;
snake.rotateY = 0;
} else if (event.key === "ArrowRight" && snake.rotateX !== -1) {
snake.rotateX = 1;
snake.rotateY = 0;
} else if (event.key === "ArrowUp" && snake.rotateY !== 1) {
snake.rotateX = 0;
snake.rotateY = -1;
} else if (event.key === "ArrowDown" && snake.rotateY !== -1) {
snake.rotateX = 0;
snake.rotateY = 1;
}
}, 0);
});
class Snake {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
this.tail = [{ x: this.x, y: this.y }];
this.rotateX = 0;
this.rotateY = 1;
}
move() {
let newRect;
if (this.rotateX === 1) {
newRect = {
x: this.tail[this.tail.length - 1].x + this.size,
y: this.tail[this.tail.length - 1].y,
};
} else if (this.rotateX === -1) {
newRect = {
x: this.tail[this.tail.length - 1].x - this.size,
y: this.tail[this.tail.length - 1].y,
};
} else if (this.rotateY === 1) {
newRect = {
x: this.tail[this.tail.length - 1].x,
y: this.tail[this.tail.length - 1].y + this.size,
};
} else if (this.rotateY === -1) {
newRect = {
x: this.tail[this.tail.length - 1].x,
y: this.tail[this.tail.length - 1].y - this.size,
};
}
this.tail.shift();
this.tail.push(newRect);
}
}
class Food {
constructor() {
let isTouching;
while (true) {
isTouching = false;
// rome-ignore format: easier to read
this.x = Math.floor((Math.random() * (canvas.width - 20)) / snake.size) * snake.size;
// rome-ignore format: easier to read
this.y = Math.floor((Math.random() * (canvas.height - 20)) / snake.size) * snake.size;
for (let i = 0; i < snake.tail.length; ++i) {
if (this.x === snake.tail[i].x && this.y === snake.tail[i].y) {
isTouching = true;
}
}
this.color = "pink";
this.size = snake.size;
if (!isTouching) {
break;
}
}
}
}
// rome-ignore format: easier to read
const snake = new Snake(100,20,20);
let food = new Food();