Im doing a game that is a spin on the pong game, now Im doing the computer controlled bat, what I want is that the computer 'will return the ball much of the time', what I thought I could do that is by having the computer bat when the ball is in the second half of the screen, ie its side to follow the balls position, so it moves up and down to the position of the balls height.
That's what I've come up with, perhaps there are better ways, but its just what I came up with of the top of my head, how can I do this, Im learning as I go along, so I pick easy games to learn by, If one adds interesting extra features and adds some nice graphics, one can still make a nice game and learn MMF2s features as one goes along.
Give the opponent a velocity, provided it is less than the overall SPEED (not velocity) of the ball, it has a chance to miss it.
try to keep the opponent velocity to a set value, depending on the level. For example, easy would move by say, 5 pixels at a time, medium by 10, and hard by 15
this only works if you're increasing the speed of the ball after each contact with a bat, and will give you a few rallies before the oponent finally misses.
You then need to move it.
When the ball changes direction, have the oponent change direction as well, but it still stays at a constant velocity (perhaps give different delays for different difficulties)
I was thinking more of the computer bat is set to randomly move up and down, it can miss the ball because it may be too far away to make it to the ball in time, that gives it some skill to aim at places far from the computer bat.
But I still need to know, how do I make the computer move up or down to be inline with the ball. It needs to follow the ball on the virticle, but I dont know how to do that.
Perhaps its like, if ball is in this area, if ball is above the bat move up, if ball is below the bat move down, but Im not sure how it can do a check for if the ball is above or below it.
Assault Andy Administrator
I make other people create vaporware
Registered 29/07/2002
Points 5686
19th September, 2007 at 21:33:35 -
I'd have code that compared the Y position of the ball to the Y position of the bat. Most pong games just have the AI follow the ball, and for different difficulties you used different bat speeds.
Eg.
If Ball.Y > Bat.Y then Bat.Y = Bat.Y + 2
If Ball.Y < Bat.Y then Bat.Y = Bat.Y - 2
That's all there is to it and to make it easier or harder you just change the 2 to make the bat move faster or slower.
Assault Andy Administrator
I make other people create vaporware
Registered 29/07/2002
Points 5686
19th September, 2007 at 21:39:56 -
I have too much free time so I decided to make the engine, it doesn't take long.
http://files-upload.com/files/514070/pong%20ai.mfa
In this example the ball is moving relatively fast so the paddle doesn't hit it back very often. Play around with the ball speed to see it return the ball more often.