Skip to content

Instantly share code, notes, and snippets.

@akirchmyer
Created November 17, 2013 04:02
Show Gist options
  • Select an option

  • Save akirchmyer/7508960 to your computer and use it in GitHub Desktop.

Select an option

Save akirchmyer/7508960 to your computer and use it in GitHub Desktop.
A Pen by Andrew Kirchmyer.
<div id="results">
</div>
function Player(name) {
this.points = 0;
this.name = name;
}
Player.prototype.play = function() {
this.points += 1;
mediator.played();
};
var scoreboard = {
// HTML element to be updated
element: document.getElementById('results'),
//update the score display
update: function(score) {
var i, msg = "";
for (i in score) {
if (score.hasOwnProperty(i)) {
msg += '<p><strong>'+ i +'</strong>:';
msg += score[i];
msg += '</p>';
}
}
this.element.innerHTML = msg;
}
}
var mediator = {
players: {},
// initialization
setup: function() {
var players = this.players;
players.home = new Player('home');
players.guest = new Player('guest');
},
played: function() {
var players = this.players,
score = {
Home: players.home.points,
Guest: players.guest.points
};
scoreboard.update(score);
},
// handle user interactions
keypress: function(e) {
if (e.which === 49) { // key "1"
mediator.players.guest.play();
return;
}
if (e.which === 48) { // key "0"
mediator.players.home.play();
return;
}
}
}
mediator.setup();
window.onkeypress = mediator.keypress;
// game over in 30 seconds
setTimeout(function() {
window.onkeypress = null;
alert('Game over!');
}, 30000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment