-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_level5.js
113 lines (90 loc) · 3.51 KB
/
script_level5.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
const playBoard = document.querySelector(".play_board");
const scorePoint = document.querySelector(".score");
const highScorePoint = document.querySelector(".high_score");
let i=0,j=0;
let gameOver = false;
let foodX, foodY;
let caterpillarX=5, caterpillarY=10;
let caterpillarBody = [];
let velocityX=0, velocityY=0;
let setIntervalID;
let score = 0;
let highScore = localStorage.getItem("high_score") || 0;
highScorePoint.innerText = `High Score : ${highScore}`;
var excludedValuesFoodY = [14,15,16];
// var excludedValuesFoodX = [...Array(14).keys()].map(i => i + 9);
const changeFoodPosition = () => {
do{
foodX = Math.floor(Math.random() * 30) + 1;
foodY = Math.floor(Math.random() * 30) + 1;
}while( (foodX==1) || (foodX==30) || ( (foodX==15) && (!(excludedValuesFoodY.includes(foodY))) ) || (foodY==1) || (foodY==30) )
}
const handleGameOver = () => {
clearInterval(setIntervalID);
alert("Game Over! Press OK to replay ...");
location.reload();
}
const boarder = () => {
let htmlboarder = '';
for (i=1;i<31;i++) {
for(j=1;j<31;j++){
if ( (j==1) || (j==30) ){
htmlboarder += `<div class="boarder horizontal" style="grid-area:${j}/${i}"></div>`;
}else if ( (i==1) || (i==30) || ( (i==15) && (!(excludedValuesFoodY.includes(j))) ) ){
htmlboarder += `<div class="boarder vertical" style="grid-area:${j}/${i}"></div>`;
}
}
}
return htmlboarder;
}
const changeDirection = (e) => {
if (e.key === "ArrowUp" && velocityY!=1 ){
velocityX = 0;
velocityY = -1;
}else if (e.key === "ArrowDown" && velocityY!=-1){
velocityX = 0;
velocityY = 1;
}else if (e.key === "ArrowRight" && velocityX!=-1){
velocityX = 1;
velocityY = 0;
}else if (e.key === "ArrowLeft" && velocityX!=1){
velocityX = -1;
velocityY = 0;
}
initGame();
}
const initGame = () => {
if (gameOver) return handleGameOver();
let htmlMarkupboarder = boarder();
let htmlMarkup = `<div class="food" style="grid-area:${foodY}/${foodX}"></div>`;
if(caterpillarX===foodX && caterpillarY===foodY) {
changeFoodPosition();
caterpillarBody.push([foodX,foodY]);
console.log(caterpillarBody);
score++;
highScore = score >= highScore ? score : highScore;
localStorage.setItem("high_score", highScore);
scorePoint.innerText = ` : ${score} `;
highScorePoint.innerText = `High Score : ${highScore}`;
}
for (let i=caterpillarBody.length - 1; i>0 ; i--) {
caterpillarBody[i] = caterpillarBody[i-1];
}
caterpillarBody[0] = [caterpillarX, caterpillarY];
caterpillarX += velocityX;
caterpillarY += velocityY;
if( (caterpillarX==1) || (caterpillarX==30) || ( (caterpillarX==15) && (!(excludedValuesFoodY.includes(caterpillarY))) ) || (caterpillarY==1) || (caterpillarY==30) ) {
gameOver = true;
}
for (let i = 0; i < caterpillarBody.length; i++) {
let eyebody = i === 0 ? "eye" : "body";
htmlMarkup += `<div class="${eyebody}" style="grid-area:${caterpillarBody[i][1]}/${caterpillarBody[i][0]}"></div>`;
if (i !== 0 && caterpillarBody[0][1] === caterpillarBody[i][1] && caterpillarBody[0][0] === caterpillarBody[i][0]) {
gameOver = true;
}
}
playBoard.innerHTML = htmlMarkup + htmlMarkupboarder;
}
changeFoodPosition();
setIntervalID = setInterval(initGame, 125);
document.addEventListener("keydown", changeDirection);