Jump to content
  • Advertisement
Sign in to follow this  
redwingdw

Pong like game need to make 1 bat have ai

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

Hi I am in the process of designing a crappy pong game .....just to get some understanding of the basics of game programming, I am using SDL andat present I have a game loop and the to bats spawn at opposite sides of the screen and the ball spawns in the middle of the screen. At present the player Bat object can be moved up and down the y axis via a handle events method. Now for the hard part im guessing ......which is maybe putting the ball starting on the computer bat giving the comp bat some ai and getting it to fire the ball at some random angle....I havedone some basic collision tutorials so im only semi worried about that , what worries me more is how do I get the computer to catch a ball thats fired its way without giving it absolute values so its got 100% success rate? I was thinking along the lines maybe I need to use a random number function and limit the amount of random numbers available to the computer dependant on difficulty......is this the correct chain of thought? also believe it or not I have never used any of the C random commands and all my values are ints so I was hoping for a random function that works in ints .....If anyone has any code snippets ideas for stuff that will be needed to make the final part of this game work it would be appreciated.......Also is there any sdl orientated tutorials that relate to AI? Thanks David One other thing I have been trying to immplement my program OO style and have succeeded for a big part of it but with me still learning Im playing it safe and doing some parts procedural......e.g only making objects out of stuff that can be clearly defined like Ball, and Layer which is a parent abstract class of Bat which deals with the cpu and player bat functions

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
if you limit the speed that both paddles are allowed to move, to be slightly slower than the speed at which the ball may change it's vertical position, then eventually the computer (and human) will miss

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
additionally, you can make the AI dumber by having it ignore the ball unless it is within a distance based on difficulty level
at highest level it would have unlimited range and try to keep the paddle centered on the ball regardless of how far away, and lower level it doesnt move the paddle till the ball is on it's half of the screen, etc

Share this post


Link to post
Share on other sites
Thanks ) .....hehe I was just trauling google there and come across some advanced AI stuff thats modelled on neurons or something man that stuff is scary lol ...the ideas supplied here are what I will go with :-)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Another idea is after you hit the ball, determine the point where the path will cross the computer opponent's paddle zone and have it always go there but slightly off a little more each time. This results in the computer player sending the ball back at you with easy angle at first, and greater angles subsequently, and if you can survive the greatest angles then eventually the computer paddle moves to a point where the ball misses the edge of it's paddle and you survive.

Alternately, have the computer always hit but there's a small area it can't move the paddle, so you have to aim to get the ball into that blind zone.

Just food for thought...

Share this post


Link to post
Share on other sites
Limit the speed of the paddles, and have a small 'acceleration' value for the AI paddle so that the AI paddle will start moving immediately but will not be moving at it's full speed immediately. Reduce the amount of time this takes for increased difficulty settings or later levels if you have either of those implemented.

When the ball is moving away from the AI paddle, have it wander towards the centre, not neccesarily at it's full speed. Once it reaches the centre or if it's already there, have it wander up and down randomly, but only a very short distance.

When the ball is moving towards the AI paddle, use trig to figure out exactly where the ball will hit. This is where the AI paddle should ideally move to, but rather than doing that, randomly generate a number and either subtract it or add it to the target position and use that as the goal instead. Generating smaller random numbers will increase the accuracy of the AI, and again could be used for difficulty settings or levels.

The above technique may sometimes result in the paddle moving past where the ball will hit, which makes the AI look pretty silly. If you'd like to make it look a bit more intelligent (and increase the difficulty somewhat), have it use the error-prone value you generated until the ball gets within some distance, at which point you would give the paddle the real target location. This will result in a behavior where the AI paddle will sometimes move too far and then correct itself, much like a human player. Obviously the further away the ball is when you give the paddle the real target, the more accurate this will be.


All of the above techniques involve the use of numbers you'll have to experiment with to find good values you like, but once you find the right distances, speeds and rates of acceleration it can look quite realistic.


Oh, and you'll want to use rand() to generate your random numbers. Don't forget to seed it with srand() first, or you'll generate the same 'random' sequence each time.


Hope that helps. [smile]

Share this post


Link to post
Share on other sites
hehe cheers :-) but upon looking on my program today ive figured I need to get the ball initialised on the computer bat ........well just touching it .....I have a severe problem that is I cant get an x and y from an sdl surface and using SDL Rect and SDL_GetClipRect is causing other complications but I have started another thread on that :-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!