Sign in to follow this  

So i want to make Pong and Tetris

This topic is 3498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, after finishing my C++ class this semester, I thought of doing something a bit more advanced since I have the time. I checked out the "Forum FAQ, Guidelines, Best Practices" thread, and it suggested I start with a few types of games. I've done the Tic-Tac-Toe and the Magic 8 ball program, so now I want to try my hand at Tetris and Pong. It feels like its a good step up from those text-based applications, but I have no idea how I could implement manipulation of moving objects in real time. Can someone point me into the right direction as to how I would get started with these?

Share this post


Link to post
Share on other sites
This should be really easy if you know C++ well enough.

For Pong, just create a class called Ball which contains variables for its x and y coordinate and a variable for x_speed and y_speed. Then, each frame, update the position of the ball by something like ball.x = ball.x + ball.x_speed; do the same for y, and there you have it, a moving ball.

All you need to do is to check for collisions with paddles and make changes accordingly. For example, an easy way of doing this would be to find if the positions of the ball and paddle are such that they are colliding, and just make the x_speed variable negative, making it go the opposite direction. The change in y_speed really depends on how you want to handle it. You could, for instance, make it depend on whether the ball hit the top or bottom of the paddle. Also, don't forget to make the ball bounce back the same way off the top or bottom of the screen.

EDIT: I realized that the issue here may have more to do with not knowing how to use graphics in your C++ programs. For that, you should view this thread since it covers just that. Also, the search feature here will turn up tons of results.

Share this post


Link to post
Share on other sites
Ok thanks :D. Yeah, I'm not familiar with how to use graphics in C++, so I'll give that article and that thread a good read tonight.

Share this post


Link to post
Share on other sites
If you're not too familiar with graphics, then you should definately check out SDL. It's a great library for beginners to start programming graphical apps, I use it too (seeing I'm still a beignner technically).

In order to learn about SDL, I used Lazy Foo's tutorials. They are also great.

The main difference between Tic Tac Toe / Magic 8 Ball and things like Pong and Tetris is that you need to find a way to move through time in a constant way.
For the algorithms, you should either be able to figure it outyourself, otherwise use the leads bschneid gave you.

Share this post


Link to post
Share on other sites
Quote:
Original post by bschneid
This should be really easy if you know C++ well enough.

For Pong, just create a class called Ball which contains variables for its x and y coordinate and a variable for x_speed and y_speed. Then, each frame, update the position of the ball by something like ball.x = ball.x + ball.x_speed; do the same for y, and there you have it, a moving ball.

All you need to do is to check for collisions with paddles and make changes accordingly. For example, an easy way of doing this would be to find if the positions of the ball and paddle are such that they are colliding, and just make the x_speed variable negative, making it go the opposite direction. The change in y_speed really depends on how you want to handle it. You could, for instance, make it depend on whether the ball hit the top or bottom of the paddle. Also, don't forget to make the ball bounce back the same way off the top or bottom of the screen.

EDIT: I realized that the issue here may have more to do with not knowing how to use graphics in your C++ programs. For that, you should view this thread since it covers just that. Also, the search feature here will turn up tons of results.

Yup if you can't make sense of the majority of this code snippet from a Pong game then you probably aren't ready and need to do some coding:

// move the ball. If a ball hits a wall then reverse the direction of the velocity
// in that direction
void moveBall()
{
int ox=(int)ball.x;
ball.x+=ball.speed*ball.vx;
if (ox<(player[1].x-15))
{
if (ballHit())
{
ball.x-=ball.speed*ball.vx;
ball.vx=-ball.vx;
}
}

ball.y+=ball.speed*ball.vy;
if (ballHit())
{
ball.y-=ball.speed*ball.vy;
ball.vy=-ball.vy;
}

if (ball.x<0)
{
player[1].score++;
initBall();
}
if (ball.x>623)
{
player[0].score++;
initBall();
}

}


I recommend Allegro myself since it seems to be slightly easier than SDL IMO and besides here's a link to complete tutorials building Pong and Tetris using Allegro!

Share this post


Link to post
Share on other sites
This

ball.x-=ball.speed*ball.vx;
ball.vx=-ball.vx;

can use some extra spaces for better understanding:

ball.x -= ball.speed*ball.vx;
ball.vx = -ball.vx;

Share this post


Link to post
Share on other sites

This topic is 3498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this