So i want to make Pong and Tetris

Started by
5 comments, last by DevFred 15 years, 11 months ago
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?
Advertisement
Integration Basics - For general physics integration (Euler is good enough for pong and tetris)


Fix your Timestep - For stepping in time, perfect article, taught me well


Hope it helps :)
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.
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.
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.
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 directionvoid 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!

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
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;

This topic is closed to new replies.

Advertisement