[SFML] I Finished Pong! I Can't Believe It!

Started by
32 comments, last by stitchs_login 11 years, 7 months ago
First you store the variable as a integer, and that's what you increment when the paddle is hit.

Then, when ready to draw, you MyString.SetText() a std::string, not an integer directly.
//When paddle is hit...
score++; //Update the score.

//Convert the score to text.
std::string scoreAsText = convert_a_int_to_a_string(score); //'convert_a_int_to_a_string' is an imaginary function that doesn't exist.

//Set the new text.
MyString.SetText(scoreAsText);

//When ready to draw each frame
RenderWindow.Draw(MyString); //Or whatever...


To convert an int to a string, you could use std::stringstream.
std::stringstream is used to format text, similar to how std::cout works (same interface).
std::stringstream formattedText;
formattedText << "Score: " << score;

std::string convertedToText = formattedText.str(); //Turn the data stream into a std::string.
MyString.SetText(convertedToText);
Advertisement

First you store the variable as a integer, and that's what you increment when the paddle is hit.

Then, when ready to draw, you MyString.SetText() a std::string, not an integer directly.
//When paddle is hit...
score++; //Update the score.

//Convert the score to text.
std::string scoreAsText = convert_a_int_to_a_string(score); //'convert_a_int_to_a_string' is an imaginary function that doesn't exist.

//Set the new text.
MyString.SetText(scoreAsText);

//When ready to draw each frame
RenderWindow.Draw(MyString); //Or whatever...


To convert an int to a string, you could use std::stringstream.
std::stringstream is used to format text, similar to how std::cout works (same interface).
std::stringstream formattedText;
formattedText << "Score: " << score;

std::string convertedToText = formattedText.str(); //Turn the data stream into a std::string.
MyString.SetText(convertedToText);



mm i could be wrong as to how pong works, but why would you update the score when the paddle is hit? i thought in pong the score should only be updated if that persons paddle does not end up hitting the ball
I made it so it auto updates to dropbox. Also, I fixed it so that it actually takes skill through balancing it.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Just updated to add a MainMenu splash screen with only one button (That takes up the whole screen!)

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !


mm i could be wrong as to how pong works, but why would you update the score when the paddle is hit? i thought in pong the score should only be updated if that persons paddle does not end up hitting the ball

You're correct - I wasn't sure why the OP requested that either. I didn't know what to call the variable, so I just called it "score". Perhaps "bounces" would be a better name, but it depends on the OP's intent.

"I have a variable in my ball class that keeps the amountofhits, whenever I hit the paddle this increments. But, I don't know how to set my text equal to this variable: MyString.SetText(variable); doesn't work. Any Ideas?"
Well, to clarify, I meant that you want to see how far you can go, because the AI never fails. So whenever the ball hits the paddle and bounces off of it, the score is increased because you went farther. Also, I updated it.
In This Update:
Fixed the MainMenu.h problems, and made the Play button work, so that everything else on the screen won't play the game.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Updated:
In This Update:
Added a game over screen (Soon to be a class, I'm working on it).

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

This is the final Update, I promise :)
ChangeLog:
Added GameOver class and fixed everything, feel free to play!

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Sorry, another update :)
ChangeLog:
Made the ball move at a random speed and at a random angle at the start of each game.
(Also, look in the console for your score!)

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

I SWEAR: THIS IS THE LAST UPDATE!!!!!!!!!!!!!!!!
CHANGELOG:
+Added Score Counter
-Took away bad programming. (There was lots of it!)

LINK

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

This topic is closed to new replies.

Advertisement