iDevGames Forums
Newbie Cocoa Q: Help with pong - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Newbie Cocoa Q: Help with pong (/thread-7343.html)

Newbie Cocoa Q: Help with pong - JeremyL - Feb 5, 2003 08:08 AM

I'm trying to make a game of pong with Project Builder/Interface Builder and Objective-C. I'm using an NSView subclass for the playing field, an NSAffineTransform with an NSBezierPath for the ball and an NSView subclass for the two paddles. I have separated the ball and two paddles into three separate NSThreads. The ball and player paddle work ok, but I'm struggling with implementing the opponent AI. My player paddle movement method looks like this:
// get mouse location and size of playing field
    NSEvent *theEvent = [NSApp currentEvent];
    NSPoint p = [theEvent locationInWindow];
    NSRect viewRect = [self bounds];
    // stop moving the paddle at the x bounds of the playing field
    if (p.x > viewRect.size.width)
        p.x = viewRect.size.width;
    if (p.x < 0)
        p.x = 0;

    // set the center of the paddle to the mouse x
    p.x -= 30;
    p.y = 0;
    [aPaddle setFrameOrigin: p];
Can anyone suggest an approach to implementing an opponent AI?

Newbie Cocoa Q: Help with pong - furballphat - Feb 5, 2003 09:00 AM

Why are you using separate threads? It is a lot easier to just have one thread which updates both paddles and the ball.

As for implementing an AI, you could just make it follow the ball, but make 'mistakes' in accordance with its difficulty.

Newbie Cocoa Q: Help with pong - Dr. Light - Feb 5, 2003 09:55 AM

Make the enemy paddle move slow, to a certain extent. That way when the ball is traveling at an extreme angle, with a subsequently faster X velocity, and the cards are just right, the player will score. Making it react at a certain distance away from its paddle will also be another variable wich can make it behave differently. For a really whopper AI, make the enemy paddle aim for the corners.

A good example is Matt Diamond's "3D Paddlebash!", just look at the difficulty controls and you can really get a good grasp.

Newbie Cocoa Q: Help with pong - Holmes - Feb 5, 2003 10:48 AM

This is the simplest AI I can think of, while still having it be effective. Enemyspeed is how many pixels the enemy paddle should move per frame. Enemyspeed is incorporated into the if statements so that the enemies paddle doesn't adjust itself back and forth when its right inline with the ball (you might see what I'm talking about later).

if (enemyPaddle.x < ball.x - enemySpeed) {
enemyPaddle.x += enemySpeed;

if (enemyPaddle.x > ball.x + enemySpeed) {
enemyPaddle.x -= enemySpeed;

For a more devious AI, find a way to figure out where the ball will be when its y position reaches the enemy, and then replace ball.x in the above code with that amount.

Newbie Cocoa Q: Help with pong - JeremyL - Feb 5, 2003 11:18 PM

Thanks for the help, you posts made things much clearer. I will put all the methods into a single thread once I've debugged my collision detection method. I'll try for a more complex AI later, but I have to fix collision detection first. I got a basic AI method with:
NSRect viewRect = [self bounds];
    NSPoint p;

    // get the middle of the opponent's bat
    float bPaddlePos = [bPaddle frame].origin.x + 30;

    // dx is the current ball x speed
    float ballMod = dx < 0? dx * -1: dx;
    float speed = 1;

    // paddle speed is adjusted for smooth movement
    speed += ballMod*.75;
    if (bPaddlePos < [ball currentPoint].x-5) {
        p.x = (bPaddlePos)+speed;
    else if (bPaddlePos > [ball currentPoint].x+5) {
        p.x = (bPaddlePos) - speed;
    else {
        p.x = bPaddlePos;
    p.x -= 30;
    p.y = viewRect.size.height - 10;
    [bPaddle setFrameOrigin: p];

Newbie Cocoa Q: Help with pong - Holmes - Feb 6, 2003 10:05 AM

Great, I'm glad to see a hint of my code in there Smile

Although it looks neater than my code usually does...

Give us future updates, I'm interested if it works out for you.

Newbie Cocoa Q: Help with pong - JeremyL - Feb 14, 2003 03:48 PM

OK, here is the current version:

Currently working on converting it to a shuffleboard game.