PONG as my first game

This is a discussion on PONG as my first game within the Game Programming forums, part of the General Programming Boards category; In the key checking, to see what keys are pressed(ex. a=left for player 1 and <- arrow = left for ...

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

    Lightbulb I haven't tried this but...

    In the key checking, to see what keys are pressed(ex. a=left for player 1 and <- arrow = left for player 2) It think if it's like that, you can use and && statement:

    if(a && left) // quick example
    {
    move();
    }

    That's what I think will work. I never tried this, though.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Noone has mentioned the angles of the ball. If the ball hits a wall it is not just going to bounce straight back, it is based on its current angle.

    Code:
    struct tagball
    {
       double x;
       double y;
       double lastx;
       double lasty;
       double xinc;
       double yinc;
    }ball;
    
    
    double diffx=ball.x-ball.lastx;
    double diffy=ball.y-ball.lasty;
    diffx*=diffx;
    diffy*=diffy;
    double tdist=sqrt(diffx+diffy);
    
    ball.xinc=diffx/tdist;
    ball.yinc=diffy/tdist;
    
    ball.x+=ball.xinc;
    ball.y+=ball.yinc;
    
    For starting angle:
    #define PI 3.14159
    #define DEGTORAD(x) (double) ((x)*(PI)/(180.0f))
    
    ball.xinc=cos(DEGTORAD(startangle));  
    ball.yinc=sin(DEGTORAD(startangle));
    Now make sure that you adjust for the four quadrants (--,-+,+-,++) Make sure that the starting angle is correct so that the ball does not just move up and down. (0 or 180;90 or 270 - depends on alignment of your axes). The xinc and yinc should never reach a point where the ball is untouchable or does not move left and right so trap for those values as well.

    Can't find my source, so hope this is correct. If it's not accurate it is very close. It does use the so slow sqrt() and there is a faster way, but just an example.

  3. #18
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    I've already taken care of the angle the ball bounces off at. While I'm sure your method works great, my way was much easier:
    totalx=20;
    totaly=200;
    ballx=.16;
    bally=.07;
    //first paddle variables
    for (x=0;x!=1;x=x)
    {
    //user input for paddle
    //draw new paddle(s). 2 for 2-play
    if ( ball is at a certain x and between 2 of the paddle coordinates)
    ballx=(-.16); changes ball direction
    //same for other side except ballx would become .16
    if (ball hits bottom of screen)
    bally=(-.07);
    if (ball hits top of screen)
    bally=.07;
    totalx=totalx+ballx; //update ball's x coordinate
    totaly=totaly+bally; //update ball's y coordinate
    //delete old ball
    //draw new ball
    }

    notice that the slope remains the same the whole time except it becomes either positive or negative. The angles the ball hits the wall are the same as the angle it bounces off. Any ideas for computer paddle A.I.?

  4. #19
    Registered User
    Join Date
    Nov 2001
    Posts
    53

    keyboards

    ... are annoying in dos games.

    I believe what you are after is a keyboard interrupt. If the other post in reply to this works then forgive this useless addition. Your game probably needs a keyboard interrupt function in it so that rather than getting the key and then checking it, a function is called whenever a key is pressed which then does the checking.
    For the rest of the time you just use your looping to move the ball around.

    Just don't ask me how to write it 'cause I can't remember - you'd have to scourge the net

    Almosthere

    ps. for the computer's AI you could either

    1) have a good look at what Bubba posted (geeez) and get the pc to use the ball's speed and path to figure out where it's gonna land - that way it could know exactly where it was based on the path it could figure it would follow (difficult) OR

    2) use your code and simulate it all with a similar loop to your movement one (same effect but probably easier... and messier) OR

    3) get it to make an educated guess based on where the ball is and where it is going (the angle and position) to roughly figure it out before fine-tuning by moving with the ball (probably what i'd make it do - more like a person that way) OR

    4) just get the computer's paddle to move in line with the ball so it's vertical (or horizontal - whichever way you're moving it) position would be tracking it (quick and dirty).

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

    thanks

    I already have tried the way you mentioned where the computer paddle goes exactly where the ball is and it slows the ball down and makes the computer unbeatble obviously, so I'll maybe try guessing at it or Bubba's method. I also need to use that interrupting function you mentioned. The problem with my original way of 2-play input was that it would check for the guy on the right first and so if they both pressed a key, the guy on the right would always get his paddle to move and not the other guy.

  6. #21
    monotonously living Dissata's Avatar
    Join Date
    Aug 2001
    Posts
    341
    try creating another thread, then have one thread control player 1's paddle and another control player 2's

    also on the computers ai try making the exact calculations then rounding it, then subtracting the equivilent of a few cm or mm off the calculation, the computer will eventualy miss the ball
    Last edited by Dissata; 11-29-2001 at 06:15 PM.
    if a contradiction was contradicted would that contradition contradict the origional crontradiction?

Page 2 of 2 FirstFirst 12
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, 02: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, 11:12 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21