Skip to content

Instantly share code, notes, and snippets.

@jjculber
Forked from anonymous/pvt.js
Last active December 25, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjculber/7002543 to your computer and use it in GitHub Desktop.
Save jjculber/7002543 to your computer and use it in GitHub Desktop.
//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 = function() {
console.log("hide_prompt()");
$('.startPrompt').hide();
}
this.show_prompt = function() {
console.log("show_prompt()");
$('.startPrompt').show();
}
this.set_timer = function(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 = function() {
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 = function() {
console.log("setup_game()");
this.hud.set_timer(this.total_time);
this.hud.show_prompt();
}
this.start_game = function() {
console.log("start_game()");
this.hud.hide_prompt();
this.start_clock();
}
this.play_game = function() {
space_callback = function(){};
console.log("play_game()");
this.start_game();
var blob = new Blob();
blob.create_new();
}
this.fail_game = function() {
console.log("fail_game()");
alert('Play Nicely: Restarting game.');
//this.play_game();
}
this.send_scores = function() {
alert("send scores");
//Dajaxice.pvt.send_score(Dajax.process,{'response_time': timeDiff.toString()})
}
this.finish_game = function() {
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 = function() {
drawCircle();
}
this.hit_blob = function() {
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 = function() {
this.hit_or_miss[this.current_blob] = false;
this.time_deltas[this.current_blob] = -1;
this.create_new();
}
this.create_new = function() {
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