-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
162 lines (143 loc) · 4.63 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
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
// define variables
var game;
var player;
var platforms;
var badges;
var items;
var cursors;
var jumpButton;
var text;
var winningMessage;
var won = false;
var currentScore = 0;
var winningScore = 100;
// add collectable items to the game
function addItems() {
items = game.add.physicsGroup();
createItem(375, 400, 'coin');
createItem(575, 500, 'coin');
createItem(225, 500, 'coin');
createItem(100, 250, 'coin');
createItem(575, 150, 'coin');
createItem(525, 300, 'coin');
createItem(650, 250, 'coin');
createItem(225, 200, 'coin');
createItem(375, 100, 'poison');
createItem(370,500,'poison');
createItem(100, 375, 'poison');
createItem(125, 50, 'star');
}
// add platforms to the game
function addPlatforms() {
platforms = game.add.physicsGroup();
platforms.create(450, 550, 'platform');
platforms.create(100, 550, 'platform');
platforms.create(300, 450, 'platform');
platforms.create(250, 150, 'platform');
platforms.create(50, 300, 'platform');
platforms.create(150, 250, 'platform');
platforms.create(650, 300, 'platform');
platforms.create(550, 200, 'platform2');
platforms.create(300, 450, 'platform2');
platforms.create(400, 350, 'platform2');
platforms.create(100, 100, 'platform2');
platforms.setAll('body.immovable', true);
}
// create a single animated item and add to screen
function createItem(left, top, image) {
var item = items.create(left, top, image);
item.animations.add('spin');
item.animations.play('spin', 10, true);
}
// create the winning badge and add to screen
function createBadge() {
badges = game.add.physicsGroup();
var badge = badges.create(750, 400, 'badge');
badge.animations.add('spin');
badge.animations.play('spin', 10, true);
}
// when the player collects an item on the screen
function itemHandler(player, item) {
item.kill();
if(item.key === 'coin'){
currentScore = currentScore + 10;
} else if (item.key === 'poison'){
currentScore = currentScore - 25;
} else if (item.key === 'star') {
currentScore = currentScore + 25;
}
if (currentScore === winningScore) {
createBadge();
}
}
// when the player collects the badge at the end of the game
function badgeHandler(player, badge) {
badge.kill();
won = true;
}
// setup game when the web page loads
window.onload = function () {
game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
// before the game begins
function preload() {
game.stage.backgroundColor = '#5db1ad';
//Load images
game.load.image('platform', 'platform_1.png');
game.load.image('platform2', 'platform_2.png');
//Load spritesheets
game.load.spritesheet('player', 'chalkers.png', 48, 62);
game.load.spritesheet('coin', 'coin.png', 36, 44);
game.load.spritesheet('badge', 'badge.png', 42, 54);
game.load.spritesheet('poison', 'poison.png', 32, 32);
game.load.spritesheet('star', 'star.png', 32, 32);
}
// initial game set up
function create() {
player = game.add.sprite(50, 600, 'player');
player.animations.add('walk');
player.anchor.setTo(0.5, 1);
game.physics.arcade.enable(player);
player.body.collideWorldBounds = true;
player.body.gravity.y = 500;
addItems();
addPlatforms();
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
text = game.add.text(16, 16, "SCORE: " + currentScore, { font: "bold 24px Arial", fill: "white" });
winningMessage = game.add.text(game.world.centerX, 275, "", { font: "bold 48px Arial", fill: "white" });
winningMessage.anchor.setTo(0.5, 1);
}
// while the game is running
function update() {
text.text = "SCORE: " + currentScore;
game.physics.arcade.collide(player, platforms);
game.physics.arcade.overlap(player, items, itemHandler);
game.physics.arcade.overlap(player, badges, badgeHandler);
player.body.velocity.x = 0;
// is the left cursor key presssed?
if (cursors.left.isDown) {
player.animations.play('walk', 10, true);
player.body.velocity.x = -300;
player.scale.x = - 1;
}
// is the right cursor key pressed?
else if (cursors.right.isDown) {
player.animations.play('walk', 10, true);
player.body.velocity.x = 300;
player.scale.x = 1;
}
// player doesn't move
else {
player.animations.stop();
}
if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down)) {
player.body.velocity.y = -400;
}
// when the player winw the game
if (won) {
winningMessage.text = "YOU WIN!!!";
}
}
function render() {
}
};