This site requires JavaScript, please enable it in your browser!
Greenfoot back
EpicPumpkinSmash
EpicPumpkinSmash wrote ...

2014/11/25

How to fire a bullet in 8 directions?

EpicPumpkinSmash EpicPumpkinSmash

2014/11/25

#
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.
/**
     * 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());
    }
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?
davmac davmac

2014/11/25

#
I think your logic for choosing a direction (line 09 onwards) is wrong, mostly because you don't use 'else' where you should, and your conditions are confused (for want of a better word). You need to have each direction handled once somewhere in the code. It should probably look something like:
if(Greenfoot.isKeyDown("up")) {
    // Three possible directions: up-left, up, up-right
    if (Greenfoot.isKeyDown("right")) {
        fireUpRight();
    }
    else if (Greenfoot.isKeyDown("left")) {
        fireUpLeft();
    }
    else {
        fireUp();
    }
}
else if (Greenfoot.isKeyDown("down")) {
    // Three possible directions: down-left, down, down-right
    if (Greenfoot.isKeyDown("right")) {
        fireDownRight();
    }
    else if (Greenfoot.isKeyDown("left")) {
        fireDownLeft();
    }
    else {
        fireDown();
    }
}
else if (Greenfoot.isKeyDown("left")) {
    // down-left and up-left are already handled
    fireLeft();
}
else if (Greenfoot.isKeyDown("right")) {
    // down-right and up-right are already handled
    fireRight();
}
EpicPumpkinSmash EpicPumpkinSmash

2014/11/26

#
Wow, thanks so much. I knew there must be a better way of organizing this, but I couldn't think of one. Thanks for pointing that out.
You need to login to post a reply.