A Pen by Andrew Kirchmyer on CodePen.
Created
November 17, 2013 04:02
-
-
Save akirchmyer/7508960 to your computer and use it in GitHub Desktop.
A Pen by Andrew Kirchmyer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="results"> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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