-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflappy.js
181 lines (161 loc) · 5.77 KB
/
flappy.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// the functions associated with preload, create and update.
var actions = { preload: preload, create: create, update: update };
// the Game object used by the phaser.io library
var width = 700;
var height = 400;
var game = new Phaser.Game(width, height, Phaser.AUTO, "game", actions);
// Global score variable initialised to 0.
var score = 0;
// Global variable to hold the text displaying the score.
var labelScore;
// Global player variable declared but not initialised.
var player;
// Global pipes variable initialised to an empty collection
var pipes = [];
// Physics constants of the game
var gameGravity = 200;
var gameSpeed = 200;
var jumpPower = 200;
// the interval (in seconds) at which new pipe columns are spawned and the
// distance (in pixels) between ends of pipes.
var pipeInterval = 1.75;
var pipeGap = 100;
// Global variables to store the bonuses
var balloons = [];
var weights = [];
// Loads all resources for the game and gives them names.
function preload() {
// make image file available to game and associate with alias playerImg
game.load.image("playerImg","assets/flappy-cropped.png");
// make sound file available to game and associate with alias score
game.load.audio("score", "assets/point.ogg");
// make image file available to game and associate with alias pipe
game.load.image("pipe","assets/pipe.png");
game.load.image("pipeEnd","assets/pipe-end.png");
// make the bonus images available
game.load.image("balloons","assets/balloons.png");
game.load.image("weight","assets/weight.png");
}
// Initialises the game. This function is only called once.
function create() {
// set the background colour of the scene
game.stage.setBackgroundColor("#BADA55");
// add score text
labelScore = game.add.text(20, 60, "0", {font: "30px Arial", fill: "#FFFFFF"});
// initialise the player and associate it with playerImg
player = game.add.sprite(80, 200, "playerImg");
// Setting the player's anchor to center the rotation
player.anchor.setTo(0.5, 0.5);
// Start the ARCADE physics engine.
// ARCADE is the most basic physics engine in Phaser.
game.physics.startSystem(Phaser.Physics.ARCADE);
// enable physics for the player sprite
game.physics.arcade.enable(player);
// set the player's gravity
player.body.gravity.y = gameGravity;
// associate spacebar with jump function
game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).onDown.add(playerJump);
// time loop for game to update
game.time.events.loop(pipeInterval * Phaser.Timer.SECOND, generate);
}
// This function updates the scene. It is called for every new frame.
function update() {
// Call gameOver function when player overlaps with pipe
// (i.e. when player hits a pipe)
game.physics.arcade.overlap(player, pipes, gameOver);
if(0 > player.body.y || player.body.y > width){
gameOver();
}
checkBonus(balloons, -50);
checkBonus(weights, 50);
player.rotation = Math.atan(player.body.velocity.y / gameSpeed);
}
function checkBonus(bonusArray, bonusEffect) {
for(var i=bonusArray.length - 1; i>=0; i--){
game.physics.arcade.overlap(player,bonusArray[i], function(){
// destroy sprite
bonusArray[i].destroy();
// remove element from array
bonusArray.splice(i,1);
// apply the bonus effect
changeGravity(bonusEffect);
});
}
}
// Adds a pipe part to the pipes group
function addPipeBlock(x, y) {
var block = game.add.sprite(x, y, "pipe");
pipes.push(block);
// enable physics engine for pipe
game.physics.arcade.enable(block);
// set the pipe's horizontal velocity to a negative value
// (negative x value for velocity means movement will be towards left)
block.body.velocity.x = -gameSpeed;
}
function addPipeEnd(x, y) {
var block = game.add.sprite(x, y, "pipeEnd");
pipes.push(block);
game.physics.arcade.enable(block);
block.body.velocity.x = - gameSpeed;
}
function generate(){
var diceRoll = game.rnd.integerInRange(1, 10);
if(diceRoll===1){
generateBalloons();
} else if(diceRoll===2){
generateWeight();
} else {
generatePipe();
}
}
// Generate moving pipe
function generatePipe() {
// Generate random integer between 1 and 5. This is the location of the
// start point of the gap.
var gapStart = game.rnd.integerInRange(50, height - 50 - pipeGap);
addPipeEnd(width-5,gapStart - 25);
for(var y=gapStart - 75; y>-50; y -= 50){
addPipeBlock(width,y);
}
addPipeEnd(width-5,gapStart+pipeGap);
for(var y=gapStart + pipeGap + 25; y<height; y += 50){
addPipeBlock(width,y);
}
// Increment the score each time a new pipe is generated.
changeScore();
}
function generateBalloons(){
var bonus = game.add.sprite(width, height, "balloons");
balloons.push(bonus);
game.physics.arcade.enable(bonus);
bonus.body.velocity.x = -gameSpeed;
bonus.body.velocity.y = -game.rnd.integerInRange(60,100);
}
function generateWeight(){
var bonus = game.add.sprite(width, 0, "weight");
weights.push(bonus);
game.physics.arcade.enable(bonus);
bonus.body.velocity.x = -gameSpeed;
bonus.body.velocity.y = game.rnd.integerInRange(60,100);
}
function changeGravity(g){
gameGravity += g;
player.body.gravity.y = gameGravity;
}
function playerJump() {
// the more negative the value the higher it jumps
player.body.velocity.y = - jumpPower;
}
// Function to change the score
function changeScore() {
//increments global score variable by 1
score++;
// updates the score label
labelScore.setText(score.toString());
}
function gameOver() {
// stop the game (update() function no longer called)
score = 0;
gameGravity = 200;
game.state.restart();
}