-
Notifications
You must be signed in to change notification settings - Fork 7
/
game.js
60 lines (50 loc) · 1.3 KB
/
game.js
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
class mainScene {
preload() {
this.load.image('player', 'assets/player.png');
this.load.image('coin', 'assets/coin.png');
}
create() {
this.player = this.physics.add.sprite(100, 100, 'player');
this.coin = this.physics.add.sprite(300, 200, 'coin');
this.score = 0;
let style = { font: '20px Arial', fill: '#fff' };
this.scoreText = this.add.text(20, 20, 'score: ' + this.score, style);
this.arrow = this.input.keyboard.createCursorKeys();
}
update() {
if (this.physics.overlap(this.player, this.coin)) {
this.hit();
}
if (this.arrow.right.isDown) {
this.player.x += 3;
} else if (this.arrow.left.isDown) {
this.player.x -= 3;
}
if (this.arrow.down.isDown) {
this.player.y += 3;
} else if (this.arrow.up.isDown) {
this.player.y -= 3;
}
}
hit() {
this.coin.x = Phaser.Math.Between(100, 600);
this.coin.y = Phaser.Math.Between(100, 200);
this.score += 10;
this.scoreText.setText('score: ' + this.score);
this.tweens.add({
targets: this.player,
duration: 200,
scaleX: 1.2,
scaleY: 1.2,
yoyo: true,
});
}
}
new Phaser.Game({
width: 700,
height: 300,
backgroundColor: '#3498db',
scene: mainScene,
physics: { default: 'arcade' },
parent: 'game',
});