How to create an automatic player using AI in C++

Hi all,

I'm dealing with a QML ping pong game.

It now works very well. At the beginning, the ball starts moving automatically from the center of the table towards the right (five times, then the turn changes to the left side this time) and when the right/left paddle hits it, it reflects towards the opposite direction approaching the other paddle.

If a paddle hits the ball while moving downwards, the ball follows that direction and moves downwards, and the same for upwards as well.

Now it's partly boring, because the player must play with both paddles, that is, he is his opponent too!

To make the game more realistic, we need to have one of the paddles play the game automatically, just like any other simple computer games we've ourselves played on a single player mode.

Now what's your idea for that goal please? Since QML can use C++ code, can I use AI in C++ to do the job? If so, how to start learning it in C++ please?
Nice interface. Sure, code the logic for the far-side paddle to move, just like how you had to code logic for the ball to update and move. "AI" is just logic like anything else, it's not really special.
Read the ball's position, and update the enemy paddle to move towards the ball's y position.

Add random modifiers so that it can sometimes lag and not reach the ball, because you don't want to make a perfect enemy.
Last edited on
Thank you so much . :)

To be honest, I myself had thought of this beforehand that could easily lead into making the enemy paddle a perfect one which loses no score and will always win the game. That is done since we give it the y movement of the ball each time it approaches the the paddle. It's actually kind of cheating! :) Hhhhh

I also had thought of using the function Math.random() to making changes in the chance of reaching the upcoming ball to prevent the paddle from being always successful in it, making the game more realistic and exciting, but the problem is that, I'm not very sure I understood the method completely or precisely.
Topic archived. No new replies allowed.