What you ask is non-trivial. I will start with the simplest solution and expand it.
First, assume that you and your enemy are motionless. You need to calculate the angle between you and your enemy, turn your weapon to point at the enemy, and then shoot. Use your favorite search engine to find a description of how to find the angle between two points on the plane (you said that 2D).
Once you write the code that can do the above, go to:
Your enemy moves in a constant direction at a constant speed. You are still motionless. This is a surprisingly difficult problem. To simplify, we assume that you can aim at your weapon instantly.
If you know where you are and the enemy, as well as the speed and direction of the enemy, you can at any time determine the position of the enemy (and, therefore, his distance and direction from you).
You know how fast your shell can travel. Therefore, if you draw a line from your current position to intercept any position on the expected path of the enemy’s movement, you can determine how long it will take for your projectile to hit the enemy. Thus, the key is to find a point on the enemy’s path, that if you shot a shell immediately, it crossed the enemy at one time. This usually requires consistent approximations.
If you cannot rotate your weapon instantly, the problem becomes more complex, because the time it takes to rotate your weapon to point at the enemy depends on how fast and in which direction the enemy moves. Additional approximations are required.
Things become even more active when you and the enemy move, although you can build the math so that you "stay motionless." That is, do the conversion of the speed and trajectory of the enemy to reflect how the enemy moves towards you. Then the math becomes identical to the fact that you are motionless.
Mathematics itself is the most elementary trigonometry. You need to know how to calculate the distance between two points, the distance between a line and a point, the angle between two points, calculate the points on a line with a given starting point and direction, and how to rotate around an arbitrary point. These are all well-known issues in which there are many good examples on the Internet. However, you will have to do a little research to find them.
It is probably best to find a good computer graphics tutorial.