PONG as my first game
As my first C++ game I'm making PONG. I have a good understanding of loops, ifs, functions, and graphics and am willing to learn other things to make this work. I need to make the ball move and bounce off walls and paddles. I know I probably have to use a loop to do this but how do I make the ball bounce.
So far I have this source for the game part:
//field dimensions are 600 X 440
testWindow.DrawCircle(x-.1, y-.08, 15); //Draws circle with a 15 r
testWindow.Drawcircle(x, y, 15);
this loop draws a red ball then covers it up with a white one (because white is the background color) every time. The ball will just move slanted in animation all the way past the edge of bottom center of the screen. How do I make it bounce? I know I have to change the y coordinate to start getting smaller, but how? Also, if it's not too much trouble, can you tell me how to make paddles move with arrowkey input? Thanks a lot!
Not to hard
You need you use if statments
to have it find the top of your screen
if (y ==15) // 15 is your r
then just reverse your direction.
That looks easy enough, what graphics thing are you using...directx, open gl, allegro?
I'm sorry, I don't know which one, my teacher just gave us all the disk to put the software onto our regular C++ program. I can tell you I have to include <graphics.h> and using namespace std, but that's it. It's just simple 2D graphics. I've tried various if statements but I can't get it to do it right. and I've tried some nested loops but to no avail. Can anyone help me?
graphics.h is your graphics, (witch means you are using dos)
Ok first you don't want to to move your ball with a loop.
This will get you started.
int main ()
int ballx, bally; //balls x and y axis center postion
int player1x, player1y; //players padles the corner
/* place the ball in the center and put the paddles down. */
//now for the collesion detection
//start moving the ball
for (int x=0; x==1; x=x) // loop
if (bally=15) // if the ball hits the top of the screen
/* since this is homework you can figur out the bottom and player paddles as well as how they move */
if (ballx= player1x-Width of paddle- 5)
x=1; //sets x to one ending the loop
that didn't even make the ball move, and secondly I'm a beginner so I don't know what "ballx++" and "bally++" means. I tried that and the ball didn't move. I'm only familiar with something like "++a", which adds one every time within a loop. I don't understand how you can redraw the ball over and over without a loop either. Please help/explain your method. Thanks.
put the ballx++ and ballyx++ inside the for loop, and put in the function to redraw the ball right after it.
ballx++ increases the value by 1.
> for (int x=0; x==1; x=x)
I might be wrong, but I don't think this for-loop will work. It will loop as long as x=1 (x==1), but since you set x to 0 (int x=0) it will never loop. And exactly what is the meaning of this code: x=x???
It would be a lot better to use this:
> if (bally=15)
> if (ballx= player1x-Widthofpaddle-5)
This will probably not work like you want it to. It should be ==, not =
OK, I got the ball moving and bouncing, but how do I make the paddle move when arrow keys are pressed? Do you use GetKeyPress? Would this work:
case ARROW_UP: ypaddle = ypaddle+5; //moves paddle up
case ARROW_DOWN: ypaddle = ypaddle - 5;
else if (ckey==ESCAPE)
x=1; //just to end loop and stop game;
//Keep in mind, this is all within the larger for loop
computer paddle movement
How can I make the computer paddle move so that the computer is beatable? If I just set the y-variables of the paddle (top and bottom) equal to the where the ball is then the computer is unbeatable, but how can I make it so the paddle moves slowly and allows you to beat the computer? Help me please. Also, I need to set up a 2 player game, but the keyboard input would be hard to do since you have 2 people hitting keys at the same time, so how do you do it? Thanks.
velocity is the key. Set the computer paddle to move up and down with the ball, but only at a max speed. for higher levels make it a higher velocity.
I know that, but how do you make it move at that set speed, hitting the ball sometimes but missing it other times? How, do I make the bal stop fashing so much? It's really annoying. And how do I allow 2 people to press buttons at the same time and move seperate paddles for a 2-play mode? Because right now, if one player were to hold a button down the computer would just ignore the other player's commands. Thanks.
How do I make the computer accept input from 2 users at the same time to move corresponding paddles? Please, if someone could just help me with this I could finish the game.
I think all you have to do is use different keys. Example:
Player 1: Arrow keys
Player 2: a, s, d, f
I think that's right
Yeah I know
I know that, but if someone held a button down it would ignore the other player. How do I do it?