I made a program that was supposed to recreate that game, "MonkeyType" in which you have to write a specific text, if you make a mistake, the letter turns to red, and if it's good, the letter is white.
I made a sf::Text object using sfml, which is colored in white, i takes the letter if it is the good one, and a space if it is the wrong one, I did same with a red text that makes the opposite.
and I draw them with same coordinates.
I am using a monospace font, so that letters size should not be a problem, but as the text becomes bigger, the red text seems to shift on the left. so the letters end up being superimposed.
I have a Class Mot that has a string field and a Method ajouterLettre to add a letter to that string. And a Method that returns the string getMot().
class Mot
{
string orthographe_;
public:
...
void ajouterLettre(char lettre) { orthographe_.push_back(lettre); }
string getMot() { return orthographe_; }
};
class Phrase
{
vector<Mot> tabMots_;
public:
...
void ajouterLettre(char lettre);
};
The method adds a letter to the last word of the list.
void Phrase::ajouterMot(const Mot& mot) {
tabMots_.push_back(mot);
}
I'm using Monospace font. In the main function, I create two texts, a red one and a white one. and two objects phrase that makes the text of these sf::Text objects. the (1, false) parameters means the phrase has only one empty word (Mot).
sf::Text textecrit1(phraseecrite.Text(), font, 24);
textecrit1.setFillColor(sf::Color::White);
textecrit1.setPosition(10, 10);
Phrase phraseecrite(1,false);
sf::Text texterreur1("", font, 24);
texterreur1.setFillColor(sf::Color(255, 0, 0));
texterreur1.setPosition(10, 10);
Phrase phraserreur(1, false);
phrase objects has a Text Method that returns a string corresponding to the list of words separated by a space.
string Phrase::Text() {
string texte;
if (tabMots_.size() == 1) {
texte = tabMots_[0].getMot();
}
else{
int i;
texte = tabMots_[0].getMot();
for (i = 1; i < tabMots_.size(); i++) {
texte = texte + " " + tabMots_[i].getMot();
}
}
return texte;
}
I have an isCorrect function that works and tells if the last letter of two sentences (Phrase) is the same.
when the user types a letter on the keyboard, the phrase "phraseecrite" takes the letter, and a phraseerreur that takes a space if it is the right and the letter if it is not. I also update the rendering sf::Text
I draw them one on top of the other so that the red one covers the white one. phraseaecrire is the sentence the player has to match with.
if (event.text.unicode >= 'a' && event.text.unicode <= 'z') {
phraseecrite.ajouterLettre( event.text.unicode );
textecrit1.setString(phraseecrite.Text());
if (phraseecrite.isCorrect(phraseaecrire1)) {
phraserreur.ajouterLettre(' ');
}
else
{
phraserreur.ajouterLettre(event.text.unicode);
}
even if I add a space to phraseerreur to make them be a the same position when the letter is incorrect. it shifts gradually when the text gets bigger, just a little at the beginning, but it becomes bigger when the text is really long.