我正在做一个游戏,我的计算机科学类,我试图移动与箭头键扩展错误的字符对象。 我应该把代码与Character类或世界一流的箭头键移动? 又该代码样子的? 现在我已经得到了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的,我怎么能叫上距离世界级的代码? 任何帮助将不胜感激!