2016-12-15 95 views
-1

我正在使用SFML和C++來製作一個小遊戲,當您嘗試不擊中牆壁和其他對象時我稍後添加,但是我遇到了問題。我有一個生命計數器,因爲我有一個循環,當我達到零生命時停止。但問題是我之前做的字符串轉換沒有更新,並且使用了我開始使用的原始數量。 這裏是代碼的main.cpp如何在遊戲中將int更新爲字符串轉換?

#include <SFML/Audio.hpp> 
#include <SFML/Graphics.hpp> 
#include <iomanip> 
#include <locale> 
#include <sstream> 
#include <string> 

using namespace std; 

#include "ResourcePath.hpp" 
#include "iostream" 

int main(int, char const**) 
{ 
int Lives = 5; 
string textlives;   
ostringstream convert; 
convert << Lives;  
textlives = convert.str(); 

int x = 20; 
int y = 20; 
// Create the main window 
sf::RenderWindow window(sf::VideoMode(800, 600), "Dont hit it"); 

//Wall 1 

sf::RectangleShape Wall1(sf::Vector2f(120, 50)); 
Wall1.setSize(sf::Vector2f(10, 1000)); 
Wall1.setPosition(0, 0); 
Wall1.setFillColor(sf::Color::Blue); 

//Wall 2 
sf::RectangleShape Wall2(sf::Vector2f(120, 50)); 
Wall2.setSize(sf::Vector2f(1000, 10)); 
Wall2.setPosition(0, 0); 
Wall2.setFillColor(sf::Color::Blue); 
//Wall 3 
sf::RectangleShape Wall3(sf::Vector2f(120, 50)); 
Wall3.setSize(sf::Vector2f(10, 1000)); 
Wall3.setPosition(790, 0); 
Wall3.setFillColor(sf::Color::Blue); 
//Wall 4 
sf::RectangleShape Wall4(sf::Vector2f(120, 50)); 
Wall4.setSize(sf::Vector2f(1000, 10)); 
Wall4.setPosition(0, 590); 
Wall4.setFillColor(sf::Color::Blue); 

// Load a sprite to display 
sf::CircleShape shape(25.f); 
shape.setFillColor(sf::Color::Green); 
shape.setPosition(x, y); 

// Create a graphical text to display 
sf::Font font; 
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) { 
    return EXIT_FAILURE; 
} 


sf::Text text; 
text.setFont(font); 
text.setString("Unlucky you lost"); 
text.setCharacterSize(40); 
text.setPosition(200, 300); 
text.setStyle(sf::Text::Bold); 

//textlives 
sf::Text TextForLives; 
TextForLives.setFont(font); 
TextForLives.setString("Lives = " + textlives); 
TextForLives.setCharacterSize(40); 
TextForLives.setPosition(100, 100); 
TextForLives.setStyle(sf::Text::Bold); 

// Load a music to play 
// sf::Music music; 
//if (!music.openFromFile(resourcePath() + "nice_music.ogg")) { 
//return EXIT_FAILURE; 
// } 

// Play the music 
//music.play(); 

// Start the game loop 
while (window.isOpen()) 
    { 
     // Process events 
     sf::Event event; 
     while (window.pollEvent(event)) 
     { 
      // Close window: exit 
      if (event.type == sf::Event::Closed) { 
       window.close(); 
      } 

      // Escape pressed: exit 
      if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) { 
       window.close(); 
      } 
     } 

    // Clear screen 
    window.clear(); 
    if (Lives > 0) { 
     if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) 
     { 
      // left key is pressed: move our character 
      shape.move(0.1, 0); 
     } 
     if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) 
     { 
      // left key is pressed: move our character 
      shape.move(-0.1, 0); 
     } 
     if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) 
     { 
      // left key is pressed: move our character 
      shape.move(0, -0.1); 
     } 
     if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) 
     { 
      // left key is pressed: move our character 
      shape.move(0., 0.1); 
     } 
     //Wall 1 collision 
     if (shape.getGlobalBounds().intersects(Wall1.getGlobalBounds())) { 
      cout << "Collided" << endl; 
      shape.setPosition(20, 20); 
      Lives = Lives - 1; 
     } 
     //Wall 2 collision 
     if (shape.getGlobalBounds().intersects(Wall2.getGlobalBounds())) { 
      cout << "Collided" << endl; 
      shape.setPosition(20, 20); 
      Lives = Lives - 1; 
     } 
     //Wall 3 collision 
     if (shape.getGlobalBounds().intersects(Wall3.getGlobalBounds())) { 
      cout << "Collided" << endl; 
      shape.setPosition(20, 20); 
      Lives = Lives - 1; 
      ; 
     } 
     //Wall 4 collision 
     if (shape.getGlobalBounds().intersects(Wall4.getGlobalBounds())) { 
      cout << "Collided" << endl; 
      shape.setPosition(20, 20); 
      Lives = Lives - 1; 
      ; 
     } 
    } 
    else 
    { 
     window.draw(text); 
    } 

    // Update the window 
    window.draw(shape); 
    window.draw(Wall1); 
    window.draw(Wall2); 
    window.draw(Wall3); 
    window.draw(Wall4); 
    window.draw(TextForLives); 
    window.display(); 
} 

return EXIT_SUCCESS; 
} 
+5

您只需將所有代碼轉儲給我們?形狀,字體和音樂與你的問題有什麼關係? (MVCE) – abelenky

+1

您應該將問題解決爲僅需重新生成問題和解決問題所需的問題。 – silentsod

回答

1

你只是轉換你的Lives在節目的開頭的字符串一次,這是不正確的。 您應該在每次打算繪製時重新轉換:

TextForLives.setString("Lives = " + std::to_string(Lives)); 
+0

感謝您的幫助 –

+0

'std :: to_string'只適用於C++ 11 – Steephen

+3

@Steephen現在已經5歲了,讓我們強迫可以使用它的人使用它。 –