-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
165 lines (154 loc) · 4.17 KB
/
script.js
File metadata and controls
165 lines (154 loc) · 4.17 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
var wormBody, food, direction, allowedToMove, isPlaying;
var map;
var speed = 2;
var play;
function initializeVariables() {
wormBody = [[0, 0]];
food = 0;
direction = 'Right';
allowedToMove = false;
isPlaying = false;
}
// Creating the map and initializing variables
function initializeGameState(mapElementId) {
map = document.getElementById(mapElementId);
initializeVariables();
// Generating the map pixels
for (var i = 0; i < 100; i++) {
var pixel = document.createElement('div');
pixel.setAttribute('class', 'pixel');
map.appendChild(pixel);
}
// Generating the worm body and food
map.children[0].classList.add('worm-body');
generateFood();
}
function generateFood() {
// To prevent generating food over the worm
while (map.children[food].classList.contains('worm-body')) {
food = Math.floor(Math.random() * 100);
}
// Placing food on the map
map.children[food].classList.add('food');
}
function startGame() {
if (!isPlaying) {
allowedToMove = true;
play = setInterval(updatePosition, 800 / speed);
document.getElementById('menu').style.display = 'none';
document.getElementById('map').style.display = '';
isPlaying = true;
}
}
function pauseGame() {
if (isPlaying) {
allowedToMove = false;
clearInterval(play);
document.getElementById('menu-text').innerText = 'PAUSED\nPress ENTER to resume';
document.getElementById('menu').style.display = '';
document.getElementById('map').style.display = 'none';
isPlaying = false;
}
}
function gameOver() {
clearInterval(play);
document.getElementById('menu-text').innerText =
'Game Over\nYour Score: ' +
(wormBody.length - 1) +
'\nPress ENTER to restart!';
document.getElementById('menu').style.display = '';
document.getElementById('map').style.display = 'none';
map.innerText = ''; // Clearing the map
initializeGameState(map.id); // Re-generating the map
}
function updatePosition() {
var newPosR, newPosC;
var head = wormBody[wormBody.length - 1];
switch (direction) {
case 'Up':
newPosR = head[0] - 1;
newPosC = head[1];
break;
case 'Down':
newPosR = head[0] + 1;
newPosC = head[1];
break;
case 'Left':
newPosR = head[0];
newPosC = head[1] - 1;
break;
case 'Right':
newPosR = head[0];
newPosC = head[1] + 1;
break;
default:
break;
}
// Checking if worm hit the wall
if (newPosR < 0 || newPosR > 9 || newPosC < 0 || newPosC > 9) {
gameOver();
} else {
wormBody.push([newPosR, newPosC]);
updateScreen();
allowedToMove = true;
}
}
function updateScreen() {
var tailArray = wormBody.shift();
var tail = parseInt(tailArray[0] + '' + tailArray[1]);
var headArray = wormBody[wormBody.length - 1];
var head = parseInt(headArray[0] + '' + headArray[1]);
// Checking if the worm bite its body
if (map.children[head].classList.contains('worm-body')) {
gameOver();
} else {
// Adds the new head block
map.children[head].classList.add('worm-body');
// Removes the tail block
map.children[tail].classList.remove('worm-body');
// If worm eats the food
if (head == food) {
map.children[food].classList.remove('food');
wormBody.unshift(tailArray);
// Checking if the worm reached its max size
wormBody.length == 100 && gameOver();
generateFood();
}
}
}
// CONTROLS
document.onkeydown = keyPress;
function keyPress(e) {
e.preventDefault();
e = e || window.event;
// Escape key is pressed
e.keyCode == 27 && pauseGame();
// Enter key is pressed
e.keyCode == 13 && startGame();
let up = 38;
let down = 40;
let left = 37;
let right = 39;
if (allowedToMove) {
allowedToMove = false;
switch (e.keyCode) {
case left:
direction != 'Right' && (direction = 'Left');
break;
case up:
direction != 'Down' && (direction = 'Up');
break;
case right:
direction != 'Left' && (direction ='Right');
break;
case down:
direction != 'Up' && (direction = 'Down');
break;
default:
allowedToMove = true;
break;
}
}
}
// Initiates the game
initializeGameState('map');