How to move a Bug in GridWorld with arrow keys

ぐ巨炮叔叔 提交于 2019-12-21 20:58:52

问题


I'm making a game for my computer Science class and I am trying to move a character object that extends Bug with the arrow keys. Should I put the code to move with the arrow keys in the Character class or in the World class? And what should the code look like? Right now I've got this code in the Character class and it complies fine, but when I try to to run it in the grid nothing happens when I press the arrow keys.

 public class Character extends Bug
{
Random pokemon;
public Character()
{

}

public void act(KeyEvent e)
{
        move(e);
        pokemon = new Random();
        if(pokemon.nextInt(10) == 5)
            System.out.println("It works!!");
}

public void move(KeyEvent e)
{
    Grid<Actor> gr = getGrid();
    Location loc = getLocation();
    if(gr == null)
        return;
    if( e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        if(!(getDirection() == 90))
            setDirection(90);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_LEFT)
    {
        if(!(getDirection() == 270))
            setDirection(270);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_UP)
    {
        if(!(getDirection() == 0))
            setDirection(0);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_DOWN)
    {
        if(!(getDirection() == 180))
            setDirection(180);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }

public class Character extends Bug
{
Random pokemon;
public Character()
{

}

public void act(KeyEvent e)
{
        move(e);
        pokemon = new Random();
        if(pokemon.nextInt(10) == 5)
            System.out.println("It works!!");
}

public void move(KeyEvent e)
{
    Grid<Actor> gr = getGrid();
    Location loc = getLocation();
    if(gr == null)
        return;
    if( e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        if(!(getDirection() == 90))
            setDirection(90);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_LEFT)
    {
        if(!(getDirection() == 270))
            setDirection(270);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_UP)
    {
        if(!(getDirection() == 0))
            setDirection(0);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_DOWN)
    {
        if(!(getDirection() == 180))
            setDirection(180);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }

Is this code correct for a KeyEvent and how can I call on this code from the World class? Any help would be greatly appreciated!


回答1:


In the ActorWorld class there is a method boolean keyPressed(String description, Location loc) this method is the for the sole purpose of being overridden in a subclass. description is the KeyStroke in the format found here, and loc is the Location that the cursor was on when the key was pressed. (Although in your case it doesn't matter)

So in short, you should extend KeyPressed in a Custom CharacterWorld extends ActorWorld class.



来源:https://stackoverflow.com/questions/15671466/how-to-move-a-bug-in-gridworld-with-arrow-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!