Newbie Cocoa Q: Help with pong
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:
Can anyone suggest an approach to implementing an opponent AI?
Code:
// 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];
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.
As for implementing an AI, you could just make it follow the ball, but make 'mistakes' in accordance with its difficulty.
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.
A good example is Matt Diamond's "3D Paddlebash!", just look at the difficulty controls and you can really get a good grasp.
"Most nutritionists say that Twinkies are bad. But they're not, they're very very good."
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.
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.
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];
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];
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.
Although it looks neater than my code usually does...
Give us future updates, I'm interested if it works out for you.
OK, here is the current version: http://www.geocities.com/salisburgensis/
Currently working on converting it to a shuffleboard game.
Currently working on converting it to a shuffleboard game.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| [Request] Pong Source & One Question | McSpider | 6 | 6,786 |
Sep 18, 2011 07:21 AM Last Post: fudog |
|
| Newbie question: Cocoa and Carbon - which for what and how to get started? | BliTz | 3 | 2,852 |
Jul 27, 2006 10:07 PM Last Post: OneSadCookie |
|
| Pong AI trouble | saxplayer13 | 15 | 7,124 |
Dec 16, 2005 10:40 AM Last Post: jamil5454 |
|
| Newbie Cocoa query - setTitle: at window init | sealfin | 3 | 4,525 |
Nov 14, 2005 10:32 AM Last Post: akb825 |
|
| My first Cocoa Game that works! (Pong) | blobbo | 16 | 9,216 |
Aug 4, 2005 03:15 PM Last Post: Andrew |
|

