-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.html
405 lines (320 loc) · 13.5 KB
/
game.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="fp.css">
<title>Maze With Us Game!</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="final.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/phaser/2.2.2/phaser.min.js"></script>
<script type="text/javascript">
WebFontConfig = {
google: { families: [ 'Abel::latin' ] }
};
(function() {
var wf = document.createElement('script');
wf.src = 'https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
</script>
<link rel="stylesheet" href="fp.css">
<!-- <style>
div {
margin: 100px 200px 200px 300px;
}
</style> -->
<script type="text/javascript">
var game = new Phaser.Game(640, 480, Phaser.CANVAS, 'game');
var PhaserGame = function (game) {
this.map = null;
this.layer = null;
this.car = null;
this.safetile = 1;
this.gridsize = 32;
this.speed = 300;
this.threshold = 100;
this.turnSpeed = 100;
this.marker = new Phaser.Point();
this.turnPoint = new Phaser.Point();
this.directions = [ null, null, null, null, null ];
this.opposites = [ Phaser.NONE, Phaser.RIGHT, Phaser.LEFT, Phaser.DOWN, Phaser.UP ];
this.current = Phaser.UP;
this.turning = Phaser.NONE;
self.timeout = false;
};
PhaserGame.prototype = {
init: function () {
this.physics.startSystem(Phaser.Physics.ARCADE);
},
preload: function () {
// We need this because the assets are on Amazon S3
// Remove the next 2 lines if running locally
//this.load.baseURL = 'http://files.phaser.io.s3.amazonaws.com/codingtips/issue005/';
//this.load.crossOrigin = 'anonymous';
this.load.tilemap('map', 'assets/maze.json', null, Phaser.Tilemap.TILED_JSON);
this.load.image('tiles', 'assets/tiles.png');
this.load.image('car', 'assets/heart32.png');
this.load.image('lvl1', 'assets/level1.jpg');
this.load.image('lvl2', 'assets/level2.jpg');
this.load.image('lvl3', 'assets/level3.jpg');
this.load.image('lvl4', 'assets/level4.jpg');
this.load.image('lvl5', 'assets/level5.jpg');
this.load.image('end', 'assets/endscreen.jpg');
},
create: function () {
this.map = this.add.tilemap('map');
this.map.addTilesetImage('tiles', 'tiles');
this.layer = this.map.createLayer('Tile Layer 1');
this.level = 0;
this.map.setCollisionBetween(3, 10000, true, this.layer);
this.car = this.add.sprite(48, 48, 'car');
this.car.anchor.set(0.5);
this.physics.arcade.enable(this.car);
this.cursors = this.input.keyboard.createCursorKeys();
// this.move(Phaser.DOWN);
},
checkKeys: function () {
console.log("HEYO")
if (this.cursors.left.isDown && this.current !== Phaser.LEFT)
{
this.checkDirection(Phaser.LEFT);
}
else if (this.cursors.right.isDown && this.current !== Phaser.RIGHT)
{
this.checkDirection(Phaser.RIGHT);
}
else if (this.cursors.up.isDown && this.current !== Phaser.UP)
{
this.checkDirection(Phaser.UP);
}
else if (this.cursors.down.isDown && this.current !== Phaser.DOWN)
{
this.checkDirection(Phaser.DOWN);
}
else
{
// This forces them to hold the key down to turn the corner
this.turning = Phaser.NONE;
}
},
checkDirection: function (turnTo) {
if (this.turning === turnTo || this.directions[turnTo] === null || this.directions[turnTo].index !== this.safetile)
{
// Invalid direction if they're already set to turn that way
// Or there is no tile there, or the tile isn't index a floor tile
return;
}
// Check if they want to turn around and can
if (this.current === this.opposites[turnTo])
{
this.move(turnTo);
}
else
{
this.turning = turnTo;
this.turnPoint.x = (this.marker.x * this.gridsize) + (this.gridsize / 2);
this.turnPoint.y = (this.marker.y * this.gridsize) + (this.gridsize / 2);
}
},
turn: function () {
var cx = Math.floor(this.car.x);
var cy = Math.floor(this.car.y);
// This needs a threshold, because at high speeds you can't turn because the coordinates skip past
if (!this.math.fuzzyEqual(cx, this.turnPoint.x, this.threshold) || !this.math.fuzzyEqual(cy, this.turnPoint.y, this.threshold))
{
return false;
}
this.car.x = this.turnPoint.x;
this.car.y = this.turnPoint.y;
this.car.body.reset(this.turnPoint.x, this.turnPoint.y);
this.move(this.turning);
this.turning = Phaser.NONE;
return true;
},
move: function (direction) {
var speed = this.speed;
if (direction === Phaser.LEFT || direction === Phaser.UP)
{
speed = -speed;
}
if (direction === Phaser.LEFT || direction === Phaser.RIGHT)
{
this.car.body.velocity.x = speed;
}
else
{
this.car.body.velocity.y = speed;
}
this.add.tween(this.car).to( { angle: this.getAngle(direction) }, this.turnSpeed, "Linear", true);
this.current = direction;
},
getAngle: function (to) {
// About-face?
if (this.current === this.opposites[to])
{
return "180";
}
if ((this.current === Phaser.UP && to === Phaser.LEFT) ||
(this.current === Phaser.DOWN && to === Phaser.RIGHT) ||
(this.current === Phaser.LEFT && to === Phaser.DOWN) ||
(this.current === Phaser.RIGHT && to === Phaser.UP))
{
return "-90";
}
return "90";
},
update: function () {
if (self.timeout) {
return;
}
if (this.car.x == 592 && this.car.y == 432) {
this.car.x = 48
this.car.y = 48
this.car.body.velocity.x = 0;
this.car.body.velocity.y = 0;
this.timeout = true;
if (this.level == 0) {
setTimeout(function(i, j) {
j.level++;
i.destroy()
j.timeout = false;
j.layer = j.map.createLayer('Tile Layer 2');
j.map.setCollisionBetween(3, 10000, true, j.layer);
j.game.world.bringToTop(j.car);
// this.move(Phaser.RIGH T);
}, 10000, this.add.sprite(0, 0, 'lvl1'), this);
}
else if (this.level == 1) {
setTimeout(function(i, j) {
j.level++;
i.destroy()
j.timeout = false;
j.layer = j.map.createLayer('Tile Layer 3');
j.map.setCollisionBetween(3, 10000, true, j.layer);
j.game.world.bringToTop(j.car);
// this.move(Phaser.RIGH T);
}, 10000, this.add.sprite(0, 0, 'lvl2'), this);
}
else if (this.level == 2) {
setTimeout(function(i, j) {
j.level++;
i.destroy()
j.timeout = false;
j.layer = j.map.createLayer('Tile Layer 4');
j.map.setCollisionBetween(3, 10000, true, j.layer);
j.game.world.bringToTop(j.car);
// this.move(Phaser.RIGH T);
}, 10000, this.add.sprite(0, 0, 'lvl3'), this);
}
else if (this.level == 3) {
setTimeout(function(i, j) {
j.level++;
i.destroy()
j.timeout = false;
j.layer = j.map.createLayer('Tile Layer 5');
j.map.setCollisionBetween(3, 10000, true, j.layer);
j.game.world.bringToTop(j.car);
// this.move(Phaser.RIGH T);
}, 10000, this.add.sprite(0, 0, 'lvl4'), this);
}
else if (this.level == 4) {
setTimeout(function(i, j) {
i.destroy()
j.add.sprite(0, 0, 'end');
// this.move(Phaser.RIGH T);
}, 10000, this.add.sprite(0, 0, 'lvl5'), this);
}
}
this.physics.arcade.collide(this.car, this.layer);
this.marker.x = this.math.snapToFloor(Math.floor(this.car.x), this.gridsize) / this.gridsize;
this.marker.y = this.math.snapToFloor(Math.floor(this.car.y), this.gridsize) / this.gridsize;
// Update our grid sensors
this.directions[1] = this.map.getTileLeft(this.layer.index, this.marker.x, this.marker.y);
this.directions[2] = this.map.getTileRight(this.layer.index, this.marker.x, this.marker.y);
this.directions[3] = this.map.getTileAbove(this.layer.index, this.marker.x, this.marker.y);
this.directions[4] = this.map.getTileBelow(this.layer.index, this.marker.x, this.marker.y);
this.checkKeys();
if (this.turning !== Phaser.NONE)
{
this.turn();
}
},
render: function () {
// Un-comment this to see the debug drawing
// for (var t = 1; t < 5; t++)
// {
// if (this.directions[t] === null)
// {
// continue;
// }
// var color = 'rgba(0,255,0,0.3)';
// if (this.directions[t].index !== this.safetile)
// {
// color = 'rgba(255,0,0,0.3)';
// }
// if (t === this.current)
// {
// color = 'rgba(255,255,255,0.3)';
// }
// this.game.debug.geom(new Phaser.Rectangle(this.directions[t].worldX, this.directions[t].worldY, 32, 32), color, true);
// }
// this.game.debug.geom(this.turnPoint, '#ffff00');
}
};
game.state.add('Game', PhaserGame, true);
</script>
<link rel="stylesheet" href="fp.css">
</head>
<body>
<div class="col-md-2">
<br>
<br>
<br>
<br>
<br>
<div id="mySidenav" class="sidenav">
<br>
<a href="index.html">MAZE WITH US</a>
<br>
<a href="game.html">Play the Game!</a>
<br>
<a href="beyond.html">Beyond the Game</a>
<br>
<a href="ack.html">Acknowledgements</a>
<br>
<a href="about.html">About Team GrowUp</a>
<br>
</div>
</div>
<div class="text-center col-md-12">
<h1></h1>
<p></p>
</div>
<h1> <div style = "background-color: white;padding:15px;">Play Maze With Us!</div></h1>
<h2 class="w3-center"></h2>
<div class="col-md-3">
<h2></h2>
<p></p>
</div>
<div class="col-md-3">
<p> <div id='game'> </div> </p>
</div>
<div class="col-md-3">
<p> <div id='game'> </div> </p>
</div>
<div class="col-md-3">
<h2>How to Play</h2>
<p> 1.Use the arrow keys to move up/down/left/right.</p>
<p> 2. Try to avoid obstacles in the maze!</p>
<p> 3. Each level completes once you get to the bottom right corner of the maze.</p>
<p> 4. After you successfully complete each level, Alexa will grow a few years!</p>
<p> 5. Refresh the page to play again :)</p>
<br>
</body>
</html>