Reputation: 94319
I want to create a game like Pong, and I want to know that if it is possible to create an AI (computer opponent) in JavaScript to control the right side. Pretend I have functions moveUp()
and moveDown()
for controlling the right paddles. So it is possible? Or will I have to use some kind of library?
Upvotes: 0
Views: 1287
Reputation: 2134
the math is simple enough but the platform is hardly the most efficient and it would of course depend on your degree of accuracy - the hard part would be the graphical aspect and for that it would make sense to use something simple like jquery animate or similar to manage the to and fro of the represented paddle(s) and ball
Upvotes: 0
Reputation: 46833
Yes it very much is possible.
You may wish to start with a very simple AI: have the right paddle moveUp at a constant velocity, then moveDown when it hits the top, then repeat.
After you have that coded, you should be able to modify your AI routines to switch directions based on the y-component of the ball's velocity. Then you can modify the velocity of the paddle based on the x-component the ball's velocity.
After that, look at updating the paddle controls to move when they make contact the ball, w.r.t. the left paddles position (ie if the left paddle is ontop, attempt to hit the ball towards the bottom).
Upvotes: 0
Reputation: 4209
Of course it is possible, a simple approach would just be to have the enemy attempt to follow the ball's current position on the y-axis. If the ball is lower than the center of the paddle, lower the paddle, etc...
There are a lot of other great frameworks that help in game creation, but learning and designing ones on your own from scratch is the best way.
Upvotes: 1