Skip to content

Instantly share code, notes, and snippets.

@valdiney
Last active December 17, 2015 13: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 valdiney/5620744 to your computer and use it in GitHub Desktop.
Save valdiney/5620744 to your computer and use it in GitHub Desktop.
MathGame Simples game de matemática. Beta Em funcionamento neste Indereço: http://codepen.io/valdiney/pen/iqepv
window.onload = function(){
var vetor_acertos = [0];
var vetor_erros = [0];
////////////////////////////////////
// LIMPA O VETOR APÓS FIM DO JOGO
function limpa_vetores(){
var acertos = document.getElementById('acertos');
var erros = document.getElementById('erros');
acertos.innerHTML = "Corretos: 0";
erros.innerHTML = "Erros: 0";
vetor_acertos[0]= null;
vetor_erros[0]= null;
}
var button_computar = document.getElementById('computar');
function apresenta_buttonComputar(n){
return button_computar .style.display = n;
}
var button_start = document.getElementById('start');
function oculta_buttonStart(n){
return button_start.style.display = n;
}
function tela_fimdeGame(n){
return document.getElementById('fim_game').style.display = n;
}
function gangouOuPerdeu(){
if(vetor_acertos[0] > vetor_erros[0]){
document.getElementById('sim_nao').innerHTML = " Acertou mais do que errou! Meus parabéns.";
}else{
document.getElementById('sim_nao').innerHTML = " Errou mais do que acertou! Tente novamente.";
}
}
///////////////////////////
// DESABILITANDO A TELA FIM DE GAME
var close_fimgame = document.getElementById('close_fimgame').onclick = function(){
tela_fimdeGame('none');
}
///////////////////////////////////////
// TEMPO PARA O DESAFIO
function tempo_correndo(){
var correr_tempo = [0];
var correndo_tempo = window.setInterval(correndo,1000);
function correndo(){
correr_tempo[0]+=1;
document.getElementById('tempo').innerHTML = "Tempo Para completar a prova: "+"<span>"+correr_tempo[0]+"</span>";
if(correr_tempo[0] == 60){
//alert("Tempo esgotado");
clearInterval(correndo_tempo);
oculta_buttonStart("block");
apresenta_buttonComputar("none");
document.getElementById('label').innerHTML ='';
tela_fimdeGame("block");
gangouOuPerdeu();
}
}
}
var start_contar = document.getElementById('start').onclick = function(){
document.getElementById('campo').focus();
oculta_buttonStart("none");
apresenta_buttonComputar("block");
tempo_correndo();
computar();
limpa_vetores();
tela_fimdeGame("none");
}
/////////////////////////////////////////
// CONFIRMA A RESPOSTA
var computar = document.getElementById('computar').onclick = function(){
var campo = document.getElementById('campo').value;
var label = document.getElementById('label');
var acertos = document.getElementById('acertos');
var erros = document.getElementById('erros');
////////////////////////////////////////
// GERANDO OS NÚMEROS RANDÔMICOS
function gerandoNumeros(){
var gerador = setTimeout(gerando_numeros,0000);
function gerando_numeros(){
numero1 = Math.floor(Math.random()*20+1);
numero2 = Math.floor(Math.random()*10+1);
operandos = numero1+" x "+numero2;
produto = numero1*numero2;
this.label.innerHTML = operandos;
}//end temporizador
}
gerandoNumeros();
//////////////////////////////////////
// ACERTOS E ERROS
function acertosEerros(){
if(campo == produto){
vetor_acertos[0]+=1
acertos.innerHTML = "Corretos: "+vetor_acertos[0];
campo = document.getElementById('campo').value='';
campo = document.getElementById('campo').focus();
}else{
vetor_erros[0]+=1
erros.innerHTML = "Errados: "+vetor_erros[0];
campo = document.getElementById('campo').value='';
campo = document.getElementById('campo').focus();
}
}//end function
acertosEerros();
}//end computar
}//end window
<!doctype html>
<html lang="pt">
<head>
<link rel="stylesheet" href="css/style.css"/>
<title>Titulo do Documento</title>
</head>
<body>
<div id="whapper">
<div id="fim_game">
<button type="button" id="close_fimgame">X</button>
<span id="acertos" class="yes">Corretos:</span>
<span id="erros" class="yes">Errados:</span>
<span id="sim_nao" class="yes">Corretos:</span>
</div>
<h3 id="tempo">Tempo Para completar a prova: 60 segundos...</h3>
<p>
Você tem 60 segundos para responder 30 perguntas relacionadas a multiplicação de valores. Tente acertar mais do que errar!
</p>
<table>
<tr>
<td><label id="label"></label></td>
<td><input type="text" id="campo"></input></td>
</tr>
</table>
<table>
<tr>
<td><button type="button" id="computar">Confirmar</button></td>
<td><button type="button" id="start">Começar o jogo</button></td>
</tr>
</table>
</div><!--end whapper-->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment