xxxxxxxxxx
60
// Project inspired by Luke Sawczak's "Code Monkey"
// For his original version go here: https://sawczak.com
let generating = true;
let chosenText;
let inp;
function randomIndex(){
const randy = Math.floor(Math.random()*options.length);
return randy;
}
function myInputEvent() {
let typed = this.value();
let numChars = typed.length;
if(typed === chosenText){
background(0,200,0);
} else if(typed !== chosenText.substring(0,numChars)){
background(200,0,0);
} else {
background(0);
}
}
function setup() {
noLoop();
createCanvas(400, 400);
background(0);
// Input
inp = createInput('');
inp.position(30, 250);
inp.size(300);
inp.input(myInputEvent);
// Button
button = createButton('New line');
button.position(30, 320);
button.size(335);
button.mousePressed(newLine);
}
function newLine(){
inp.value('');
background(0);
generating = true;
}
function draw() {
textSize(20);
fill(255);
if(generating){
chosenText = options[randomIndex()];
text(chosenText,50,100);
generating = false;
loop();
}
text(chosenText,50,100);
}