@@ -8,6 +8,10 @@ var game_mode = "prestart";
8
8
var time_game_last_running ;
9
9
var bottom_bar_offset = 0 ;
10
10
var pipes = [ ] ;
11
+ var currentScore = 0 ;
12
+ var bestScore = 0 ;
13
+
14
+ syncScore ( ) ;
11
15
12
16
function MySprite ( img_url ) {
13
17
this . x = 0 ;
@@ -125,14 +129,16 @@ function display_intro_instructions() {
125
129
) ;
126
130
}
127
131
function display_game_over ( ) {
128
- var score = 0 ;
129
- for ( var i = 0 ; i < pipes . length ; i ++ )
130
- if ( pipes [ i ] . x < bird . x ) score = score + 0.5 ;
132
+ currentScore = scoreFromPipes ( ) ;
133
+ if ( currentScore > bestScore ) {
134
+ bestScore = currentScore ;
135
+ syncScore ( ) ;
136
+ }
131
137
ctx . font = "30px Arial" ;
132
138
ctx . fillStyle = "red" ;
133
139
ctx . textAlign = "center" ;
134
140
ctx . fillText ( "Game Over" , myCanvas . width / 2 , 100 ) ;
135
- ctx . fillText ( "Score: " + score , myCanvas . width / 2 , 150 ) ;
141
+ ctx . fillText ( "Score: " + currentScore , myCanvas . width / 2 , 150 ) ;
136
142
ctx . font = "20px Arial" ;
137
143
ctx . fillText ( "Click, touch, or press to play again" , myCanvas . width / 2 , 300 ) ;
138
144
}
@@ -149,6 +155,7 @@ function reset_game() {
149
155
bird . angle = 0 ;
150
156
pipes = [ ] ; // erase all the pipes from the array
151
157
add_all_my_pipes ( ) ; // and load them back in their starting positions
158
+ currentScore = 0 ;
152
159
}
153
160
function add_all_my_pipes ( ) {
154
161
add_pipe ( 500 , 100 , 140 ) ;
@@ -188,6 +195,11 @@ function Do_a_Frame() {
188
195
make_bird_tilt_appropriately ( ) ;
189
196
make_bird_slow_and_fall ( ) ;
190
197
check_for_end_game ( ) ;
198
+ currentScore = scoreFromPipes ( ) ;
199
+ if ( currentScore > bestScore ) {
200
+ bestScore = currentScore ;
201
+ syncScore ( ) ;
202
+ }
191
203
break ;
192
204
}
193
205
case "over" : {
@@ -205,3 +217,21 @@ bird.x = myCanvas.width / 3;
205
217
bird . y = myCanvas . height / 2 ;
206
218
207
219
setInterval ( Do_a_Frame , 1000 / FPS ) ;
220
+ function scoreFromPipes ( ) {
221
+ var score = 0 ;
222
+ for ( var i = 0 ; i < pipes . length ; i ++ ) {
223
+ if ( pipes [ i ] . x < bird . x ) score = score + 0.5 ;
224
+ }
225
+ return Math . floor ( score ) ;
226
+ }
227
+
228
+ function syncScore ( ) {
229
+ if ( window . GameHub ) {
230
+ if ( typeof window . GameHub . setStageScore === 'function' ) {
231
+ window . GameHub . setStageScore ( bestScore ) ;
232
+ }
233
+ if ( typeof window . GameHub . recordScore === 'function' ) {
234
+ window . GameHub . recordScore ( 'flappy' , bestScore , 'best' ) ;
235
+ }
236
+ }
237
+ }
0 commit comments