Skip to content

Commit d2b3487

Browse files
committed
initial commit
0 parents  commit d2b3487

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed

assets/bomb.png

495 Bytes
Loading

assets/dude.png

3.15 KB
Loading

assets/platform.png

14.6 KB
Loading

assets/sky.png

4.65 KB
Loading

assets/star.png

443 Bytes
Loading

index.html

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<title>Making your first Phaser 3 Game - Part 10</title>
7+
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
8+
<style type="text/css">
9+
body {
10+
margin: 0;
11+
}
12+
</style>
13+
</head>
14+
15+
<body>
16+
17+
<script type="text/javascript">
18+
var config = {
19+
type: Phaser.AUTO,
20+
width: 800,
21+
height: 600,
22+
physics: {
23+
default: 'arcade',
24+
arcade: {
25+
gravity: {
26+
y: 300
27+
},
28+
debug: false
29+
}
30+
},
31+
scene: {
32+
preload: preload,
33+
create: create,
34+
update: update
35+
}
36+
};
37+
38+
var player;
39+
var stars;
40+
var bombs;
41+
var platforms;
42+
var cursors;
43+
var score = 0;
44+
var gameOver = false;
45+
var scoreText;
46+
47+
var game = new Phaser.Game(config);
48+
49+
function preload() {
50+
this.load.image('sky', 'assets/sky.png');
51+
this.load.image('ground', 'assets/platform.png');
52+
this.load.image('star', 'assets/star.png');
53+
this.load.image('bomb', 'assets/bomb.png');
54+
this.load.spritesheet('dude', 'assets/dude.png', {
55+
frameWidth: 32,
56+
frameHeight: 48
57+
});
58+
}
59+
60+
function create() {
61+
this.add.image(400, 300, 'sky');
62+
platforms = this.physics.add.staticGroup();
63+
platforms.create(400, 568, 'ground').setScale(2).refreshBody();
64+
platforms.create(600, 400, 'ground');
65+
platforms.create(50, 250, 'ground');
66+
platforms.create(750, 220, 'ground');
67+
platforms.create
68+
player = this.physics.add.sprite(100, 450, 'dude');
69+
player.setBounce(0.2);
70+
player.setCollideWorldBounds(true);
71+
this.anims.create({
72+
key: 'left',
73+
frames: this.anims.generateFrameNumbers('dude', {
74+
start: 0,
75+
end: 3
76+
}),
77+
frameRate: 10,
78+
repeat: -1
79+
});
80+
81+
this.anims.create({
82+
key: 'turn',
83+
frames: [{
84+
key: 'dude',
85+
frame: 4
86+
}],
87+
frameRate: 20
88+
});
89+
90+
this.anims.create({
91+
key: 'right',
92+
frames: this.anims.generateFrameNumbers('dude', {
93+
start: 5,
94+
end: 8
95+
}),
96+
frameRate: 10,
97+
repeat: -1
98+
});
99+
100+
cursors = this.input.keyboard.createCursorKeys();
101+
stars = this.physics.add.group({
102+
key: 'star',
103+
repeat: 11,
104+
setXY: {
105+
x: 12,
106+
y: 0,
107+
stepX: 70
108+
}
109+
});
110+
111+
stars.children.iterate(function (child) {
112+
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
113+
});
114+
115+
bombs = this.physics.add.group();
116+
117+
scoreText = this.add.text(16, 16, 'score: 0', {
118+
fontSize: '32px',
119+
fill: '#000'
120+
});
121+
122+
this.physics.add.collider(player, platforms);
123+
this.physics.add.collider(stars, platforms);
124+
this.physics.add.collider(bombs, platforms);
125+
126+
this.physics.add.overlap(player, stars, collectStar, null, this);
127+
128+
this.physics.add.collider(player, bombs, hitBomb, null, this);
129+
}
130+
131+
function update() {
132+
if (gameOver) {
133+
return;
134+
}
135+
136+
if (cursors.left.isDown) {
137+
player.setVelocityX(-160);
138+
139+
player.anims.play('left', true);
140+
} else if (cursors.right.isDown) {
141+
player.setVelocityX(160);
142+
143+
player.anims.play('right', true);
144+
} else {
145+
player.setVelocityX(0);
146+
147+
player.anims.play('turn');
148+
}
149+
150+
if (cursors.up.isDown && player.body.touching.down) {
151+
player.setVelocityY(-330);
152+
}
153+
}
154+
155+
function collectStar(player, star) {
156+
star.disableBody(true, true);
157+
158+
score += 10;
159+
scoreText.setText('Score: ' + score);
160+
161+
if (stars.countActive(true) === 0) {
162+
stars.children.iterate(function (child) {
163+
164+
child.enableBody(true, child.x, 0, true, true);
165+
166+
});
167+
168+
var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
169+
170+
var bomb = bombs.create(x, 16, 'bomb');
171+
bomb.setBounce(1);
172+
bomb.setCollideWorldBounds(true);
173+
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
174+
bomb.allowGravity = false;
175+
176+
}
177+
}
178+
179+
function hitBomb(player, bomb) {
180+
this.physics.pause();
181+
182+
player.setTint(0xff0000);
183+
184+
player.anims.play('turn');
185+
186+
gameOver = true;
187+
}
188+
</script>
189+
190+
</body>
191+
192+
</html>

0 commit comments

Comments
 (0)