I'm making a game that is astonishingly akin to the classic arcade game "Berzerk", which is a shooter game. The player can fire in 8 directions, while the player's icon remains forward. I'm trying to get the firing action, but it gets messy and doesn't really work.
The move methods work fine. If I change the fire methods to text, it works as expected. When the actual bullets come into play, it's different. For example, if I hold shift and the up and right arrows, the bullet fires up, right, and diagonally. It's a mess, I know. Can this be sorted out better and actually work?
/**
* Check whether there are any key pressed and react to them. If shift is pressed, fire a laser. Else, move.
*/
public void checkKeys()
{
if(Greenfoot.isKeyDown("shift")) {
if (laserCount > 6) {
if(Greenfoot.isKeyDown ("up") && Greenfoot.isKeyDown("right")) {
fireUpRight();
} else {
if(Greenfoot.isKeyDown("up")) {
fireUp();
} else {
if(Greenfoot.isKeyDown("right")) {
fireRight();
}
}
}
if(Greenfoot.isKeyDown ("up") && Greenfoot.isKeyDown("left")) {
fireUpLeft();
} else {
if(Greenfoot.isKeyDown("left")) {
fireLeft();
} else {
if(Greenfoot.isKeyDown("up")) {
fireUp();
}
}
}
if(Greenfoot.isKeyDown ("down") && Greenfoot.isKeyDown("right")) {
fireDownRight();
} else {
if(Greenfoot.isKeyDown("right")) {
fireRight();
} else {
if(Greenfoot.isKeyDown("down")) {
fireDown();
}
}
}
if(Greenfoot.isKeyDown ("down") && Greenfoot.isKeyDown("left")) {
fireDownLeft();
} else {
if(Greenfoot.isKeyDown("left")) {
fireLeft();
} else {
if(Greenfoot.isKeyDown("down")) {
fireDown();
}
}
}
}
} else {
if(Greenfoot.isKeyDown("up")) {
moveUp();
}
if(Greenfoot.isKeyDown("down")) {
moveDown();
}
if(Greenfoot.isKeyDown("left")) {
moveLeft();
}
if(Greenfoot.isKeyDown("right")) {
moveRight();
}
}
}
//
//Move methods.
//
public void moveLeft() {
setRotation(0);
move(-1);
}
public void moveRight() {
setRotation(0);
move(1);
}
public void moveUp() {
setRotation(90);
move(-1);
}
public void moveDown() {
setRotation(90);
move(1);
}
//
//Fire methods.
//
public void fireLeft() {
getWorld().addObject(new Laser(180), getX(), getY());
}
public void fireRight() {
getWorld().addObject(new Laser(0), getX(), getY());
}
public void fireUp() {
getWorld().addObject(new Laser(-90), getX(), getY());
}
public void fireDown() {
getWorld().addObject(new Laser(90), getX(), getY());
}
public void fireUpRight() {
getWorld().addObject(new Laser(-45), getX(), getY());
}
public void fireUpLeft() {
getWorld().addObject(new Laser(-135), getX(), getY());
}
public void fireDownRight() {
getWorld().addObject(new Laser(45), getX(), getY());
}
public void fireDownLeft() {
getWorld().addObject(new Laser(135), getX(), getY());
}
