Pong A.I?
I''m making my secound game at the moment(pong) and i never implemented AI in my first game but i know im going to need it for pong so could anyone give me an insight into the code for an AI pong paddel? Or maybe a tutorial?
Damn I'm quick with my doubles these days...
[edited by - civguy on December 22, 2002 5:55:18 PM]
[edited by - civguy on December 22, 2002 5:55:18 PM]
You could move the ball in advance (or "internally") to predict where it will land. Then just move the computer''s pad towards that position. To not make it impossible to win, you could make it predict the position a bit wrong, maybe with some bias to up/down. And don''t forget to put reflexes for the computer. I.e. it won''t start moving immediately when it sees the ball bouncing off the enemy pad. 0,20s is typical human reaction time.
I''m not sure why people usually make pong overly complicated. My first game was pong, and the AI was just something like:
There ya go. Pong AI in only a couple lines of code. There is no need to calculate anything. With the right values for the ball movements and paddle movements this creates a very playable, moderately challenging game of pong. Plus this looks way more human-like than a paddle that moves to the exact spot and sits there, like humans do that ALL the time.
// inside the main loopif (ball_y > paddle2_y) paddle2_y += 2; // play with the valueif (ball_y < paddle2_y) paddle2_y -= 2;
There ya go. Pong AI in only a couple lines of code. There is no need to calculate anything. With the right values for the ball movements and paddle movements this creates a very playable, moderately challenging game of pong. Plus this looks way more human-like than a paddle that moves to the exact spot and sits there, like humans do that ALL the time.
Russel, pong is so simple a game so why not put some work in to AI code instead of just a few lines that make it look like a machine? At least when I play pong, I try to predict where the ball would land. And I don't have 1 ns reflexes either.
[edited by - civguy on December 23, 2002 1:00:35 PM]
[edited by - civguy on December 23, 2002 1:00:35 PM]
Here''s what I did... be forewarned, this is some of the first code I ever wrote
The basic idea here is the AI moves the paddle towards the center when the ball is going towards the other side and computes which way it should go when the ball''s heading its way. You''ll also note that the paddle bounces off of walls and is subject to the classic increase in speed over time.
_____________________________
And the Phoenix shall rise from the ashes...
--Thunder_Hawk -- ¦þ
______________________________
void DoAI (DWORD milliseconds){ if (Ball.velocity.y > 0) { float tempx, tempy, tempvelx; tempx = Ball.position.x; tempy = Ball.position.y; tempvelx = Ball.velocity.x; while (tempy < 6.25f) { if (((tempx+tempvelx*0.03125) < -4.75f) || ((tempx+tempvelx*0.03125) > 4.75f)) { tempvelx *= -1; } tempx += tempvelx*0.03125; tempy += Ball.velocity.y*0.03125f; } if (tempx < -4.75f) tempx = -4.75f; if (tempx > 4.75f) tempx = 4.75f; if (TopPad.position.x < (tempx - 0.10f)) { TopPad.velocity.x += base_accell*milliseconds*0.001f; } else if (TopPad.position.x > (tempx + 0.10f)) { TopPad.velocity.x -= base_accell*milliseconds*0.001f; } else { TopPad.velocity.x = 0.0f; } } else { if (TopPad.position.x < -0.10f) { TopPad.velocity.x += base_accell*milliseconds*0.001f; } else if (TopPad.position.x > 0.10f) { TopPad.velocity.x -= base_accell*milliseconds*0.001f; } else { TopPad.velocity.x = 0.0f; } } if (((TopPad.position.x+TopPad.velocity.x*milliseconds*0.001f) > 4.5f) || ((TopPad.position.x+TopPad.velocity.x*milliseconds*0.001f) < -4.5f)) { TopPad.velocity.x *= -0.875f; PlaySound("Data/Bounce2.wav", NULL, SND_ASYNC); } TopPad.position.x += TopPad.velocity.x*milliseconds*0.001f;}
The basic idea here is the AI moves the paddle towards the center when the ball is going towards the other side and computes which way it should go when the ball''s heading its way. You''ll also note that the paddle bounces off of walls and is subject to the classic increase in speed over time.
_____________________________
And the Phoenix shall rise from the ashes...
--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
I''ll have to clean this code up sometime soon. I plan on making a tutorial out of my pong game if I ever get it finished. It''s got to be a fully thought out game afterall. LOL...look at all the magic numbers.
When working on an AI always start out with how you would respond to all the different situations, then improve on those, then add in some random elements to the AI so the user can''t find a cheap way to beat it every time. The more situations you include and the more random changes you add, the more realistic (and probably better) your AI will be.
When working on an AI always start out with how you would respond to all the different situations, then improve on those, then add in some random elements to the AI so the user can''t find a cheap way to beat it every time. The more situations you include and the more random changes you add, the more realistic (and probably better) your AI will be.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
quote: Original post by civguy
Russel, pong is so simple a game so why not put some work in to AI code instead of just a few lines that make it look like a machine? At least when I play pong, I try to predict where the ball would land. And I don''t have 1 ns reflexes either.
Well, ok. So try and make an AI that won''t just ''not lose'' - make one which actively tries to beat the player.
I''m assuming that you''ve got the ''hit position on paddle affects bounce direction'' thing like most pong games.
Your AI could work out where the ball is going to land on it''s own side, and move there; then, it could look at the player''s paddle, and position itself so that the ball will land (on the player''s side) as far as possible from the current position.
Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement