-
-
Save jjculber/7002542 to your computer and use it in GitHub Desktop.
This file contains 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
//Global Functions & Set Up | |
function drawCircle() { | |
var c=document.getElementById("c"); | |
var ctx=c.getContext("2d"); | |
ctx.beginPath(); | |
ctx.arc(250,150,50,0,2*Math.PI); | |
ctx.fillStyle = "#F00"; | |
ctx.fill(); | |
ctx.strokeStyle = "#F00"; | |
ctx.stroke(); | |
} | |
var space_callback = function(){}; | |
//timer for 1 second pass if no key press then failure | |
function Hud() { | |
this.hide_prompt = hide_prompt; | |
function hide_prompt() { | |
console.log("hide_prompt()"); | |
$('.startPrompt').hide(); | |
} | |
this.show_prompt = show_prompt; | |
function show_prompt() { | |
console.log("show_prompt()"); | |
$('.startPrompt').show(); | |
} | |
this.set_timer = set_timer; | |
function set_timer(time) { | |
console.log("set_timer()"); | |
var seconds = time % 60; | |
seconds = (seconds < 10) ? '0' + seconds : seconds; | |
var timeText = ~~(time/60) + ':' + seconds; | |
$('.timer').text(timeText); | |
} | |
} | |
function Game(hud, time) { | |
this.hud = hud; | |
this.total_time = time; | |
this.current_time = time; | |
this.clock = undefined; | |
this.reaction_time = []; //new Array(); | |
this.start_clock = start_clock; | |
function start_clock() { | |
console.log("start_clock()"); | |
this.clock = setInterval(function() { | |
this.current_time = this.current_time - 1; //-1 second | |
if (this.current_time === 0) { | |
this.finish_game(); | |
} | |
this.hud.set_timer(this.current_time); | |
}, 1000); | |
} | |
this.setup_game = setup_game; | |
function setup_game() { | |
console.log("setup_game()"); | |
this.hud.set_timer(this.total_time); | |
this.hud.show_prompt(); | |
} | |
this.start_game = start_game; | |
function start_game() { | |
console.log("start_game()"); | |
this.hud.hide_prompt(); | |
this.start_clock(); | |
} | |
this.play_game = play_game; | |
function play_game() { | |
space_callback = function(){}; | |
console.log("play_game()"); | |
this.start_game(); | |
var blob = new Blob(); | |
blob.create_new(); | |
} | |
this.fail_game = fail_game; | |
function fail_game() { | |
console.log("fail_game()"); | |
alert('Play Nicely: Restarting game.'); | |
//this.play_game(); | |
} | |
this.send_scores = send_scores; | |
function send_scores() { | |
alert("send scores"); | |
//Dajaxice.pvt.send_score(Dajax.process,{'response_time': timeDiff.toString()}) | |
} | |
this.finish_game = finish_game; | |
function finish_game() { | |
space_callback = function(){}; | |
this.send_scores(); | |
} | |
} | |
function Blob() { | |
this.display_timer = undefined; | |
this.current_blob = 0; | |
this.hit_or_miss = []; //new Array(); //True is Hit; False is Miss | |
this.time_deltas = []; //new Array(); | |
this.display_time = undefined; | |
this.hit_time = undefined; | |
this.display_blob = display_blob; | |
function display_blob() { | |
drawCircle(); | |
} | |
this.hit_blob = hit_blob; | |
function hit_blob() { | |
this.hit_time = new Date().getTime(); | |
var time_diff = this.hit_time - this.display_time; | |
this.hit_or_miss[this.current_blob] = true; | |
this.time_deltas[this.current_blob] = time_diff; | |
this.create_new(); | |
} | |
this.miss_blob = miss_blob; | |
function miss_blob() { | |
this.hit_or_miss[this.current_blob] = false; | |
this.time_deltas[this.current_blob] = -1; | |
this.create_new(); | |
} | |
this.create_new = create_new; | |
function create_new() { | |
var timer_next = Math.floor((Math.random() * 20) + 1); | |
this.timer = setTimeout(function() { | |
this.display_blob(); | |
this.display_time = new Date().getTime(); | |
space_callback = this.miss_blob; | |
}, timer_next * 1000); | |
} | |
} | |
$(function() { | |
//key press function calls | |
$(window).keypress(function(e) { | |
console.log("keypress"); | |
var code = e.keyCode || e.which; | |
if (code == 32) { | |
space_callback(); | |
} | |
e.preventDefault(); | |
}); | |
var hud = new Hud(); | |
var game = new Game(hud, 180); | |
space_callback = game.play_game; | |
//Clear Timers | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment