-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
339 lines (268 loc) · 8.65 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="https://cdn3.iconfinder.com/data/icons/pokemon-go-3/512/pokemon_go_play_game_charcter-512.png" />
<meta name="description" content="GoGo Pikachu | Phaser 2D Platform Game">
<meta property="og:type" content="website">
<meta property="og:url" content="https://gogo-pikachu.vercel.app/">
<meta property="og:title" content="GoGo Pikachu">
<meta property="og:description" content="GoGo Pikachu | Phaser 2D Platform Game">
<meta property="og:image" content="https://res.cloudinary.com/ddlhtsgmp/image/upload/v1658590433/GoGo_Pikachu.png">
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="https://gogo-pikachu.vercel.app/">
<meta property="twitter:title" content="GoGo Pikachu">
<meta property="twitter:description" content="GoGo Pikachu | Phaser 2D Platform Game">
<meta property="twitter:image" content="https://res.cloudinary.com/ddlhtsgmp/image/upload/v1658590433/GoGo_Pikachu.png">
<title>GoGo Pikachu | Phaser 2D Platform Game</title>
<style>
* { padding: 0; margin: 0; }
canvas {
display: block;
margin:0 auto;
}
.mobile-message{
display: none;
font-family: Arial, Helvetica, sans-serif;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
@media screen and (max-width: 491px) {
canvas{
display: none;
}
.mobile-message {
display: block;
text-align: center;
margin: 0 auto;
}
}
</style>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
</head>
<body>
<div class="mobile-message">Recommend playing on desktop 💻</div>
<script>
/** GoGo Pikachu Script */
// phaser game config/ init
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 200 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
// globals
var player;
var thunderStones;
var toxicOrbs;
var platforms;
var cursors;
var score = 0;
var level = 0
var gameOver = false;
var levelText
var scoreText;
var gameOverText
var gameTitle
var ToggleMusicOn
// CONSTANTS
const GROUND_KEY = 'ground'
const PLAYER_KEY = 'pikachu'
const THUNDER_STONE_KEY = 'thunder-stone'
const TOXIC_ORB_KEY = 'toxic-orb'
const BACKGROUND_MUSIC_KEY = 'background-music'
const PIKACHU_CHANT_KEY = 'level-up'
const GAME_OVER_MUSIC_KEY = 'game-over'
// game init
var game = new Phaser.Game(config);
// PRELOAD
function preload ()
{
// load sky
this.load.image('sky', 'assets/images/sky.png')
// load ground
this.load.image(GROUND_KEY, 'assets/images/platform.png')
// load thunder stones
this.load.image(THUNDER_STONE_KEY, 'assets/images/thunder-stone.png')
// load pikachu/player
this.load.image(PLAYER_KEY, 'assets/images/pikachu.png')
// load toxicOrbs
this.load.image(TOXIC_ORB_KEY, 'assets/images/toxic-orb.png')
// load game music
this.load.audio(BACKGROUND_MUSIC_KEY, ['assets/music/gogo-pikachu-music.mp3']);
// load game over music
this.load.audio(GAME_OVER_MUSIC_KEY, ['assets/music/game-over.mp3']);
//// load pikachu chant
this.load.audio(PIKACHU_CHANT_KEY, ['assets/music/pikachu-echo-level-up.mp3']);
}
// CREATE
function create ()
{
// background
this.add.image(400, 300, 'sky');
// add background music
this.backgroundMusic = this.sound.add(BACKGROUND_MUSIC_KEY, {loop: true})
// check if music is locked
// toggle music on
if (this.sound.locked)
{
ToggleMusicOn = this.add.text(16, 60, 'Click to play audio 🔊', { fontSize: '20px', fill: '#000' });
this.sound.once('unlocked', function ()
{
ToggleMusicOn.destroy();
// play background music
this.backgroundMusic.play()
}, this);
} else {
// play background music
this.backgroundMusic.play()
}
// add pikachu chant
this.pikachu = this.sound.add(PIKACHU_CHANT_KEY, { loop: false });
// create platform group
platforms = this.physics.add.staticGroup();
// platform ground
platforms.create(400, 568, GROUND_KEY).setScale(2).refreshBody();
// other platforms
platforms.create(600, 400, GROUND_KEY);
platforms.create(50, 250, GROUND_KEY);
platforms.create(750, 220, GROUND_KEY);
// create player
player = this.physics.add.sprite(95, 450, PLAYER_KEY).setDisplaySize(50, 50)
// set bounce
player.setBounce(0.1)
// set bounds -> only movable in canvas
player.setCollideWorldBounds(true)
// input -> cursors
cursors = this.input.keyboard.createCursorKeys();
// add thunder stones to collect
thunderStones = this.physics.add.group({
key: THUNDER_STONE_KEY,
repeat: 11,
setXY: { x: 12, y: 0, stepX: 70 },
setScale: { x: .3, y: .3}
});
thunderStones.children.iterate(function (child) {
// give each thunder stone a different bounce
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
toxicOrbs = this.physics.add.group();
// score
scoreText = this.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#000' });
// level
levelText = this.add.text(633, 16, 'Level: 0', { fontSize: '32px', fill: '#000' });
// game title
gameTitle = this.add.text(295, 16, 'GoGo Pikachu', { fontSize: '32px', fill: 'yellow' });
// collide player and the thunder stones with the platforms
this.physics.add.collider(player, platforms);
this.physics.add.collider(thunderStones, platforms);
this.physics.add.collider(toxicOrbs, platforms);
// Checks to see if the player overlaps with any of the thunder stones, if he does call the collectThunderStone function
this.physics.add.overlap(player, thunderStones, collectThunderStone, null, this);
this.physics.add.collider(player, toxicOrbs, hitToxicOrb, null, this);
}
// UPDATE
function update ()
{
if (gameOver)
{
return;
}
// go left
if (cursors.left.isDown)
{
player.setVelocityX(-160);
}
// go right
else if (cursors.right.isDown)
{
player.setVelocityX(160);
}
// stay grounded
else
{
player.setVelocityX(0);
}
// jump
if (cursors.up.isDown && player.body.touching.down)
{
player.setVelocityY(-330);
}
}
// collect thunder stones
function collectThunderStone (player, thunderStone)
{
thunderStone.disableBody(true, true);
// add and update score
score += 10;
scoreText.setText('Score: ' + score);
if (thunderStones.countActive(true) === 0)
{
// camera shake
this.cameras.main.shake(225);
// spawn new batch of thunder stones
thunderStones.children.iterate(function (child) {
child.enableBody(true, child.x, 0, true, true);
});
var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
// add/create toxic orb into the game for each level completed
var toxicOrb = toxicOrbs.create(x, 16, TOXIC_ORB_KEY);
toxicOrb.setBounce(1);
toxicOrb.setCollideWorldBounds(true);
toxicOrb.setVelocity(Phaser.Math.Between(-200, 200), 20);
toxicOrb.allowGravity = false;
// add and update level
level ++;
levelText.setText('Level: ' + level);
// if sound isn't locked
// init pikachu chant
if (!this.sound.locked) {
// play pikachu chant
this.pikachu.play()
} else {
// no pikachu sound
return
}
}
}
// player hits toxic orbs -> game over
function hitToxicOrb (player, toxicOrb)
{
this.physics.pause();
// stop background music
this.backgroundMusic.stop()
// init game over sound effect
this.gameOverSound = this.sound.add(GAME_OVER_MUSIC_KEY, {loop: false})
// play game over
this.gameOverSound.play()
player.setTint(0xff0000);
// game over
gameOverText = this.add.text(325, 300, 'Game Over!', { fontSize: '30px', fill: 'red', align: 'center', strokeThickness: 1, stroke: 'red' });
scoreText.visible = false
levelText.visible = false
gameTitle.visible = false
ToggleMusicOn.visible = false
gameOver = true;
// restart game/window
setTimeout(() => {
location.reload()
}, 4000)
}
</script>
</body>
</html>