Dragon Song bloggpost #5

This is the last week of the game before the beta presentation. The programmers have been focusing on getting as much done as we possibly can. We have been polishing the power ups, we have been fixing some minor bugs. This week we have also been adding a few states, we started by adding an End State which accrue when the player gets hit three times by the enemies. In the end state you can choose if you want to go back to the menu or quit the game. There is also a picture of the dragon getting attacked by the enemies of the game.

The other state that we have been working on is the Menu state. This state is a little more interesting because we have been adding buttons that you can press. At the moment we only have two buttons that will either direct the player to Game state or exit the game. We will probably add more before the final version of the game, like credits and  options.

Creating states and switching between them was not really a challenge. We started by just creating the different states .hpp and .cpp files. Then we added so you could move around between the states. When the Update function returns false, the function Get Next State triggers this function returns a string that is the name of the next state.  The string that is returned to Get Next State is a member variable of the current state, this string is determined when a key is pressed on the keyboard. For example in End State when you want to go back to the menu you have to press the Y button on your keyboard. We choose Y because we added a pop up with text that says want to go back to menu Y / N.

As mentioned we also did buttons in the menu state that takes you to game state or exits the game. The way these buttons work is that we have a sf integer rectangle and if the mouse is within this rectangle and press the mouse button we change the state to next state.  The buttons has two different sprites one is brighter than the other. When the mouse is within the rectangle the button switches to the brighter button. We did this to add the effect that you are holding the button.

This is how the code for the button in menu state looks like.

m_StartRekt = sf::IntRect(sf::Vector2i(200,250),sf::Vector2i(300,100));

if (m_StartRekt.contains(sf::Mouse::getPosition())){

m_ButtonSprite.setTextureRect(sf::IntRect(sf::Vector2i(0, 100), sf::Vector2i(300, 100)));

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
m_nextState = "GameState";
return false;
}
}

About Love Åkerlund

2014  Programming