-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
205 lines (189 loc) · 5.03 KB
/
script.js
File metadata and controls
205 lines (189 loc) · 5.03 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//block browser default action
window.addEventListener(
'keydown',
function(e) {
// space and arrow keys
if ([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
},
false
);
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
function setTimer() {
document.getElementById('button').style.display = 'none';
var timeLeft = 20;
var countDownArea = document.getElementById('countDownArea');
var timerId = setInterval(countdown, 1000);
var img = document.createElement('img');
img.src = 'scareFace.jpg';
var audio = new Audio('scream.mp3');
var flag = 0;
function countdown() {
if (timeLeft === 0 && flag === 0) {
document.getElementById('informationArea').style.display = 'none';
document.getElementById('gameContainer').style.display = 'none';
document.getElementById('flexContainer').appendChild(img);
audio.play();
flag++;
clearInterval(timerId);
} else {
countDownArea.innerHTML = timeLeft;
timeLeft--;
}
}
}
//box sizes
var sideSize = 25;
//set score
var score = 0;
//player position state
var position = {
x: 0,
y: 0
};
//food position state
let food = {
x: 1,
y: 1
};
//maze map
var map = [
[0, 1, 1, 1, 1, 1, 1, 1, 1, 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 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
//create the Map
function drawMap() {
for (var y = 0; y < map.length; y++) {
var row = map[y];
for (var x = 0; x < row.length; x++) {
var tile = {
y: y * sideSize,
x: x * sideSize,
width: sideSize,
height: sideSize
};
if (map[y][x] === 1) {
context.fillStyle = 'black';
context.fillRect(tile.x, tile.y, tile.width, tile.height);
}
}
}
}
//Create the player
function drawPlayer() {
context.fillStyle = 'blue';
context.fillRect(
position.x * sideSize,
position.y * sideSize,
sideSize,
sideSize
);
}
//Create the food
function drawFood() {
if (map[food.y][food.x] === 0) {
context.fillStyle = 'green';
context.fillRect(food.x * sideSize, food.y * sideSize, sideSize, sideSize);
} else {
var newFoodPosition = {
x: Math.round(Math.random() * 19),
y: Math.round(Math.random() * 19)
};
food = newFoodPosition;
drawFood();
}
}
//the player moves around the maze
document.addEventListener('keydown', direction);
function direction(event) {
let key = event.keyCode;
//left
if (key == 37) {
var newPosition = {
x: position.x - 1,
y: position.y
};
if (map[newPosition.y][newPosition.x] !== 1 && newPosition.x >= 0) {
position = newPosition;
drawPlayer();
}
}
//down
else if (key == 38) {
var newPosition = {
x: position.x,
y: position.y - 1
};
if (map[newPosition.y][newPosition.x] !== 1 && newPosition.y >= 0) {
position = newPosition;
drawPlayer();
}
}
//right
else if (key == 39) {
var newPosition = {
x: position.x + 1,
y: position.y
};
if (
map[newPosition.y][newPosition.x] !== 1 &&
newPosition.x < map[1].length
) {
position = newPosition;
drawPlayer();
}
}
//up
else if (key == 40) {
var newPosition = {
x: position.x,
y: position.y + 1
};
if (map[newPosition.y][newPosition.x] !== 1 && newPosition.y < map.length) {
position = newPosition;
drawPlayer();
}
}
}
function eat() {
if (food.x === position.x && food.y === position.y) {
score++;
document.getElementById('score').innerHTML = 'Score: ' + score;
var newFoodPosition = {
x: Math.round(Math.random() * 19),
y: Math.round(Math.random() * 19)
};
food = newFoodPosition;
drawFood();
}
}
//render the game with different functions built within
function render() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawMap();
drawPlayer();
drawFood();
eat();
window.requestAnimationFrame(render);
}
render();