-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaking_first_phaser_game.html
245 lines (152 loc) · 5.54 KB
/
making_first_phaser_game.html
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Phaser - Making your first game</title>
<script type="text/javascript" src="js/phaser.min.js"></script>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
function preload() {
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);
game.load.spritesheet('baddie', 'assets/baddie.png', 32, 32)
}
var platforms;
var player;
var health;
var cursors;
var rKey;
var stars;
var score = 0;
var scoreText;
var gameOver;
var tryAgain;
var enemy;
function create() {
/* Create world */
game.physics.startSystem(Phaser.Physics.ARCADE); //enable arcade physics system
game.add.sprite(0, 0, 'sky'); //add bg
platforms = game.add.group(); //contains ground and two ledges
platforms.enableBody = true; //enable physics for any object created in this group
var ground = platforms.create(0, game.world.height - 64, 'ground'); //create ground
ground.scale.setTo(2,2); //fit sprite to the width of the game (original sprite is 400x32, we need 800 px of width)
ground.body.immovable = true; //keeps it from falling when jumping on it
var ledge = platforms.create(400,400,'ground'); //create first ledge
ledge.body.immovable = true;
ledge = platforms.create(-150,250,'ground'); //create second ledge
ledge.body.immovable = true;
/* Create player */
player = game.add.sprite(32, game.world.height - 150, 'dude'); //add player
game.physics.arcade.enable(player); //enable physics on player
//caracter's physic properties
player.body.bounce.y = 0.2;
player.body.gravity.y = 300;
player.body.collideWorldBounds = true;
//walk left and walk right animation
player.animations.add('left', [0, 1, 2, 3], 10, true); //(direction, frames, frame per sec, enable to loop)
player.animations.add('right', [5, 6, 7, 8], 10, true);
player.health = 50;
/* Create stars */
stars = game.add.group();
stars.enableBody = true;
for (var i = 0; i < 12; i++){
var star = stars.create(i * 70, 0, 'star'); //create a star in 'stars' group
star.body.gravity.y = 6;
star.body.bounce.y = 0.7 + Math.random() * 0.2; //slight random bounce value
}
/* Score line */
scoreText = game.add.text(16, 16, 'score: 0', {fontsize: '32px', fill: '#000'});
/* Create enemies */
enemy = game.add.sprite(200, game.world.height - 94, 'baddie');
var tweenemy = game.add.tween(enemy).to({ x: 600 }, 1500, Phaser.Easing.Linear.None, true, 0, Number.MAX_VALUE, true)
.to({ x: 300}, 1500, Phaser.Easing.Linear.None, true, 0, Number.MAX_VALUE, true)
.loop()
.start();
//Add animations!
//Need both left and righ, depending on path direction?
/* Game over line */
gameOver = game.add.text(game.world.centerX, 200, '', {font: '50px Courier', fill: '#000'});
gameOver.anchor.setTo(0.5);
/* Victory line */
victory = game.add.text(game.world.centerX, 200, '', {font: '50px Courier', fill: '#000'});
victory.anchor.setTo(0.5);
/* Try again */
tryAgain = game.add.text(game.world.centerX, 350, '', {font: '35px Courier', fill: '#000'});
tryAgain.anchor.setTo(0.5);
/* R Key */
}
function update() {
game.physics.arcade.collide(player, platforms);
game.physics.arcade.collide(stars, platforms);
//no physics enabled on enemy, otherwise tween wasn't working
game.physics.arcade.overlap(player, stars, collectStar, null, this);
cursors = game.input.keyboard.createCursorKeys();
//reset player velocity
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 if (cursors.down.isDown ){
//speed up fall
player.body.velocity.y = 350;
//what if left or right is still down?
} else {
//stand still
player.animations.stop();
player.frame = 4;
}
//jump if up arrow is pressed and caracter is touching the ground
if(cursors.up.isDown && player.body.touching.down){
player.body.velocity.y = -325;
}
//check if enemy and player are overlaping
if(checkOverLap(player, enemy)){
baddieTouch(enemy, player);
}
//press R to retry
if(game.input.keyboard.isDown(Phaser.Keyboard.R)){
player.reset(32, game.world.height - 150);
restart();
}
}
function checkOverLap(spriteA, spriteB){
var boundsA = spriteA.getBounds();
var boundsB = spriteB.getBounds();
return Phaser.Rectangle.intersects(boundsA, boundsB);
}
function collectStar(player, star){
star.kill(); //removes star from screen
score += 10; //score increases by 10
scoreText.text = 'Score: ' + score; //displays the actual score
if(score == 120){
victory.text = 'Victory!'; //if all the stars have been collected, display Victory!
//add some kind of end, kill all enemies or freeze everything?
}
}
function baddieTouch(player, baddie){
baddie.kill();
gameOver.text = 'Game over :/';
tryAgain.text = 'Try again? (R)';
}
function restart(){
score = 0;
gameOver.text = '';
tryAgain.text = '';
}
</script>
</body>
</html>