@@ -221,14 +221,13 @@ <h2>controls:</h2>
221
221
<!-- turn off display:none on #report canvas for debugging -->
222
222
< canvas id ="report " width ="500 " height ="500 "> </ canvas >
223
223
< script type ="text/javascript ">
224
-
225
-
224
+
226
225
var c , s , r , northBird , southBird , eastBird , westBird ;
227
226
var ballRadius = 10 ;
228
227
var intervalId = 0 ;
229
228
var pfWidth = pfHeight = 500 ;
230
229
var firstDraw = true ;
231
- var pigCount = 24 ;
230
+ var pigCount = 50 ;
232
231
var pigs = [ ] ;
233
232
var capturedPigs = [ ] ;
234
233
var birdCount = 4 ;
@@ -376,6 +375,7 @@ <h2>controls:</h2>
376
375
function gameOver ( ) {
377
376
378
377
clearInterval ( intervalId ) ;
378
+ unbindKeyboardHandlers ( ) ;
379
379
380
380
c . fillStyle = "rgba(0,0,0, .8)" ;
381
381
c . beginPath ( ) ;
@@ -478,49 +478,53 @@ <h2>controls:</h2>
478
478
479
479
}
480
480
481
- $ ( window ) . keypress ( function ( e ) {
482
- if ( e . keyCode == 84 || e . keyCode == 66 || e . keyCode == 76 || e . keyCode == 65 ) {
483
- // typeaheadfind driving me nuts, sorry about any other key commands this screws up, Firefox users...
484
- e . preventDefault ( ) ;
485
- e . stopPropagation ( ) ;
486
- e . stopImmediatePropagation ( ) ;
487
- return false ;
488
- }
489
- else {
490
- return true ;
491
- }
492
- } ) ;
481
+ function bindKeyboardHandlers ( ) {
482
+ $ ( document ) . keypress ( function ( e ) {
483
+ if ( e . keyCode == 84 || e . keyCode == 66 || e . keyCode == 76 || e . keyCode == 65 ) {
484
+ // typeaheadfind driving me nuts, sorry about any other key commands this screws up, Firefox users...
485
+ e . preventDefault ( ) ;
486
+ e . stopPropagation ( ) ;
487
+ e . stopImmediatePropagation ( ) ;
488
+ return false ;
489
+ }
490
+ else {
491
+ return true ;
492
+ }
493
+ } ) . keydown ( function ( e ) {
494
+ switch ( e . keyCode ) {
495
+ case 84 : // T
496
+ e . preventDefault ( ) ;
497
+ if ( ! northBird . launched ) {
498
+ northBird . launch ( ) ;
499
+ }
500
+ break ;
501
+ case 66 : // B
502
+ e . preventDefault ( ) ;
503
+ if ( ! southBird . launched ) {
504
+ southBird . launch ( ) ;
505
+ }
506
+ break ;
507
+ case 76 : // L
508
+ e . preventDefault ( ) ;
509
+ if ( ! eastBird . launched ) {
510
+ eastBird . launch ( ) ;
511
+ }
512
+ break ;
513
+ case 65 : // A
514
+ e . preventDefault ( ) ;
515
+ if ( ! westBird . launched ) {
516
+ westBird . launch ( ) ;
517
+ }
518
+ break ;
519
+ default :
520
+ break ;
521
+ }
522
+ } ) ;
523
+ }
493
524
494
- $ ( document ) . keydown ( function ( e ) {
495
- switch ( e . keyCode ) {
496
- case 84 : // T
497
- e . preventDefault ( ) ;
498
- if ( ! northBird . launched ) {
499
- northBird . launch ( ) ;
500
- }
501
- break ;
502
- case 66 : // B
503
- e . preventDefault ( ) ;
504
- if ( ! southBird . launched ) {
505
- southBird . launch ( ) ;
506
- }
507
- break ;
508
- case 76 : // L
509
- e . preventDefault ( ) ;
510
- if ( ! eastBird . launched ) {
511
- eastBird . launch ( ) ;
512
- }
513
- break ;
514
- case 65 : // A
515
- e . preventDefault ( ) ;
516
- if ( ! westBird . launched ) {
517
- westBird . launch ( ) ;
518
- }
519
- break ;
520
- default :
521
- break ;
522
- }
523
- } ) ;
525
+ function unbindKeyboardHandlers ( ) {
526
+ $ ( document ) . unbind ( 'keypress keydown' ) ;
527
+ }
524
528
525
529
function startDirectionRandomizer ( ) {
526
530
var num = ( Math . floor ( Math . random ( ) * 5 ) ) + 1 ;
@@ -556,11 +560,11 @@ <h2>controls:</h2>
556
560
for ( i = 0 ; i < pigCount ; i ++ ) {
557
561
558
562
pigs . push ( new Pig (
559
- Math . floor ( Math . random ( ) * 50 ) + 225 , // start X
560
- Math . floor ( Math . random ( ) * 50 ) + 225 , // start Y
563
+ Math . floor ( Math . random ( ) * 200 ) + 150 , // start X
564
+ Math . floor ( Math . random ( ) * 200 ) + 150 , // start Y
561
565
Math . floor ( Math . random ( ) * 6 ) + 1 , // imgNum
562
- ( Math . floor ( Math . random ( ) * 5 ) + 5 ) * startDirectionRandomizer ( ) , // xVel
563
- ( Math . floor ( Math . random ( ) * 5 ) + 5 ) * startDirectionRandomizer ( ) // yVel
566
+ ( Math . ceil ( Math . random ( ) * 15 ) ) * startDirectionRandomizer ( ) , // xVel
567
+ ( Math . ceil ( Math . random ( ) * 15 ) ) * startDirectionRandomizer ( ) // yVel
564
568
)
565
569
) ;
566
570
}
@@ -590,6 +594,13 @@ <h2>controls:</h2>
590
594
}
591
595
pigs [ i ] . width = pigs [ i ] . pigImg . width ;
592
596
pigs [ i ] . height = pigs [ i ] . pigImg . height ;
597
+
598
+ //make sure there aren't any pigs that are too slow
599
+ if ( pigs [ i ] . xVel < 2 && pigs [ i ] . yVel < 2 ) {
600
+ var prop = ( startDirectionRandomizer ( ) == 1 ) ? pigs [ i ] . xVel : pigs [ i ] . yVel ;
601
+ prop = Math . floor ( Math . random ( ) * 5 ) + 5 ;
602
+ }
603
+
593
604
}
594
605
595
606
northBird = new Bird ( 250 , 0 , '#ffffff' , 'y' , 1 , 'n' , birdImgs . whiteBird , 250 , 150 ) ;
@@ -601,6 +612,7 @@ <h2>controls:</h2>
601
612
602
613
for ( i = 0 ; i < birdCount ; i ++ )
603
614
{
615
+ birds [ i ] . score = 0 ;
604
616
drawCaptureArea ( birds [ i ] ) ;
605
617
drawBird ( birds [ i ] ) ;
606
618
}
@@ -609,11 +621,13 @@ <h2>controls:</h2>
609
621
$ ( '#hhb' ) . unbind ( 'click' ) ;
610
622
$ ( '#start' ) . unbind ( 'click' ) . text ( 'play' ) . toggle (
611
623
function ( ) {
624
+ bindKeyboardHandlers ( ) ;
612
625
$ ( this ) . text ( 'pause' ) ;
613
626
intervalId = setInterval ( draw , 25 ) ;
614
627
return intervalId ;
615
628
} ,
616
629
function ( ) {
630
+ unbindKeyboardHandlers ( ) ;
617
631
$ ( this ) . text ( 'continue' ) ;
618
632
clearInterval ( intervalId ) ;
619
633
}
0 commit comments