Sign in to follow this  

try my pong and ai question

This topic is 4358 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


Link to post
Share on other sites
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


Link to 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


Link to 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


Link to 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


Link to 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


Link to 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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites
Pong AI link for you

http://www.javaspot.net/articles/pong/#2.3

The easiest none simple AI is to do what mnansgar suggested.

If you want to get more complicated you could do something like as follows.

Find the point where the ball will hit. Then make this value fuzzy. That way the computer paddle won't know exactly where the ball will hit.

The hard part is making the point fuzzy so that it still looks realistic. You want the paddle to hit the ball most of the time. What I suggest is to use a bell shaped curve between -3 and 3. Then you set up points where the ball will miss the paddle say below -2 and above 2.
Check out the following link under Gaussian Random Numbers
http://www.bearcave.com/misl/misl_tech/wavelets/hurst/random.html
for more info on how to get your random numbers.

So to make it more concrete. If your ball is going to hit at x=100 and your paddle is 20 units wide. Then if the random number is 0 the ball will hit the exact middle of the paddle. If the paddle centre is below 90 the ball will miss the paddle
rand Num Paddle centre Ball centre
0 100 100
1 or -1 97 or 103 100
2 or -2 90 or 110 100
2.01 89 or 111 100 The ball will miss the paddle.

So depending on the random number generated the value passed to the computer paddle changes. If the rand num is 2 then the ball centre passed to the paddle is 110 even though the ball will actually hit 100.

I hope this makes a bit of sense. :-) If not ask more questions and I will try to explain better.

Greig

Share this post


Link to post
Share on other sites

This topic is 4358 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this