Help with a Pong game ^-^

Started by
6 comments, last by Hushed 16 years, 9 months ago
Well, just like everyone else that starts, Im making a Pong game. Entitled "Zombie Ball". The difference? One paddle is green. But this is besides the point. Im using C++ with Allegro. Ive come across having everything working, but one thing, Ball movement. This is how I think it should be set up in my head. It has two directions, a leftright, and an updown. In the beginning, randomly it picks up or down, and left or right. This is its heading. When it hits a wall, if its going up, it goes down, if its going down, the ball will shoot up. Throughout the game, it checks for winning conditions. So if the ball goes behind the paddle, the other player wins. Now, when it hits a paddle, it randomly chooses to go up, or down. If its going left, it switches to right, if its going right, it switches to left. If its not touching anything, it continues along its path. Another thing, keeping the score is fine, its outputting the score to the screen. I have an area of the screen that I can put the score, but it doesnt let me textout_ex an int variable. So I convert it to a string, it didnt work, and I convert it to a char, it didnt work. So im thinking I may have to try something other than textout_ex Now, I need to know if this is the right set of mind, and also how to create a random int, also about the textout_ex thing. Thank you GameDev ^-^ [Edited by - Hushed on July 16, 2007 5:08:58 PM]
Advertisement
Seems like you're headed in the correct direction.

Quote:also how to create a random int.
What language are you using?
Ah, sorry. C++ with Allegro, edited my first post.

Also need help with the score system.
Quote:
Another thing, keeping the score is fine, its outputting the score to the screen. I have an area of the screen that I can put the score, but it doesnt let me textout_ex an int variable. So I convert it to a string, it didnt work, and I convert it to a char, it didnt work. So im thinking I may have to try something other than textout_ex


Thank you, ;)
you can use boost::lexical_cast to cast ints to std::strings painlessly.
int score = getScore();render.drawText(boost::lexical_cast<std::string>(score));

or you can use stringstreams (less cool!)
#include <sstream>int i = getScore();string str;stringstream out;out << i;str = out.str();

or
#include <sstream>template <class T>inline std::string to_string (const T& t){std::stringstream ss;ss << t;return ss.str();}


random numbers

Boost also has a random number library
Hello I googled in advance for you and found this piece of code that might help you:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int random_integer = rand();
cout << random_integer << endl;
}

alternatively you can go to this website http://www.daniweb.com/forums/post9554.html and find out many other examples related to random numbers.

for the score system, you just need to keep track of the score of both players and then you will need to check when the ball leaves the right side of the screen you increase the right player's score(which can be a simple variable ex: int rightPlayerScore). Thats all! Hope it helps.
blog: www.brasilokau.com/games/blog
building on Hushed's post, if you are gonna use the random, but you want it too be a 1 or a 2, then you do this:

#include <cstdlib>int rand_number=rand();rand_number=(rand_number%2)+1;


this will set rand_number to either a 1 or a 2 (up/down) (left/right).
Quote:Original post by BrasiLokau
Hello I googled in advance for you and found this piece of code that might help you:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int random_integer = rand();
cout << random_integer << endl;
}



That's not a random number, as it's never seeded properly. Follow my first random number generator link. It discusses this.
Thank you everyone, I believe you answered my questions.

This topic is closed to new replies.

Advertisement