-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
442 lines (336 loc) · 12.6 KB
/
index.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Dodger Game</title>
<script src="js/phaser.min.js"></script>
<style type="text/css">
canvas{
width:100%;
height:100%;
}
</style>
</head>
<body>
<script type="text/javascript">
var game;
var player;
var zombies;
var coins;
var score = 0;
var elapsedSecondsPlaying = 0;
var background;
var cursors;
var gameState = 'TITLE_MENU'; //PLAYING, GAMEOVER, PAUSED
var playerState = 'ALIVE';
var startGameText;
var pauseGameText;
var gameoverGameText;
var scoreText = '';
var playerBaseSpeed = 200;
var pauseKey;
var pauseKeyDown = false;
var coin_sound;
var zombie_sound;
var touched_sound;
var game_music;
window.onload = function() {
//init game object and and disable antialiasing with last params
//new Game(width, height, renderer, parent, state, transparent, antialias, physicsConfig)
game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render:render }, true, false);
function preload () {
//images
game.load.image('backgroundRoad', 'img/background-1.png');
game.load.spritesheet('characters', 'img/7DRL-Characters-old.png', 16, 16);
game.load.spritesheet('coin_copper', 'img/coin_copper.png', 32, 32);
game.load.spritesheet('coin_gold', 'img/coin_gold.png', 32, 32);
game.load.spritesheet('coin_silver', 'img/coin_silver.png', 32, 32);
//audio
game.load.audio('coin_sound', 'sounds/coin_1.wav');
game.load.audio('zombie_sound', 'sounds/zombie-16.wav');
game.load.audio('touched_sound', 'sounds/deathh.wav');
game.load.audio('game_music', 'sounds/wyver9_Fast Level.wav');
}
function create () {
//set physics
game.physics.startSystem(Phaser.Physics.ARCADE);
//init background and resize to fit
background = game.add.sprite(0, 0, 'backgroundRoad');
background.scale.setTo( game.world.width/background.width , game.world.height/background.height);
//create enemies group
zombies = game.add.group();
game.physics.arcade.enable(zombies);
zombies.enableBody = true;
//create coins group
coins = game.add.group();
game.physics.arcade.enable(coins);
coins.enableBody = true;
//create player
player = game.add.sprite(game.world.width/2, game.world.height/2, 'characters');
//scale player
player.scale.setTo(4,4);
//set physics to player
game.physics.arcade.enable(player);
//set player animations
player.animations.add('move', [0, 1, 2, 3], 4, true);
player.enableBody = true;
player.body.setSize(12, 12, 2, 2);
//add start game instructions
startGameText = game.add.text(game.world.centerX, game.world.centerY, 'Click / Touch to start', { font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" });
startGameText.anchor.set(0.5);
startGameText.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);
//add score text
scoreText = game.add.text(16, 16, 'Score: '+score, { font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" });
//scoreText.anchor.set(0.5);
scoreText.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);
//bind click to start game
game.input.onDown.add(startGame, this);
//Set sounds
coin_sound = game.add.audio('coin_sound');
coin_sound.volume = 0.5;
zombie_sound = game.add.audio('zombie_sound');
zombie_sound.volume = 0.5;
touched_sound = game.add.audio('touched_sound');
touched_sound.volume = 0.5;
game_music = game.add.audio('game_music');
game_music.volume = 0.3;
game_music.loop = true;
}
function update () {
//Game Logic
gameCodeBlock();
//Player Logic
playerCodeBlock();
zombiesCodeBlock();
coinsCodeBlock();
//reset pause game button
pauseKeyDown = false;
}
function startGame(){
console.log("Start Game");
game.input.onDown.remove(startGame, this);
gameState = "PLAYING";
startGameText.destroy();
//bind pause button
pauseKey = game.input.keyboard.addKey(Phaser.Keyboard.P);
pauseKey.onDown.add(updatePauseKeyDown, this);
//bind restart button
pauseKey = game.input.keyboard.addKey(Phaser.Keyboard.R);
pauseKey.onDown.add(updateRestartKeyDown, this);
//animate player
player.animations.play('move');
game.time.events.loop(Phaser.Timer.SECOND, updateScore, this);
game.time.events.loop(Phaser.Timer.SECOND*2, playZombieSound, this);
game.time.events.loop(Phaser.Timer.SECOND/2, createZombies, this);
game.time.events.loop(Phaser.Timer.SECOND/2, createCoins, this);
game_music.play();
}
function updatePauseKeyDown (){
pauseGame();
}
function updateRestartKeyDown(){
if(gameState == "GAMEOVER"){
score = 0;
elapsedSecondsPlaying = 0;
game.state.start(game.state.current);
}
}
function pauseGame(){
if(gameState == "PLAYING"){
gameState = "PAUSE";
pauseGameText = game.add.text(game.world.centerX, game.world.centerY, 'Game Paused, Press P to Continue', { font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" });
pauseGameText.anchor.set(0.5);
pauseGameText.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);
player.animations.stop();
zombies.animations.stop();
coins.animations.stop();
}
else
if(gameState == "PAUSE"){
gameState = "PLAYING";
pauseGameText.destroy();
player.animations.play('move');
zombies.animations.play('move');
coins.animations.play('move');
}
}
function updateScore(){
if(gameState == "PLAYING"){
score += 10;
elapsedSecondsPlaying += 1;
}
}
function gameOverGame(){
player.enableBody = false;
game_music.stop();
gameState = "GAMEOVER";
gameoverGameText = game.add.text(game.world.centerX, game.world.centerY, "Game Over \n Your Score: " + score + "\n Pres R to Restart", {align: "center", font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" });
gameoverGameText.anchor.set(0.5);
gameoverGameText.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);
//player.animations.stop();
}
function createZombies(){
var minX;
var maxX;
var spawnX;
var maxSpeed;
var nZombies;
nZombies = (elapsedSecondsPlaying/20)+1;
if(nZombies > 3){
nZombies = 3;
}
if(gameState == "PLAYING"){
for(x=0; x< nZombies; x++){
minX = 8;
maxX = game.world.width - minX;
spawnX = Math.floor(Math.random() * maxX) + minX;
maxSpeed = 300;
var zombie = zombies.create( spawnX, -20, 'characters');
zombie.scale.setTo(4,4);
zombie.animations.add('move', [33,34,35, 36], 2, true);
zombie.animations.play('move');
zombie.body.velocity.y = Math.floor(Math.random() * maxSpeed)+100;
zombie.body.setSize(9, 12, 4, 3);
//zombie.body.setCircle(8);
}
}
}
function createCoins(){
var minX;
var maxX;
var spawnX;
var maxSpeed;
var nCoins;
nCoins = (elapsedSecondsPlaying/20)+1;
if(nCoins > 3){
nCoins = 3;
}
if(gameState == "PLAYING"){
for(x=0; x< nCoins; x++){
minX = 8;
maxX = game.world.width - minX;
spawnX = Math.floor(Math.random() * maxX) + minX;
maxSpeed = 300;
var coin = coins.create( spawnX, -20, 'coin_gold');
coin.scale.setTo(2,2);
coin.animations.add('move', [0, 1, 2, 3, 4, 5, 6, 7], 2, true);
coin.animations.play('move');
coin.body.velocity.y = Math.floor(Math.random() * maxSpeed)+100;
//coin.body.setSize(14, 14, 6, 4);
coin.body.setCircle(9, 8,5);
}
}
}
function playerCodeBlock(){
//player touch enemy
if(gameState == "PLAYING")
game.physics.arcade.overlap(player, zombies,
function (player, zombie){
//player.destroy();
touched_sound.play();
gameOverGame();
}
, null, this);
//player touch coin
if(gameState == "PLAYING")
game.physics.arcade.overlap(player, coins,
function (player, coin){
coin_sound.play();
coin.destroy();
score += 15;
}
, null, this);
//player movement
if(gameState == "PLAYING"){
player.x = game.input.x-player.width/2;
player.y = game.input.y-player.height/2;
}
}
function zombiesCodeBlock(){
//For each zombie
zombies.forEach(function(zombie){
//Move
if(gameState == "PLAYING"){
//zombie.y += zombie.speed;
}
//Destroy
if(gameState == "PLAYING")
if(zombie.y > game.world.height){
zombie.destroy();
}
});
}
function coinsCodeBlock(){
//For each zombie
coins.forEach(function(coin){
//Move
if(gameState == "PLAYING"){
//coin.y += coin.speed;
}
//Destroy
if(gameState == "PLAYING")
if(coin.y > game.world.height){
coin.destroy();
}
});
}
function playZombieSound(){
if(gameState == "PLAYING")
zombie_sound.play();
}
function gameCodeBlock(){
scoreText.setText('Score: ' + score);
}
function render() {
// Sprite debug info
//game.debug.spriteInfo(player, 32, 100);
//game.debug.text( "N Zombies: "+zombies.length+" Seconds: "+elapsedSecondsPlaying, 100, 380 );
/*coins.forEach(function(coin){
game.debug.body(coin);
});
zombies.forEach(function(zombie){
game.debug.body(zombie);
})
game.debug.body(player);*/
}
/*function playerController(){
player.body.velocity.x = 0;
player.body.velocity.y = 0;
if (cursors.left.isDown)
{
// Move to the left
player.body.velocity.x = -playerBaseSpeed;
player.animations.play('move');
}
if (cursors.right.isDown)
{
// Move to the right
player.body.velocity.x = playerBaseSpeed;
player.animations.play('move');
}
if (cursors.up.isDown)
{
// Move to the right
player.body.velocity.y = -playerBaseSpeed;
player.animations.play('move');
}
if (cursors.down.isDown)
{
// Move to the right
player.body.velocity.y = playerBaseSpeed;
player.animations.play('move');
}
if(player.body.velocity.x == 0 && player.body.velocity.y == 0)
{
// Stand still
player.animations.play('move');
}
if(pauseKeyDown){
console.log("pauseKeyDown: ", pauseKeyDown);
game.state.restart();
}
}*/
};
</script>
</body>
</html>