Hi, I'm a new programmer and I have made this hangman game. I am trying to make the user input at the console and the animation to move on the game window simultaneously.
However, when the user is needed to make an input for the console, the game window only shows the first still sprite image and animate through the row.
void DrawHangman(Animation& animation, sf::RenderWindow& window, sf::Clock& clock, float& deltaTime, int totalWrong)
{
deltaTime += clock.getElapsedTime().asSeconds();
clock.restart();
if (deltaTime >= 0.25f)
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
animation.update(deltaTime, totalWrong);
window.clear();
animation.draw(window);
window.display();
deltaTime = 0.0f;
}
}
int main()
{
unsigned int videoWidth{ 900 };
unsigned int videoHeight{ 500 };
sf::RenderWindow window(sf::VideoMode(videoWidth, videoHeight), "Skipping");
Animation skipper(std::string("Sprites/blueskipper.png"), 4, 4, sf::Vector2f(200, 220));
float deltaTime{ 0.0f };
sf::Clock clock;
// Picks a random word from wordlist.txt
WordMap word;
const std::string wordToGuess = word.getRandomWord();
Game game(wordToGuess);
while (window.isOpen())
{
//Draw sprite
DrawHangman(skipper, window, clock, deltaTime, game.get_CurrentWrongLetters());
// Main game that happens in console
if (!game.playGame())
break;
}
return 0;
}
This is the playGame() function.
bool Game::playGame()
{
system("cls");
std::cout << "Guessed Letters: " << guessedLetters << '\n';
std::cout << progress;
char guess = getGuess();
IsContains(guess);
return ((wrongLetters < maxWrongGuesses) && (progress != word));
}
update function
void Animation::update(float deltaTime, int totalWrong)
{
m_timeTaken += deltaTime;
if (m_timeTaken >= 0)
{
m_source.x++;
if (m_source.x >= m_column)
{
m_source.y++;
m_source.x = 0;
}
if (m_source.y >= totalWrong)
m_source.y = 0;
}
}
Example of what happens:
It will show this on console
[console]
Guessed Letters: a12
___a____
Enter letter:
[game window]
Still image of the first sprite.
However when I tried
while (game.playGame())
{
while (window.isOpen())
DrawHangman( skipper, window, clock, deltaTime, game.get_CurrentWrongLetters() );
}
It does the complete opposite. The game window will have the sprite animated, but won't allow inputs for the console.
I understand why they both don't work but I just can't figure how animate in the game window while the console is active too.