Skip to content

Commit 51ce91b

Browse files
committedFeb 20, 2011
changed pig distribution logic, bound and unbound keyboard handlers, upped pig count.
1 parent f8d38ca commit 51ce91b

File tree

1 file changed

+63
-49
lines changed

1 file changed

+63
-49
lines changed
 

‎index.html

+63-49
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,13 @@ <h2>controls:</h2>
221221
<!-- turn off display:none on #report canvas for debugging -->
222222
<canvas id="report" width="500" height="500"></canvas>
223223
<script type="text/javascript">
224-
225-
224+
226225
var c, s, r, northBird, southBird, eastBird, westBird;
227226
var ballRadius = 10;
228227
var intervalId = 0;
229228
var pfWidth = pfHeight = 500;
230229
var firstDraw = true;
231-
var pigCount = 24;
230+
var pigCount = 50;
232231
var pigs = [];
233232
var capturedPigs = [];
234233
var birdCount = 4;
@@ -376,6 +375,7 @@ <h2>controls:</h2>
376375
function gameOver() {
377376

378377
clearInterval(intervalId);
378+
unbindKeyboardHandlers();
379379

380380
c.fillStyle = "rgba(0,0,0, .8)";
381381
c.beginPath();
@@ -478,49 +478,53 @@ <h2>controls:</h2>
478478

479479
}
480480

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+
}
493524

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+
}
524528

525529
function startDirectionRandomizer() {
526530
var num = (Math.floor(Math.random()*5)) + 1;
@@ -556,11 +560,11 @@ <h2>controls:</h2>
556560
for(i=0; i<pigCount; i++) {
557561

558562
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
561565
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
564568
)
565569
);
566570
}
@@ -590,6 +594,13 @@ <h2>controls:</h2>
590594
}
591595
pigs[i].width = pigs[i].pigImg.width;
592596
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+
593604
}
594605

595606
northBird = new Bird( 250, 0, '#ffffff', 'y', 1, 'n', birdImgs.whiteBird, 250, 150 );
@@ -601,6 +612,7 @@ <h2>controls:</h2>
601612

602613
for(i=0; i<birdCount; i++)
603614
{
615+
birds[i].score = 0;
604616
drawCaptureArea(birds[i]);
605617
drawBird(birds[i]);
606618
}
@@ -609,11 +621,13 @@ <h2>controls:</h2>
609621
$('#hhb').unbind('click');
610622
$('#start').unbind('click').text('play').toggle(
611623
function() {
624+
bindKeyboardHandlers();
612625
$(this).text('pause');
613626
intervalId = setInterval(draw, 25);
614627
return intervalId;
615628
},
616629
function() {
630+
unbindKeyboardHandlers();
617631
$(this).text('continue');
618632
clearInterval(intervalId);
619633
}

0 commit comments

Comments
 (0)
Please sign in to comment.