This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockdodger.html
321 lines (295 loc) · 11.7 KB
/
blockdodger.html
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<!DOCTYPE html>
<html>
<head>
<title>Test Game</title>
<meta charset="utf-8"/>
<style>
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
/* global $, alert */
$(document).ready( function() {
"use strict";
const canvasWidth = 480;
const canvasHeight = 480;
const playerWidth = 30;
const playerHeight = 30;
const enemyWidth = 25;
const enemyHeight = 25;
const UP = 0;
const RIGHT = 1;
const DOWN = 2;
const LEFT = 3;
const defaultEnemySpawnCooldown = 750;
var gameArea = {
canvas: document.createElement("canvas"),
start: function() {
this.canvas.width = canvasWidth;
this.canvas.height = canvasHeight;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 20);
},
clear: function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
}
// These variable track the state of this game
var player, enemies, score, enemySpawnCooldown, enemySpawnTimer, time, gameOver;
startGame();
function startGame() {
player = new character(playerWidth, playerHeight, "green",
canvasWidth/2-playerWidth/2, canvasHeight/2-playerHeight/2, 0.2);
enemies = [];
score = 0;
enemySpawnCooldown = defaultEnemySpawnCooldown;
enemySpawnTimer = enemySpawnCooldown;
time = new Date().getTime();
gameOver = false;
gameArea.start();
}
function character(width, height, color, x, y, speed) {
this.width = width;
this.height = height;
this.color = color;
this.x = x;
this.y = y;
this.speed = speed;
this.direction = -1;
this.draw = function() {
const ctx = gameArea.context;
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.move = function(deltatime) {
// Move character
if (this.direction === UP) {
this.y -= this.speed*deltatime;
} else if (this.direction === RIGHT) {
this.x += this.speed*deltatime;
if (player.outsideGameArea()) {
this.x -= canvasWidth + playerWidth;
}
} else if (this.direction === DOWN) {
this.y += this.speed*deltatime;
if (player.outsideGameArea()) {
this.y -= canvasHeight + playerHeight;
}
} else if (this.direction === LEFT) {
this.x -= this.speed*deltatime;
if (player.outsideGameArea()) {
this.x += canvasWidth + playerWidth;
}
}
}
this.collidesWith = function(other) {
//Check if the rectangles overlap
return !((this.y + this.height < other.y) || (this.y > other.y + other.height) ||
(this.x + this.width < other.x) || (this.x > other.x + other.width));
}
this.outsideGameArea = function() {
return !this.collidesWith({x: 0, y: 0, width: canvasWidth, height: canvasHeight});
}
}
function loadCharacter(characterVars) {
var new_character = new character(characterVars.width, characterVars.height,
characterVars.color, characterVars.x, characterVars.y, characterVars.speed);
new_character.direction = characterVars.direction;
return new_character;
}
function drawScore(text, width, height, color) {
const ctx = gameArea.context;
ctx.font = width + " " + height;
ctx.fillStyle = color;
const metrics = ctx.measureText(text);
ctx.fillText(text, canvasWidth - metrics.width - 20, 30);
}
function updateGameArea() {
if (gameOver) return;
//Calculate the time since last frame
var currentTime = new Date().getTime();
var deltatime = currentTime-time;
time = currentTime;
if (deltatime > 500) return;
gameArea.clear();
//Spawn new enemies
enemySpawnTimer -= deltatime;
if (enemySpawnTimer <= 0) {
enemySpawnTimer = enemySpawnCooldown;
const enemySpawnDirection = Math.floor(Math.random()*4);
var enemyX;
var enemyY;
var enemyDirection;
if (enemySpawnDirection === UP) {
enemyY = -enemyHeight;
enemyX = Math.floor(Math.random()*(canvasWidth-enemyWidth+1));
enemyDirection = DOWN;
} else if (enemySpawnDirection === RIGHT) {
enemyY = Math.floor(Math.random()*(canvasHeight-enemyHeight+1));
enemyX = canvasWidth;
enemyDirection = LEFT;
} else if (enemySpawnDirection === DOWN) {
enemyY = canvasHeight;
enemyX = Math.floor(Math.random()*(canvasWidth-enemyWidth+1));
enemyDirection = UP;
} else if (enemySpawnDirection === LEFT) {
enemyY = Math.floor(Math.random()*(canvasHeight-enemyHeight+1));;
enemyX = -enemyWidth;
enemyDirection = RIGHT
}
const enemy = new character(enemyWidth, enemyHeight, "red", enemyX, enemyY, 0.1);
enemy.direction = enemyDirection;
enemies.push(enemy);
}
//When going outside of the game area, make player come out from the other side
player.move(deltatime);
if (player.outsideGameArea()) {
if (player.direction === UP) {
player.y += canvasHeight + playerHeight;
} else if (this.direction === RIGHT) {
player.x -= canvasWidth + playerWidth;
} else if (this.direction === DOWN) {
player.y -= canvasHeight + playerHeight;
} else if (this.direction === LEFT) {
player.x += canvasWidth + playerWidth;
}
}
//Move and draw enemies
for (var i = 0; i < enemies.length; i += 1) {
enemies[i].move(deltatime);
enemies[i].draw();
//Check collision with enemies
if (player.collidesWith(enemies[i])) {
gameOver = true;
}
// Delete enemies that go outside the game area
if (enemies[i].outsideGameArea()) {
delete enemies[i];
}
}
enemies = enemies.filter(function( enemy ) {
return enemy !== undefined;
});
player.draw();
//Increase score
score += deltatime/100;
const scoreText="SCORE: " + Math.floor(score);
drawScore(scoreText, "30px", "Consolas", "black");
//Make enemies spawn faster
if (enemySpawnCooldown > 0) {
enemySpawnCooldown -= deltatime/100;
}
if (gameOver) {
//Stop the game and submit score
clearInterval(gameArea.interval);
var msg = {
"messageType": "SCORE",
"score": score
};
window.parent.postMessage(msg, "*");
}
}
// Record keyboard inputs
$(document).keydown(function(event){
switch(event.which) {
// Left or A
case 37:
case 65:
player.direction = LEFT;
break;
// Up or W
case 38:
case 87:
player.direction = UP;
break;
// Right or D
case 39:
case 68:
player.direction = RIGHT;
break;
// Down or S
case 40:
case 83:
player.direction = DOWN;
break;
default: return;
}
event.preventDefault();
});
// Sends this game's state to the service.
// The format of the game state is decided
// by the game
$("#save").click( function () {
var msg = {
"messageType": "SAVE",
"gameState": {
"player": player,
"enemies": enemies,
"enemySpawnCooldown": enemySpawnCooldown,
"enemySpawnTimer": enemySpawnTimer,
"gameOver": gameOver,
"score": score
}
};
var obj = JSON.parse(JSON.stringify(msg));
window.parent.postMessage(obj, "*");
});
// Sends a request to the service for a
// state to be sent, if there is one.
$("#load").click( function () {
var msg = {
"messageType": "LOAD_REQUEST",
};
window.parent.postMessage(msg, "*");
});
// Listen incoming messages, if the messageType
// is LOAD then the game state will be loaded.
//
// Also handles any errors that the service
// wants to send (displays them as an alert).
window.addEventListener("message", function(evt) {
if(evt.data.messageType === "LOAD") {
var playerVars = evt.data.gameState.player;
var enemyVarsList = evt.data.gameState.enemies;
enemySpawnCooldown = evt.data.gameState.enemySpawnCooldown;
enemySpawnTimer = evt.data.gameState.enemySpawnTimer;
gameOver = evt.data.gameState.gameOver;
score = evt.data.gameState.score;
player = loadCharacter(playerVars);
enemies = [];
for (var i = 0; i < enemyVarsList.length; i += 1) {
enemies.push(loadCharacter(enemyVarsList[i]));
}
time = new Date().getTime();
gameArea.start();
} else if (evt.data.messageType === "ERROR") {
alert(evt.data.info);
}
});
$("#restart").click( function () {
startGame();
});
// Request the service to set the resolution of the
// iframe correspondingly
var message = {
messageType: "SETTING",
options: {
"width": 500, //Integer
"height": 530 //Integer
}
};
window.parent.postMessage(message, "*");
});
</script>
</head>
<body>
<br>
<button id="restart">Restart Game</button>
<button id="save">Save</button>
<button id="load">Load</button>
</body>
</html>