![]() |
|
Newbie Cocoa Q: Help with pong - Printable Version +- iDevGames Forums (http://www.idevgames.com/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: Code: // get mouse location and size of playing fieldNewbie 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: Code: NSRect viewRect = [self bounds];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 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: http://www.geocities.com/salisburgensis/ Currently working on converting it to a shuffleboard game. |