-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathflappy.html
More file actions
125 lines (110 loc) · 4.2 KB
/
flappy.html
File metadata and controls
125 lines (110 loc) · 4.2 KB
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
/**
* Created by v3513 on 11/08/2015.
*/
// 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 game = new Phaser.Game(790, 400, 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 the empty array.
var pipes = [];
// the interval (in seconds) at which new pipe columns are spawned
var pipeInterval = 1.75;
// Loads all resources for the game and gives them names.
jQuery("#greeting-form").on("submit", function(event_details) {
var greeting = "Hello ";
var name = jQuery("#fullName").val();
var greeting_message = greeting + name;
jQuery("#greeting-form").hide();
jQuery("#greeting").append("<p>" + greeting_message + "</p>");
event_details.preventDefault();
});
function preload() {
// make image file available to game and associate with alias playerImg
game.load.image("playerIMG","../assets/burger2.gif");
// 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("background", "../assets/frenchfriesbackground.jpg");
}
// Initialises the game. This function is only called once.
function create() {
// set the background colour of the scene
game.stage.setBackgroundColor("#F3D3A3");
game.add.sprite(0,0, "background");
// add welcome text
// add score text
labelScore = game.add.text(20, 0, "0",
{font: "30px Arial", fill: "#FFFFFF"});
// initialise the player and associate it with playerImg
var burger = player = game.add.sprite(80, 200, "playerIMG");
burger.scale.setTo(0.05, 0.05);
// 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 = 500;
// 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, generatePipe);
}
// This function updates the scene. It is called for every new frame.
function update() {
// Call gameOver function when player overlaps with any pipe
game.physics.arcade
.overlap(player,
pipes,
gameOver);
}
// Adds a pipe part to the pipes array
function addPipeBlock(x, y) {
// make a new pipe block
var block = game.add.sprite(x,y,"pipe");
// insert it in the pipe array
pipes.push(block);
// enable physics engine for the block
game.physics.arcade.enable(block);
// set the block's horizontal velocity to a negative value
// (negative x value for velocity means movement will be towards left)
block.body.velocity.x = -200;
}
// 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(1, 5);
// Loop 8 times (8 is the height of the canvas).
for (var count = 0; count < 8; count++) {
// If the value of count is not equal to the gap start point
// or end point, add the pipe image.
if(count != gapStart && count != gapStart+1){
addPipeBlock(750, count * 50);
}
}
// Increment the score each time a new pipe is generated.
changeScore();
}
function playerJump() {
// the more negative the value the higher it jumps
player.body.velocity.y = -200;
}
// 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)
game.destroy();
}