-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
149 lines (123 loc) · 3.63 KB
/
script.js
File metadata and controls
149 lines (123 loc) · 3.63 KB
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
const canvas = document.querySelector("canvas");
const context = canvas.getContext("2d");
const DOWN = "down";
const UP = "up";
const LEFT = "left";
const RIGHT = "right";
const SIZE = 10;
function Snake() {
this.body = [{ x: 0, y: 2 * SIZE },
{ x: 0, y: 1 * SIZE },
{ x: 0, y: 0 * SIZE }
];
this.color = "green";
this.direction = DOWN;
this.xDelta = 0;
this.yDelta = SIZE;
this.drawSnake = function(color) {
this.body.forEach((element) => {
drawObject(element, this.color);
});
}
}
function Apple() {
this.x = random(0, 190);
this.y = random(0, 190);
this.width = SIZE;
this.height = SIZE;
this.color = "red";
}
function SnakeGame() {
this.snake = new Snake();
this.apple = new Apple();
this.reset = false;
this.drawBoard = function() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawObject(this.apple);
this.snake.drawSnake();
}
this.changeDirection = function(direction){
game.snake.direction = direction;
switch (this.snake.direction) {
case UP:
this.snake.xDelta = 0;
this.snake.yDelta = -SIZE;
break;
case DOWN:
this.snake.xDelta = 0;
this.snake.yDelta = SIZE;
break;
case RIGHT:
this.snake.xDelta = SIZE;
this.snake.yDelta = 0;
break;
case LEFT:
this.snake.xDelta = -SIZE;
this.snake.yDelta = 0;
break;
}
}
this.moveSnake = function() {
const head = {
x: this.snake.body[0].x + this.snake.xDelta,
y: this.snake.body[0].y + this.snake.yDelta,
};
this.snake.body.unshift(head);
if (this.interactFood()) {
this.apple = new Apple();
} else {
this.snake.body.pop();
}
}
this.isGameOver = function() {
if (this.snake.body[0].x < 0 || this.snake.body[0].x > canvas.width || this.snake.body[0].y < 0 || this.snake.body[0].y > canvas.height) {
this.reset = true;
alert("game over");
}
}
this.resetGame = function() {
this.snake = new Snake();
this.apple = new Apple();
this.reset = false;
}
this.interactFood = function() {
return (this.snake.body[0].x === this.apple.x &&
this.snake.body[0].y === this.apple.y)
}
}
let game = new SnakeGame();
let updated = 0;
function drawObject(object, color) {
context.fillStyle = object.color ? object.color : color;
context.fillRect(object.x, object.y, SIZE, SIZE);
}
function loop(now) {
requestAnimationFrame(loop);
if (!game.reset) {
if (now - updated > 400) {
game.drawBoard();
game.moveSnake();
updated = now;
}
game.isGameOver();
} else {
game.resetGame();
}
}
loop();
document.addEventListener('keydown', function(event) {
if (event.code === "ArrowUp" && game.snake.direction != DOWN) {
game.changeDirection(UP);
} else if (event.code === "ArrowDown" && game.snake.direction != UP) {
game.changeDirection(DOWN);
} else if (event.code === "ArrowLeft" && game.snake.direction != RIGHT) {
game.changeDirection(LEFT);
} else if (event.code === "ArrowRight" && game.snake.direction != LEFT) {
game.changeDirection(RIGHT);
}
});
function random(min, max) {
let rand = Math.floor(Math.random() * (max - min)) + min;
rand += (SIZE - rand % SIZE);
return rand;
}