xxxxxxxxxx
312
var myVoice; // new P5.Speech object
var sum; // The answer we are looking for
var maxsum=20; // Used to adjust maximum sum answer
var ans=[]; // Array of answers
var score=0; // Score
var gameover=false; // Gameover Flag
var correct=false; // Used to display "Great Job1"
var incorrect=false; // Used to diplay "Try again".
var lives=3; // Number of incorrect guesses.
var add1; // First addition
var add2; // Second Addition
var xarr=[]; // Array of xvalues for buttons
var yarr=[]; // array of yvalues for buttons
var buttonsize=50; // Size of buttons
var availans=[]; // Array used to prevent duplicate answers :O
var cols=['red', 'pink', 'orange', 'yellow', 'green', 'blue', 'purple'];
var blockcolor='green';
var showcount=0; // Used to show feedback.
var unitsize=20;
var msg=0;
var dly=10; // Delay in speaking
var agn=0;
var input;
var great=["Great Job!", "Way to go!", "Fantastic", "WOW", "Amazing"];
var again=["TRY AGAIN!", "NOT QUITE!", "OH NO"];
function setup() {
frameRate(10);
createCanvas(400, 400);
input=createButton("Activate Sound");
input.mousePressed(actSound);
myVoice = new p5.Speech()
myVoice.interrupt=false;
myVoice.setVolume(20);
myVoice.setPitch(120/100);
myVoice.setRate(75/100);
for(var i=0; i<4; i++)
{
xarr.push(125+(i*55));
yarr.push(height- 50);
}
for(var i=0; i<maxsum; i++)
{
availans.push(i+1);
}
createSum();
}
function draw() {
background('skyblue');
showProblem();
if(correct)
{
if(showcount<=5)
{
stroke(0);
fill(0);
textAlign(LEFT)
textSize(30);
text(great[msg], 10,50)
showcount++;
}
else
{
showcount=0;
correct=false;
msg=floor(random (great.length));
}
}
if(incorrect)
{
if(showcount<5)
{
stroke(0);
fill(0);
textAlign(LEFT);
textSize(30);
text(again[agn], 10,50)
showcount++;
}
else
{
showcount=0;
incorrect=false;
agn=floor(random(again.length));
}
}
}
function createSum()
{
//myVoice.cancel();
ans=[0,0,0,0];
sum = floor(random(maxsum)+1)
if(sum>10)
{
add1=10;
}
else
{
add1 = floor(random(sum+1))
}
add2 = sum - add1;
//ans.push(sum);
ans[0]=sum;
availans.splice(sum-1, 1);
//console.log("HERE");
shuffle(availans, true);
for(var i=0; i<3; i++)
{
ans[i+1]=availans[i];
//ans[i+1]=floor(random(maxsum)+1);
}
//console.log(ad1 + "+" + ad2 + "=" + sum)
myVoice.speak(add1 + "PLUS" + add2 + "equals");
mixAnswers();
}
function mixAnswers()
{
var temp=0;
//console.log("HERE");
for(var i=0; i<4; i++)
{
temp = ans[i];
rnd = floor(random(4));
ans[i]=ans[rnd];
ans[rnd]=temp;
//noLoop();
}
}
function showProblem()
{
fill(0);
line(0, height-(height/3), width, height-(height/3));
textSize(30);
textAlign(CENTER);
text(add1 + "+" + add2 + "= ?", (width/2), height-(height/3) + 30);
fill(255);
for(var i=0; i<ans.length; i++)
{
fill(255);
circle(xarr[i], yarr[i], buttonsize);
fill(0);
text(ans[i], xarr[i], yarr[i]+7.5);
}
// Attempt to show "tens pieces"
var space=0;
stroke(0);
for(var i=0; i<add1; i++)
{
fill(blockcolor);
if(add1!=10)
{
space=5;
}
rect(width/4, (230-(i*(unitsize+space))), unitsize);
}
if(add1==0)
{
fill(0);
textSize(50);
text("0",width/4, 200)
}
fill(0);
textSize(50);
text("+", width/2, 200)
var space=0;
stroke(0);
for(var i=0; i<add2; i++)
{
fill(blockcolor);
if(add2!=10)
{
space=5;
}
rect((width/4)*3, (230-(i*(unitsize+space))), unitsize);
}
if(add2==0)
{
fill(0);
textSize(50);
text("0",(width/4)*3, 200)
}
//line(width/3, (200-(add1*unitsize)), width/3, 200);
//line(0,0, width, height);
}
function mousePressed()
//function touchStarted()
{
userStartAudio();
// Check for clicked button
if(showcount==0)
{
for(var i=ans.length-1; i>=0; i--)
{
if(dist(mouseX, mouseY, xarr[i], yarr[i])<buttonsize/2)
{
if(ans[i]==sum)
{
//console.log("GREAT JOB");
//myVoice.speak("GREAT");
dly=0;
correct=true;
resetAvail();
blockcolor=cols[floor(random(cols.length))];
console.log(myVoice.pending);
myVoice.speak(great[msg]);
// myVoice.speak("TEST");
//myVoice.speak("TEST2");
createSum();
}
else
{
//console.log("TRY AGAIN");
myVoice.speak(again[agn]);
incorrect=true;
ans.splice(i, 1);
}
}
}
}
}
function resetAvail()
{
xarr.length=0;
yarr.length=0;
availans.length=0;
for(var i=0; i<4; i++)
{
xarr.push(125+(i*55));
yarr.push(height- 50);
}
for(var i=0; i<maxsum; i++)
{
availans.push(i+1);
}
}
function wait(delay)
{
var startTime=millis();
do{
//console.log("WAIT");
}while((millis() - startTime)<=delay*1000)
}
function actSound()
{
userStartAudio();
myVoice.speak("Active");
input.hide();
}