-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
152 lines (126 loc) · 4.03 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
document.addEventListener("DOMContentLoaded",function(){
const gameArena=document.getElementById('game-arena');
const arenasize=600;
const cellsize=20;
let score=0;
let gamestarted=false;
let food={x:300,y:200};
let snake=[{x:160,y:200},{x:140,y:200},{x:120,y:200}];
let dx=cellsize;
let dy=0;
let speed=200;
function ChangeDirection(event){
const isGoingDown= dy===cellsize;
const isGoingUp= dy===-cellsize;
const isGoingLeft= dx===-cellsize;
const isGoingRight= dx===cellsize;
if(event.key==='ArrowUp' && !isGoingDown){
dx=0;
dy=-cellsize;
}
else if(event.key==='ArrowDown' && !isGoingUp){
dx=0;
dy=cellsize;
}
else if(event.key==='ArrowLeft' && !isGoingRight){
dx=-cellsize;
dy=0;
}
else if(event.key==='ArrowRight' && !isGoingLeft){
dx=cellsize;
dy=0;
}
}
function foodposupdate(){
food.x=Math.floor(Math.random()*30)*20;
food.y=Math.floor(Math.random()*30)*20;
console.log(food.x,food.y);
}
function UpdateSnake(){
const newHead={x:snake[0].x+dx,y:snake[0].y+dy};
snake.unshift(newHead);
if(newHead.x>=600 || newHead.y>=600){
//gameover();
//loopbreak();
}
if(newHead.x==food.x && newHead.y==food.y){
score+=10;
foodposupdate();
if(speed>50){
clearInterval(intervalid);
speed-=10;
gameloop();
}
}
else{
snake.pop();
}
}
function drawdiv(x,y,className){
const divelement=document.createElement("div");
divelement.classList.add(className);
divelement.style.top=`${y}px`;
divelement.style.left=`${x}px`;
return divelement;
}
function DrawSnakeandFood(){
gameArena.innerHTML='';
snake.forEach((snakecell)=>{
const snakeELement=drawdiv(snakecell.x,snakecell.y,"snake");
gameArena.appendChild(snakeELement);
})
const fooditem=drawdiv(food.x,food.y,"food");
gameArena.appendChild(fooditem);
}
function isGameOver(){
for(let i = 1; i < snake.length; i++) {
if(snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
return true;
}
}
// wall collision checks
const hitLeftWall = snake[0].x < 0; // snake[0] -> head
const hitRightWall = snake[0].x > arenasize - cellsize;
const hitTopWall = snake[0].y < 0;
const hitBottomWall = snake[0].y > arenasize - cellsize;
return hitLeftWall || hitRightWall || hitTopWall || hitBottomWall;
}
function gameloop(){
intervalid=setInterval(()=>{
if(isGameOver()){
clearInterval(intervalid);
gamestarted=false;
alert(`GAME OVER SCORE IS ${score}`);
return;
}
UpdateSnake();
drawScoreBoard();
DrawSnakeandFood();
},speed);
}
function drawScoreBoard(){
const scoreBoard=document.getElementById('score-board');
scoreBoard.textContent=`SCORE : ${score}`;
}
function runGame(){
if(!gamestarted){
gamestarted=true;
document.addEventListener("keydown",ChangeDirection);
gameloop();
}
}
function initialGame(){
let scoreboard=document.createElement("div");
scoreboard.id='score-board';
document.body.insertBefore(scoreboard,gameArena);
let starButton=document.createElement("button");
starButton.textContent="START";
starButton.classList.add("startbtn");
starButton.addEventListener("click",function start(){
starButton.style.display='none';
runGame();
});
document.body.appendChild(starButton);
}
initialGame();
})