Uh, I have no direct solution for this, but usually I just work with vectors:
// Very simplified class, if you use LWJGL / LibGDX use their Vector classes
public class Vec2 {
// Make them immutable or not, it's up to you
public final float x, y;
public Vec2(float x, float y) {
this.x = x;
this.y = y;
}
// These are all the methods we need ^^
public float length() {
return Math.sqrt(x * x + y * y);
}
public Vec2 div(float v) {
return new Vec2(x / v, y / v);
}
public Vec2 mul(float v) {
return new Vec2(x * v, y * v);
}
public Vec2 normalize() {
return div(length());
}
}
So now that we have your Vec class, let’s implement the player logic:
float speed = 4f;
Vec2 velocity = new Vec2(0, 0);
// in game logic:
velocity = new Vec2(0, 0); // reset velocity
if (/*pressed left*/) {
velocity = new Vec2(velocity.x - 1, velocity.y);
}
if (/*pressed right*/) {
velocity = new Vec2(velocity.x + 1, velocity.y);
}
if (/*pressed up*/) {
velocity = new Vec2(velocity.x, velocity.y - 1); // plus or minus depending on y up or y down
}
if (/*pressed down*/) {
velocity = new Vec2(velocity.x, velocity.y + 1);
}
velocity = velocity.normalize().mul(speed);
// The velocity vector is always going to have the length "speed".
// Normalize makes the vector have the length 1, mul() does the obvious with the length.
x += velocity.x;
y += velocity.y;
// done.