-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
166 lines (156 loc) · 3.99 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
let game_board = document.getElementById('Snake-Board');
let score = document.getElementById('score');
let context = game_board.getContext('2d');
let body = document.body;
let SCORE = 0
let WIDTH = Number(game_board.width );
let HEIGHT = Number(game_board.height );
let FUNIT = 20;
let foodX ;
let foodY ;
let Xvel = 20;
let Yvel = 0;
let snake = [
{x:FUNIT*3,y:0},
{x:FUNIT*2,y:0},
{x:FUNIT,y:0},
{x:0,y:0}
]
let speed = 200;
let started = false
let running = true
let pause = false
window.addEventListener('keydown',keypress);
startGame()
function startGame(){
clearBoard()
createFood()
displayFood()
displaySnake()
}
function clearBoard(){
context.fillStyle = "#000000"
context.fillRect(0,0,WIDTH,HEIGHT)
}
function createFood(){
foodX = Math.floor(Math.random()*WIDTH/FUNIT)*FUNIT
foodY = Math.floor(Math.random()*WIDTH/FUNIT)*FUNIT
game_board.style.animation = "gboard 1s linear 1 "
}
function displayFood(){
game_board.style.animation = ""
context.fillStyle = "red"
context.fillRect(foodX,foodY,FUNIT,FUNIT)
}
function displaySnake(){
context.fillStyle = "#fff200"
context.strokeStyle = "#000000"
snake.forEach((snakepart)=>{
context.fillRect(snakepart.x,snakepart.y,FUNIT,FUNIT)
context.strokeRect(snakepart.x,snakepart.y,FUNIT,FUNIT)
})
}
function moveSnake(){
const head = {x:snake[0].x+Xvel,y:snake[0].y+Yvel}
snake.unshift(head)
if(snake[0].x==foodX && snake[0].y==foodY){
createFood()
SCORE+=1
score.innerText = `Score : ${SCORE}`
if(speed>75){
speed -=5
}
}
else
snake.pop()
}
function nextTick(){
if(running){
setTimeout(()=>{
clearBoard()
displayFood()
displaySnake()
moveSnake()
Gameover()
// checkPause()
if(!pause)
nextTick()
},speed)
document.body.style.overflowY = "hidden";
}
else{
clearBoard()
context.font = "bold 50px serif"
context.fillStyle = "white"
context.textAlign = "center"
context.fillText("GAME OVER!!",WIDTH/2,HEIGHT/2)
body.style.animation = ""
document.body.style.overflowY = "scroll";
}
}
function keypress(event){
const LEFT = 37
const UP = 38
const RIGHT = 39
const DOWN = 40
const SPACE = 32
if(!started){
started = true
body.style.animation = "bganimation 2s linear infinite"
nextTick()
}
if(event.keyCode==SPACE){
if(pause){
pause=false
nextTick()
}
else{
pause=true;
document.body.style.overflowY = "scroll";
}
}
switch(true){
case(event.keyCode==LEFT && Xvel!=FUNIT):
if(pause){
nextTick()
pause=false
}
Xvel = -FUNIT
Yvel = 0
break;
case(event.keyCode==RIGHT && Xvel!=-FUNIT):
if(pause){
nextTick()
pause=false
}
Xvel = FUNIT
Yvel = 0
break;
case(event.keyCode==UP && Yvel!=FUNIT):
if(pause){
nextTick()
pause=false
}
Xvel = 0
Yvel = -FUNIT
break;
case(event.keyCode==DOWN && Yvel!=-FUNIT):
if(pause){
nextTick()
pause=false
}
Xvel = 0
Yvel = FUNIT
break;
}
}
function Gameover(){
if(snake[0].x<0 || snake[0].x>=WIDTH || snake[0].y<0 || snake[0].y>=HEIGHT){
running = false
}
for(let i=2;i<snake.length;i++){
if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
running =false
}
}
}