What theagentd said is correct, but ultimately you don’t really want that angle since it will have to be transformed back into Cartesian coordinates to move your player. Here is how you would achieve what you want. Have three variables, dx, dy, and speed. The variables dx and dy represent the change in the players X and Y coordinate per game loop. In case you are curious, they are named this way with the d because of calculus. Not really important actually, you could name them “changeInX, changeInY” if you prefer. Each game loop, you calculate what dx and dy are:
dx = mouseX - playerX
dy = mouseY - playerY
So now you have the proper direction, but not the proper speed. If you add dx and dy to your players location now they will instantly teleport to the mouse location, which isn’t what you want. To fix this, we scale the values of dx and dy. Think of dx and dy as the sides of a right triangle, and the hypotenuse is the direction you are going to travel. We want this hypotenuse to be the same length no matter which direction the player is heading. We can use the Pythagorean Theorem to figure out that what we want is
dx^2 + dy^2 = speed^2
So what we will do is normalize the d variables: scale the triangle they currently make to have hypotenuse of length one. We do this by calculating the distance using the Pythagorean theorem and then dividing both variables by that distance. Finally, we multiply dx and dy by speed, which will give us the result we want! Here it all is:
double hypotenuse = Math.sqrt(dx*dx + dy*dy);
dx /= hypotenuse;
dy /= hypotenuse;
dx *= speed;
dy *= speed;
// now we have the correct dx and dy, so add those to the players position!
playerX += dx;
playerY += dy;
You can of course do a slight optimization here by dividing speed by hypotenuse and multiplying dx and dy by that. If you are heart set on using trigonometry, then the code would look like this:
double theta = Math.atan2(mouseY - playerY, mouseX - playerX);
dx = speed*Math.cos(theta);
dy = speed*Math.sin(theta);
playerX += dx;
playerY += dy;
This will work just fine as well and basically does the same thing, however, it involves these trig functions which are slow. In a small game it probably won’t matter though. If I were you, I would avoid using math that you haven’t learned about yet since you will not be able to fix any bugs that show up. Hope this helps! ;D