-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
177 lines (141 loc) · 3.56 KB
/
script.js
File metadata and controls
177 lines (141 loc) · 3.56 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const gameBoard=document.getElementById('gameBoard');
const context =gameBoard.getContext('2d');
const scoreTxt = document.getElementById("scoreVal")
const WIDTH =gameBoard.width;
const HEIGHT =gameBoard.height;
const UNIT=25;
let foodX;
let foodY;
let xVel = 25;
let yVel = 0;
let score=0;
let active=true;
let started =false;
let snake = [
{x:UNIT*3,y:0},
{x:UNIT*2,y:0},
{x:UNIT,y:0},
{x:0,y:0},// SNAKE INITIAL POINT VALUE
];
window.addEventListener("keydown",keyPress)
startGame();
function startGame(){
context.fillStyle= '#212121';
// fillreact(xstart,ystart,widht,height)
context.fillRect(0,0,WIDTH,HEIGHT);
creatFood();
displayFood();
drawSnake();
}
function clearBoard(){
context.fillStyle= '#212121';
// fillreact(xstart,ystart,widht,height)
context.fillRect(0,0,WIDTH,HEIGHT);
}
function creatFood(){
foodX=Math.floor(Math.random()*WIDTH/UNIT)*UNIT;
foodY=Math.floor(Math.random()*HEIGHT/UNIT)*UNIT;
}
function displayFood(){
context.fillStyle='red'
context.fillRect(foodX,foodY,UNIT,UNIT);
}
function drawSnake(){
context.fillStyle = 'aqua';
context.strokeStyle = '#212121';
snake.forEach((snakePart)=>{
context.fillRect(snakePart.x,snakePart.y,UNIT,UNIT);
context.strokeRect(snakePart.x,snakePart.y,UNIT,UNIT);
})
}
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){
score +=1;
scoreTxt.textContent=score;
creatFood();
}else{
snake.pop();
}
}
function nextTick(){
if(active){
setTimeout(()=>{
clearBoard();
displayFood();
moveSnake();
drawSnake();
checkGameover();
nextTick();
},300);
}else{
clearBoard();
context.font = 'bold 50px serif';
context.fillStyle = 'red';
context.textAlign= 'center';
context.fillText("Game Over!!!",WIDTH/2,HEIGHT/2)
//started=false;
// Restart();
}
}
function keyPress(event){
if(!started){
started=true;
nextTick();
}
const LEFT= 37;
const UP =38;
const RIGHT =39;
const DOWN =40;
switch(true){
case(event.keyCode===LEFT && xVel!=UNIT):
xVel=-UNIT;
yVel=0;
break;
case(event.keyCode===RIGHT && xVel!=-UNIT):
xVel=UNIT;
yVel=0;
break;
case(event.keyCode===UP && yVel!=UNIT):
xVel=0;
yVel=-UNIT;
break;
case(event.keyCode===DOWN && yVel!=-UNIT):
xVel=0;
yVel =UNIT;
break;
}
}
function checkGameover(){
switch(true){
case(snake[0].x<0):
case(snake[0].x>=WIDTH):
case(snake[0].y<0):
case(snake[0].y>=HEIGHT):
active=false;
break;
}
}
window.addEventListener("click",Restart)
function Restart(){
if(active==false){
active=true;
started=false;
xVel = 25;
yVel = 0;
score=0;
scoreTxt.textContent=score;
active=true;
started =false;
snake = [
{x:UNIT*3,y:0},
{x:UNIT*2,y:0},
{x:UNIT,y:0},
{x:0,y:0},// SNAKE INITIAL POINT VALUE
];
startGame();
//nextTick();
}
}