如何使用箭头键在GridWorld中移动Bug

时间:2013-03-27 23:24:31

标签: keyevent arrow-keys gridworld

我正在为我的计算机科学课制作游戏,我正在尝试使用箭头键移动扩展Bug的角色对象。我应该使用Character类或World类中的箭头键来移动代码吗?代码应该是什么样的?现在我在Character类中得到了这个代码并且它符合要求,但是当我尝试在网格中运行它时,当我按下箭头键时没有任何反应。

 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();
        }
    }

这个代码对于KeyEvent是否正确?我如何从World类调用此代码? 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

ActorWorld类中有一个方法boolean keyPressed(String description, Location loc)此方法的唯一目的是在子类中重写。 description是找到here格式的KeyStroke,而loc是按下该键时光标所在的Location。 (虽然在你的情况下没关系)

简而言之,您应该在自定义KeyPressed类中扩展CharacterWorld extends ActorWorld