String speech = "four score and seven years ago our fathers brought forth " +
"upon this continent a new nation conceived in liberty " +
"and dedicated to the proposition that all men are created equal";
String[] words = speech.split(" ");
float[] xs = new float[words.length];
int nextWord = 0;
float bottom = 10; // change this to 0 once program is ready
float speed;
int fontSize = 24;
String myWord = "";
float wpm = 0.0;
int startFrame = -1;
void setup() {
size(400, 600);
speed = height / (20.0 * 60);
for (int i = 0; i < xs.length; i++) {
textSize(fontSize);
xs[i] = random(0, width - textWidth(words[i]));
}
}
void draw() {
background(0); // black
// display normally if nextWord still is in array
if (nextWord < words.length) {
fill(0, 255, 0);
text("WPM: " + (int)wpm, 100, height-100);
drawWords();
// draw typed word
textAlign(CENTER);
textSize(fontSize * 3 / 2);
text(myWord, width / 2, height - 5);
}
else {
textSize(72);
textAlign(CENTER, CENTER);
text("Victory!", width/2, height/2);
text("WPM: " + (int)wpm, width/2, height-100);
}
}
void drawWords() {
textSize(fontSize);
textAlign(LEFT);
for (int i = nextWord; i < words.length; i++) {
float y = bottom - i * fontSize;
fill(255); // white
text(words[i], xs[i], y);
}
bottom += speed;
}
void keyPressed() {
if (key == BACKSPACE) {
myWord = "";
}
// ignore spaces, also stop once all words are done
else if (nextWord < words.length && key != ' ' && key != ENTER) {
if(startFrame < 0) {
startFrame = frameCount;
}
myWord += key;
// check typed word against word from array once length
// matches up
if (myWord.length() >= words[nextWord].length()) {
// if it's a match, move to new word
if (myWord.equals(words[nextWord])) {
nextWord++;
}
// code this part later, after adding speed
else { // otherwise, a speed penalty!
speed *= 1.05;
}
float timeElapsed = (frameCount - startFrame) / 3600.0;
wpm = nextWord / timeElapsed;
// clear typing once each word is done
myWord = "";
}
}
}