• Advertisement

• ### Popular Now

• 13
• 17
• 27
• 9
• 9
• Advertisement
• Advertisement
• Advertisement

# try my pong and ai question

This topic is 4450 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I have been working on a pong game using a tutorial and the sdl library. You can find an initial release here. Please give me any comments or thoughts that you may have. I know that it needs a lot of work still but this is a start. Also tell me how it worked on your system and if you had any problems please give your specs. Now my one question is about ai. This game currently has no ai to support a single player mode. How hard would it be to program some ai for a computer paddle? I haven't really done any ai programming before so tips and suggestions would be welcomed. I could also get around to posting some code if need be. Thanks again and enjoy.

#### Share this post

##### Share on other sites
Advertisement
It works like a charm on my system.

There is two things I would try to fix:

1.
When the ball hits the paddle, it leave a small black hole in the paddle.

2.
When the ball hits the paddle too close to the paddle's edge, something weird happens. It looks like the ball whipes out the paddle by following it, before it keeps moving out of the screen.

One thing that would be cool is to adjust the direction of the ball based on where on the paddle it hits.
I would also move the paddles away from the window edge a bit.

When it comes to writing a decent AI player, I have no experience, but making the "perfect" player should not be too hard. The problem, I guess, is to make it "stupid" enough to miss the ball now and then, in some random way.

#### Share this post

##### Share on other sites
agreed. the harder part (in most cases for games like this) is to make it stupid enough to miss, but not TOO stupid. but its much easier to create it so you just cant beat it. perhaps make it so it can only move SO fast but the ball gets increasingly faster if its hit on a certain part of the paddle. or maybe even with every hit, it gets alittle faster. just a thought. goodluck!

#### Share this post

##### Share on other sites
Works fine on my system, i would tweak the ball speed and y velocity because it feels like your watching the ball just go side to side. The only problem I noticed were the black dot when the ball hits the paddle. Also, the paddle going up or down on the ball is a problem with almost everyone's first pong attempt. I think what happens is the ball gets trapped inside the paddle when you move up or down ON the ball, then the collision detection sees that it meets the criteria for collision and changes the X direction every frame so that it just goes back and forth until it gets out of the paddle. When I figured it out on my pong game, I tested it on every pong game I play and probably about 75% of them have this problem. To fix it you either have to change when in the loop the collisions are tested or actually set the pixel to a certain position if a collision is found, I can't remember which. Congrats on a completed project!

#### Share this post

##### Share on other sites
Thanks for the comments. I just have to figure out how to put a simple ai to work. Then I can have both single and 2 player games. I also might add a score system and maybe even put a menu system on it.

#### Share this post

##### Share on other sites
Hey,
not bad at all.

The only bugs I encountered, were the same ones, that the other people mentioned.

On the black dot, I think I know the problem. It seems like, that you are clearing part of the screen, when it hits the paddle. So like...the ball hits the paddle, and you cleared that part of the screen, to the background color.

Chad.

#### Share this post

##### Share on other sites
The "trick" to make a perfect AI is to always make the AI paddle follow the ball. Move the paddle toward the ball's center at all times. Then, when the ball approaches the AI paddle's end, it will already be centered on the paddle and it will be hit.

An easy way to make it non-perfect is to generate a random number for each frame that will tell whether the AI paddle should move toward the ball or not. So, for instance, make a random number between 1 and 100 each time the ball moves. If the number chosen is >= 80, then do not move the paddle toward the ball. Hence, if set appropriately, the AI paddle will miss the ball sometimes.

#### Share this post

##### Share on other sites
Does anybody know of any good pong ai code that they have seen anywhere before? I've been searching and haven't really came up with too much yet.

Also, here is my paddle collision code

[sourcecpp]// bounce ball if it hits paddle  if (((game->ball.x <= game->p1.w) &&       (game->ball.y >= game->p1.y &&        ((game->ball.y + game->ball.h) <= game->p1.y + game->p1.h))) ||      (game->ball.x >= (SCREEN_WIDTH - (game->p2.w + (game->ball.w))) &&       (game->ball.y >= game->p2.y &&        ((game->ball.y + game->ball.h) <= game->p2.y + game->p2.h)))) {    game->slope.dx *= -1;  }

Does anybody know what part would cause the black dot to appear when it hits off the paddle? Thanks again for any comments or tips.

#### Share this post

##### Share on other sites
Look for this,

SDL_UpdateRect(g_Window, 0,0,0,0);

You are painting your rect with black where you arent supposed to. Also have a look at which order you are blitting your surfaces.

Good Luck.

#### Share this post

##### Share on other sites
on my sig i have a pong game called bounce and bounce tennis..both are pong games with included source. They both deal with the ball stuck in paddle problem in different ways. One allows the ball to hit the wall and go through the top of the paddle. The other has top and bottom paddle collisions. that bounce the ball and changes y velocity if it hits the top or bottom

#### Share this post

##### Share on other sites

• Advertisement