Skip to content

Commit

Permalink
added score
Browse files Browse the repository at this point in the history
  • Loading branch information
master-lincoln committed Feb 15, 2014
1 parent a9bf6d0 commit 4e0af3b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
34 changes: 25 additions & 9 deletions src/game.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(['bird', 'pipe'], function(bird, Pipe) {
define(['bird', 'pipe', 'score'], function(bird, Pipe, score) {
"use strict";

var offsetX = 0;
Expand Down Expand Up @@ -53,6 +53,7 @@ define(['bird', 'pipe'], function(bird, Pipe) {
var promImg = new Promise(preloadImage);
var promMap = new Promise(preloadMap);

score.init(game);
bird.init(game);
pipes.push(new Pipe(game));
pipes.push(new Pipe(game));
Expand All @@ -65,7 +66,7 @@ define(['bird', 'pipe'], function(bird, Pipe) {
var lastPipe = pipes[pipes.length-1];
// add new pipe at the end
if (lastPipe &&
lastPipe.getX()+offsetX < game.SIZE[0]) {
lastPipe.getX()-offsetX < game.SIZE[0]) {
pipes.push(new Pipe(game));
}
// remove first pipe if offscreen
Expand All @@ -75,13 +76,7 @@ define(['bird', 'pipe'], function(bird, Pipe) {
}
}

function tick() {
var moveX = game.tickTime/16;
offsetX += moveX;
bird.tick(moveX);

addAndRemovePipes();

function checkCollisions() {
var collision = false;
pipes.forEach(function(pipe, i) {
pipe.getBoundingBoxes().forEach(function(bb) {
Expand All @@ -95,12 +90,32 @@ define(['bird', 'pipe'], function(bird, Pipe) {
h: game.SIZE[1]-groundLevel
};
if ( bird.collidesWith(bbG) || collision ) {
score.resetPoints();
bird.die();
}

bird.drawBB(collision);
}

function tick() {
var moveX = game.tickTime/16;
offsetX += moveX;
bird.tick(moveX);

addAndRemovePipes();
checkCollisions();

pipes.forEach(function(pipe) {
var pipeRightOuter = pipe.getX()+pipe.getBoundingBoxes()[0].w;
if ( !pipe.passed &&
pipeRightOuter <bird.getPosition().x) {
pipe.passed = true;
score.addPoint();
}
});

}

function drawBG(ctx) {
drawImage("bg", 0, 0, ctx);
drawImage("bg", 288, 0, ctx);
Expand Down Expand Up @@ -129,6 +144,7 @@ define(['bird', 'pipe'], function(bird, Pipe) {
bird.draw(ctx);

ctx.restore();
score.draw(ctx);
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ define([], function() {
this.game = game;
this.height = 270;
this.widths = 52;

this.passed = false;
var newX = lastX + pipeDistance;
lastX = newX;
this.position = {x:newX, y:Math.random()*250+150};
Expand Down
43 changes: 43 additions & 0 deletions src/score.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
define([], function() {
"use strict";

var points = 0;
var maxPoints = 0;

var game;

function init(theGame) {
game = theGame;
}

function draw(ctx) {
var pos = {x:20, y:20};

var strPoints = points.toString();
strPoints.split('').forEach(function(c,i) {
game.drawImage("number_score_0"+c, pos.x+i*15, pos.y, ctx);
});

var strMaxPoints = maxPoints.toString();
strMaxPoints.split('').forEach(function(c,i) {
game.drawImage("number_context_0"+c, pos.x+5+i*13, pos.y+25, ctx);
});
}

function resetPoints() {
points = 0;
}

function addPoint() {
points += 1;
maxPoints = Math.max(points, maxPoints);
}

return {
init: init,
draw: draw,
resetPoints: resetPoints,
addPoint: addPoint
};

});

0 comments on commit 4e0af3b

Please sign in to comment.