xxxxxxxxxx
let randWord = '', typing = '', cursor = '_';
let cursTime = 0, typTime = 0, maxTime = 450, timeLeft = maxTime, playing = 0, meh = 0, randVal = 0, right = 0;
let words = [];
function preload() {
words = loadStrings('words.txt');
}
function setup() {
createCanvas(500, 500);
textSize(32);
randWord = words[round(random(0, words.length - 1))];
//print(words.length);
//print(words);
randVal = round(random(0, 150));
}
function draw() {
background(randVal);
noStroke();
fill(255 - randVal + (randVal/2));
if(playing == 1){
if(millis() >= 500 + cursTime)
{
cursor = '_';
cursTime = millis();
} else {
cursor = '';
}
if(millis() >= 1000 + typTime)
{
if(timeLeft > 0) {
timeLeft -= 45;
}
typTime = millis();
}
if(timeLeft <= 0){
playing = 2;
}
backSpace();
} else if(playing == 0){
rect(175, 300, 50, 150);
rect(275, 300, 50, 150);
} else if(playing == 2){
text('GAME OVER', 25, 400);
right = 0;
maxTime = 450;
typing = '';
//randWord = words[round(random(0, words.length - 1))];
} else {
playing = 0;
}
text('Type this word: ' + randWord, 25, 100, 450, 185);
text('> ' + typing + cursor, 25, 190, 450, 500);
rect(25, 25, timeLeft, 50);
}
function keyTyped() {
if(playing == 1) {
if(key != ' ' && key != 'Enter') {
typing += String(key);
}
}
}
function keyPressed() {
if(playing == 1){
if(key == 'Enter') {
if(typing == randWord){
randWord = words[round(random(0, words.length - 1))];
maxTime -= 10;
timeLeft = maxTime;
right++;
} else {
timeLeft -= 5;
}
typing = '';
//print(maxTime);
}
}
if(key == ' '){
if(playing == 1){
playing = 0;
} else if(playing == 2){
playing = 1;
timeLeft = maxTime;
} else {
playing = 1;
}
}
//print('key: ' + key + ' keyCode: ' + keyCode);
//print(words[round(random(0, words.length))]);
//print(round(random(0, words.length)));
//print(words);
}
function backSpace() {
if(keyIsDown(8)) {
if(millis() >= 150 + meh) {
if(typing.length > 0){
typing = typing.substring(0, typing.length - 1);
//shorten(typing);
}
meh = millis();
}
//print('test');
}
}