Advertisement

pong "ai"

Started by February 15, 2002 02:33 AM
5 comments, last by AbsolutDew 22 years, 10 months ago
Maybe this should go in the AI forum, but it''s more of a simple gameplay question then any kind of artificial intelligence. I decided to make a pong clone, not so much because i wasn''t capable of anything a little more advanced, but because i actually wanted to COMPLETE a game, ie a middle begining and an end, possibly levels of some kind, hi score, etc etc etc. Anyways, this is one of the first things i coded and it''s been left till now, and well, it sucks.

//-----------------------------------------------------------------------------
// Name: MoveOpponent()
// Desc: Handles the computer controlled paddles movement
//-----------------------------------------------------------------------------
VOID MoveOpponent()
{
	// Really stupid AI, test the ball position against the center
	// of the paddle, and move the paddle accordingly

	// First test some possible conditions in which the computer
	// doesn''t have to do anything

	// Get the y coordinate to the center of the paddle and ball
	float paddle_center = pong_paddle[PLAYER_2].position.y + PADDLE_WIDTH / 2;
	float ball_center = pong_ball.position.y + pong_ball.radius;

	// Is ball moving away
	if( pong_ball.velocity.x < 0 )
		return;
			
	// Is ball close enough to worry about
	if( pong_ball.position.x < SCREEN_WIDTH / 2 )
		return;
	
	// Is ball close enough to center of paddle, also somewhat prevents jerky movement
	float difference = (float)fabs( ball_center - paddle_center );
	if( difference < PADDLE_WIDTH / 3 )
		return;

	// Move paddle to intercept
	if( ball_center > paddle_center )
	{
		pong_paddle[PLAYER_2].position.y += PADDLE_SPEED * g_pTiming->m_fSpeedFactor;
		pong_paddle[PLAYER_2].moving = TRUE;
	}
	else if( ball_center < paddle_center )
	{
		pong_paddle[PLAYER_2].position.y -= PADDLE_SPEED * g_pTiming->m_fSpeedFactor;
		pong_paddle[PLAYER_2].moving = TRUE;
	}
	
}
 
My problem is that it can not lose. I was hoping for any suggestions on making it less artificial. preferably a simple concept for a simple game, but not something as arberitrary as a preset chance of either missing or hitting.
The "can not lose" factor is common in Pong. There are ways of fixing" it. On their own, they''re not very good, but you can make a passable "AI" by combining them:

- making the computer-paddle move only when the ball is within its half of the screen, or maybe its two-thirds of the screen (play around with that)
- building in a delay in reaction (doesn''t really make sense until you read the next item)
- this is the best one, I think: make the ball not move according to a set speed, but make its speed dependent on where it hits the paddle. If it hits the paddle on the ends, it should move sideways much faster than if it were to hit the paddle dead-center. You could even take paddle motion into account when updating the ball''s velocity. If the ball''s maximum speed is higher than the paddles'', there''s actually some skill involved other than just precisely mirroring the ball movements with the paddle.

Kippesoep
Advertisement
Thanks Kippesoep I actually have the first one, it''s set to half screen.. i guess i could move it closer to the side. I have a little random y velocity changes built in when the paddle is moving. I wonder if I could increase or decrease it based on how far off center the ball is.

Ill also see about somehow adding a reaction delay, and maybe speeding up the ball a little.

anyone have some other ideas while im at it?

Thanks again
You can have the paddle be able to move slightly in x direction and set a velocity there, making it behave more like a ping pong or tennis. Then you can randomize the computers behaviour (x) and make the balls velocity accumulate with the players movements?
I''ve never done this, but maybe you could "pretend" the ball is at a different place.

For example, your CPU paddle knows the ball is at (x,y) and is moving with velocity v. Try spicing it up a little bit. Tell the cpu that the ball is at (x + rand()%3, y + rand()%3) and the velocity is (v + rand()%3). When you calculate your paddle''s actions from this (slight) disinformation, it might occasionally miss the ball.

-------------
SpaceRook
Latest Project: Paper Shredder
Visit my homepage for other projects!
Give the AI what''s called a field of view. For instance do not allow the AI to see the ball until it''s at least 1/2 way across the screen. It will then have less time to react and account for the balls movement.
Advertisement
I made a pong game once (not programmed, I''m ashamed to say: it was in my innocent "author-ware" years...)
I don''t have the time/patience/copitence to read through your code, but heres my suggestion:
First of all, to make a HARD computer (as in, they can select the difficulty), you should make it so that when the Y position of the (center of the) paddle is different from the Y position of the ball, the paddle moves up/down to compensate; IMPORTANT: The computer''s paddle should move at the same speed/acceleration (if you have it accelerate in your game, and not jump directly to a certain speed) should be the same (if not less, to make it easier to beat) than the player''s speed/accel.
Note that in most (good in my opinion) pong games, the ball does not speed up when it hits the edges, but it''s speed increases everytime it hits a paddle. If you''re playing against HARD, and you''re REALLY good, you CAN beat the computer that I have just described (like Kippesoep said, theres some skill involved when playing pong besides just mimicking the balls movements: if, after playing for a short wile (with the ball going pretty fast), and you hit the (fast) ball on the corner of your paddle, and it hits the top of the screen about 3/4 of the way to the computer, and the computer''s paddle is on the top, it won''t have enough time to move down to the bottem and hit it back. If it were smart, it would have known that it would have had to stay on the bottem, because thats where the ball would end up when it hit it''s side. Of coarse, if it DID calculate the trajectory (excuse my spelling ), than it WOULD be impossible!)
Hope my random babbling helped, otherwise I wasted 15 minutes of my life....I say that like I wouldn''t have wasted it anyways.....



"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin

This topic is closed to new replies.

Advertisement