-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
104 lines (91 loc) · 3.49 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const Phaser = require('phaser-ce');
let platforms;
let player;
let cursors;
let stars;
let score = 0;
let scoreText;
const preload = (game) => {
game.load.image('sky', 'assets/sky.png');
game.load.image('ground', 'assets/platform.png');
game.load.image('star', 'assets/star.png');
game.load.spritesheet('dude', 'assets/dude.png', 32, 48);
};
const create = (game) => {
// We're going to be using physics, so enable the Arcade Physics system
game.physics.startSystem(Phaser.Physics.ARCADE);
// A simple background for our game
game.add.sprite(0, 0, 'sky');
// The platforms group contains the ground and the 2 ledges we can jump on
platforms = game.add.group();
// We will enable physics for any object that is created in this group
platforms.enableBody = true;
// Here we create the ground.
const ground = platforms.create(0, game.world.height - 64, 'ground');
// Scale it to fit the width of the game (the original sprite is 400x32 in size)
ground.scale.setTo(2, 2);
// This stops it from falling away when you jump on it
ground.body.immovable = true;
// Now let's create two ledges
const ledge = platforms.create(400, 400, 'ground');
ledge.body.immovable = true;
const ledge2 = platforms.create(-150, 250, 'ground');
ledge2.body.immovable = true;
player = game.add.sprite(32, game.world.height - 150, 'dude');
// We need to enable physics on the player
game.physics.arcade.enable(player);
// Player physics properties. Give the little guy a slight bounce.
player.body.bounce.y = 0.2;
player.body.gravity.y = 300;
player.body.collideWorldBounds = true;
// Our two animations, walking left and right.
player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);
// Finally some stars to collect
stars = game.add.group();
// We will enable physics for any star that is created in this group
stars.enableBody = true;
// Here we'll create 12 of them evenly spaced apart
for (let i = 0; i < 12; i++) {
// Create a star inside of the 'stars' group
const star = stars.create(i * 70, 0, 'star');
// Let gravity do its thing
star.body.gravity.y = 300;
// This just gives each star a slightly random bounce value
star.body.bounce.y = 0.7 + Math.random() * 0.2;
}
scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });
cursors = game.input.keyboard.createCursorKeys();
};
const update = (game) => {
game.physics.arcade.collide(player, platforms);
game.physics.arcade.collide(stars, platforms);
// Checks to see if the player overlaps with any of the stars,
// if he does call the collectStar function
game.physics.arcade.overlap(player, stars, (player, star) => {
// Removes the star from the screen
star.kill();
score += 10;
scoreText.text = `Score: ${score}`;
}, null, this);
// Reset the players velocity (movement)
player.body.velocity.x = 0;
if (cursors.left.isDown) {
// Move to the left
player.body.velocity.x = -150;
player.animations.play('left');
} else if (cursors.right.isDown) {
// Move to the right
player.body.velocity.x = 150;
player.animations.play('right');
} else {
// Stand still
player.animations.stop();
player.frame = 4;
}
// Allow the player to jump if they are touching the ground.
if (cursors.up.isDown && player.body.touching.down) {
player.body.velocity.y = -350;
}
};
new Phaser.Game(800, 600, Phaser.AUTO, 'app', { preload, create, update });