Thread: PONG as my first game

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Question 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
    for (x=20;x<=580;x=x+.1)
    {
    y=y+.08;
    testWindow.SetBrush(White);
    testWindow.DrawCircle(x-.1, y-.08, 15); //Draws circle with a 15 r
    testWindow.SetBrush(RED);
    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!

  2. #2
    .
    Join Date
    Aug 2001
    Posts
    598
    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.
    To Err Is To Be Human. To Game Is Divine!"

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    hmm...

    That looks easy enough, what graphics thing are you using...directx, open gl, allegro?

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    PONG graphics

    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?

  5. #5
    .
    Join Date
    Aug 2001
    Posts
    598
    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.

    PHP Code:
    //includes

    int main ()
    {

    int ballxbally//balls x and y axis center postion
    int player1xplayer1y//players padles the corner 

    /* place the ball in the center and put the paddles down.  */
     
    //now for the collesion detection
    //start moving the ball
    ballx++ ;
    bally++;
    for (
    int x=0x==1x=x// loop
    {
    if (
    bally=15// if the ball hits the top of the screen
    bally-- 
    /* since this is homework you can figur out the bottom and player paddles as well as how they move */

    //loose
    if (ballxplayer1x-Width of paddle5
    x=1;  //sets x to one ending the loop
    }
    return 
    0;

    This will get you started.
    To Err Is To Be Human. To Game Is Divine!"

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    PONG

    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.

  7. #7
    .
    Join Date
    Aug 2001
    Posts
    598
    Sorry

    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.
    To Err Is To Be Human. To Game Is Divine!"

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    > 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:

    int x=1;
    while(x==1)
    {
    //Game loop
    }


    > if (bally=15)
    > if (ballx= player1x-Widthofpaddle-5)

    This will probably not work like you want it to. It should be ==, not =
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    keyboard input

    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:
    ypaddle=250;
    testWindow.GetKeyPress(ckey);
    if (ckey==ARROW)
    {
    switch (ckey)
    {
    case ARROW_UP: ypaddle = ypaddle+5; //moves paddle up
    break;
    case ARROW_DOWN: ypaddle = ypaddle - 5;
    break;
    }
    }
    else if (ckey==ESCAPE)
    x=1; //just to end loop and stop game;
    system("CLS");

    //Keep in mind, this is all within the larger for loop
    Thankyou.

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Unhappy 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.

  11. #11
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    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.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    paddles

    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.
    Last edited by program man; 11-14-2001 at 02:28 PM.

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    keyboard

    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.

  14. #14
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb I think...

    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

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    10

    Yeah I know

    I know that, but if someone held a button down it would ignore the other player. How do I do it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New Project, text game, design stage.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 05-23-2007, 06:39 AM
  2. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  3. My pong game stops painting
    By SniperSAS in forum Game Programming
    Replies: 15
    Last Post: 11-25-2005, 03:42 PM
  4. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM