forked from cambridgecoding/flappybird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutoringChallengeTwoSnippet.txt
163 lines (121 loc) · 3.75 KB
/
tutoringChallengeTwoSnippet.txt
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
// the Game object used by the phaser.io library
var stateActions = { preload: preload, create: create, update: update };
// Phaser parameters:
// - game width
// - game height
// - renderer (go for Phaser.AUTO)
// - element where the game will be drawn ('game')
// - actions on the game state (or null for nothing)
var game = new Phaser.Game(790, 400, Phaser.AUTO, 'game', stateActions);
var scoreP1 = 0;
var scoreP2 = 0;
var label_scoreP1;
var label_scoreP2;
var player;
var pipes;
var pipeInterval = 3;
/*
* Loads all resources for the game and gives them names.
*/
function preload() {
game.load.image("imgPlayer", "assets/flappy.png");
game.load.image("mouseCursorImg", "assets/cursor.png");
game.load.audio("spaceOgg", "assets/point.ogg");
game.load.image("pipe", "assets/pipe.png");
game.load.audio("jump", "assets/jump.ogg");
game.load.audio("crash", "assets/fall-crash.ogg");
game.load.image("imgPlayer2", "assets/player-2.png");
}
/*
* Initialises the game. This function is only called once.
*/
function create() {
// set the background colour of the scene (with hexadecimal)
game.stage.setBackgroundColor("#E7FF00");
game.physics.startSystem(Phaser.Physics.ARCADE);
player = game.add.sprite(395, 200, "imgPlayer");
player2 = game.add.sprite(395, 230, "imgPlayer2");
game.physics.arcade.enable(player);
game.physics.arcade.enable(player2);
game.time.events.loop(pipeInterval * Phaser.Timer.SECOND, createPipe);
player.body.gravity.y = 300;
player2.body.gravity.y = 300;
game.input.onDown.add(p2Jump);
game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).onDown.add(p1Jump);
label_scoreP1 = game.add.text(700, 50, "0");
label_scoreP2 = game.add.text(90, 50, "0");
pipes = game.add.group();
createPipe();
if (player.velocity.y === 100 && player2.velocity.y === 100) {
pipes.velocity.x = 0;
}
}
/*
* This function updates the scene. It is called for every new frame.
*/
function update() {
game.physics.arcade.overlap(player, pipes, game_overP1);
game.physics.arcade.overlap(player2, pipes, game_overP2);
}
function p1Jump(event) {
if (player.velocity.y != 100) {
player.velocity.y = -165;
}
game.sound.play("jump");
}
function p2Jump(event) {
if (player2.velocity.y != 100) {
player2.velocity.y = -165;
}
game.sound.play("jump");
}
function changeScoreP1() {
scoreP1 = scoreP1 + 1;
label_scoreP1.setText(scoreP1.toString());
game.sound.play("spaceOgg");
}
function changeScoreP2() {
scoreP2 = scoreP2 + 1;
label_scoreP2.setText(scoreP2.toString());
game.sound.play("spaceOgg");
}
function addPipeBlock(x, y) {
var block = pipes.create(x, y, "pipe");
game.physics.arcade.enable(block);
block.velocity.x = -200;
}
function createPipe() {
var randNum = game.rnd.integerInRange(1, 7);
var chance = game.rnd.integerInRange(1, 2);
for (var count = 0; count < 8; count++) {
if (chance === 1) {
if (count != randNum) {
addPipeBlock(780, count * 50);
}
} else {
if (randNum != 7) {
if (count != randNum && count != randNum + 1) {
addPipeBlock(780, count * 50);
}
} else {
if (count != randNum && count != randNum - 1) {
addPipeBlock(780, count * 50);
}
}
}
}
if (player.velocity.y != 100) {
changeScoreP1();
}
if (player2.velocity.y != 100) {
changeScoreP2();
}
}
function game_overP1() {
player.velocity.y = 100;
game.sound.play("crash");
}
function game_overP2() {
player2.velocity.y = 100;
game.sound.play("crash");
}