-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
171 lines (134 loc) · 4.63 KB
/
script.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
const snakeboard = document.getElementById("gameBoard");
const stats = document.getElementById("stats");
const ctx = snakeboard.getContext("2d");
const manage = {
board: {
border: 'black',
background: "#dbe6fd",
},
food: {
x: 0,
y: 0,
},
dx: -10,
dy: 0,
changing_direction: false,
};
class Snake {
constructor(snake, score) {
this.snake = snake;
this.score = score;
}
get getSnake() {
return this.snake;
}
drawSnake() {
this.snake.forEach(function drawSnakePart(snakePart) {
ctx.fillStyle = '#346751';
ctx.fillRect(snakePart.x, snakePart.y, 12, 12);
});
}
drawFood() {
ctx.fillStyle = '#c84b31';
ctx.fillRect(manage.food.x, manage.food.y, 12, 12);
}
getFood(min , max) {
return Math.round((Math.random() * (max-min) + min) / 10) * 10;
}
move_snake() {
const head = {x: this.snake[0].x + manage.dx, y: this.snake[0].y + manage.dy,};
this.snake.unshift(head);
if(this.snake[0].x === manage.food.x && this.snake[0].y === manage.food.y) {
let highscore = localStorage.getItem("highscore");
this.score += 10;
if(highscore !== null) {
if(this.score > highscore) {
localStorage.setItem("highscore", this.score);
}
} else {
localStorage.setItem("highscore", this.score);
}
document.getElementById("score").innerHTML = "Score: " + this.score + " HighScore: " + localStorage.getItem("highscore");
generate_food();
} else {
this.snake.pop();
}
}
}
const snake = new Snake([{x: 200, y: 200}, {x: 190, y: 200}, {x: 180 , y: 200},], 0);
generate_food();
document.addEventListener("keydown", move_controller);
function StartGame() {
document.getElementById("press").remove();
document.getElementById("score").innerHTML = "Score: " + 0;
Main();
}
function Main() {
if(statusGame())
return $(stats).append(`<button id = "restart" onclick = "location.reload();">Try Again</button>`);
manage.changing_direction = false;
setTimeout(function onTick() {
clear_board();
snake.drawFood();
snake.move_snake();
snake.drawSnake();
Main();
}, 150)
}
function clear_board() {
ctx.fillStyle = manage.board.background;
ctx.strokestyle = manage.board.border;
ctx.fillRect(0, 0, snakeboard.width, snakeboard.height);
ctx.strokeRect(0, 0, snakeboard.width, snakeboard.height);
}
function move_controller(event) {
if(manage.changing_direction) return;
manage.changing_direction = true;
const LEFT_KEY = 37;
const RIGHT_KEY = 39;
const UP_KEY = 38;
const DOWN_KEY = 40;
const keyPressed = event.keyCode;
const goingUp = manage.dy === -10;
const goingDown = manage.dy === 10;
const goingRight = manage.dx === 10;
const goingLeft = manage.dx === -10;
if (keyPressed === LEFT_KEY && !goingRight){
manage.dx = -10;
manage.dy = 0;
}
if (keyPressed === UP_KEY && !goingDown) {
manage.dx = 0;
manage.dy = -10;
}
if (keyPressed === RIGHT_KEY && !goingLeft) {
manage.dx = 10;
manage.dy = 0;
}
if (keyPressed === DOWN_KEY && !goingUp) {
manage.dx = 0;
manage.dy = 10;
}
}
function statusGame() {
let snake1 = snake.getSnake;
for(let i = 3; i < snake1.length; ++i) {
const has_collided = snake1[i].x === snake1[0].x && snake1[i].y === snake1[0].y;
if(has_collided)
return true;
}
const hitLeftWall = snake1[0].x < 0;
const hitRightWall = snake1[0].x > snakeboard.width - 10;
const hitTopWall = snake1[0].y < 0;
const hitBottomWall = snake1[0].y > snakeboard.height - 10;
return hitLeftWall || hitRightWall || hitTopWall || hitBottomWall;
}
function generate_food() {
let snake1 = snake.getSnake;
manage.food.x = snake.getFood(0, snakeboard.width - 20);
manage.food.y = snake.getFood(0, snakeboard.height - 20);
snake1.forEach(function has_snake_eaten_food(part) {
const has_eaten = part.x == manage.food.x && part.y == manage.food.y;
if(has_eaten) generate_food();
});
}