-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
89 lines (76 loc) · 2.5 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
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
class MainScene extends Phaser.Scene {
constructor() {
super({ key: 'MainScene' });
}
preload() {
// Load assets like sprites and sounds here
this.load.image('stella', 'assets/Stella.png');
this.load.image('treat', 'assets/treat.png');
}
create() {
// Initialize scene, positions of sprites, etc.
this.player = this.physics.add.sprite(100, 100, 'stella');
this.coin = this.physics.add.sprite(300, 300, 'treat');
// Initialize score and display it
this.score = 0;
let style = { font: '20px Arial', fill: '#000' };
this.scoreText = this.add.text(20, 20, 'Score: ' + this.score, style);
// Input handling
this.arrow = this.input.keyboard.createCursorKeys();
}
update() {
// Handle game logic like movements here
// Horizontal movements
if (this.arrow.right.isDown) {
// If the right arrow is pressed, move to the right
this.player.x += 3;
} else if (this.arrow.left.isDown) {
// If the left arrow is pressed, move to the left
this.player.x -= 3;
}
// Vertical movements
if (this.arrow.down.isDown) {
this.player.y += 3;
} else if (this.arrow.up.isDown) {
this.player.y -= 3;
}
// Collision handling
if (this.physics.overlap(this.player, this.coin)) {
// Call the hit() method
this.hit();
}
}
hit() {
// Change the position x and y of the coin randomly
this.coin.x = Phaser.Math.Between(100, 600);
this.coin.y = Phaser.Math.Between(100, 300);
// Increment the score by 100
this.score += 100;
// Display the updated score on the screen
this.scoreText.setText('Score: ' + this.score);
// Create a new tween
this.tweens.add({
targets: this.player, // on the player
duration: 200, // for 200ms
scaleX: 1.2, // that scale vertically by 20%
scaleY: 1.2, // and scale horizontally by 20%
yoyo: true, // at the end, go back to original scale
});
}
}
// Create the Phaser game instance
let config = {
type: Phaser.AUTO,
width: 700,
height: 400,
backgroundColor: '#a9d2d5',
physics: {
default: 'arcade',
arcade: {
// Configure arcade physics here if needed
}
},
parent: 'game', // This should match the ID of the HTML element where the game should be rendered
scene: [MainScene] // MainScene added to the game
};
let game = new Phaser.Game(config);