如何使用箭头键移动GridWorld中的错误 [英] How to move a Bug in GridWorld with arrow keys
问题描述
我正在为我的计算机科学课制作一个游戏,并且我试图移动一个用箭头键扩展Bug的角色对象.是否应该在Character类或World类中使用箭头键移动代码?代码应该是什么样的?现在,我已经在Character类中获得了这段代码,它可以很好地运行,但是当我尝试在网格中运行它时,按箭头键没有任何反应.
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();
}
}
此代码对KeyEvent正确吗,如何从World类中调用此代码? 任何帮助将不胜感激!
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!
推荐答案
在ActorWorld
类中,有一个方法boolean keyPressed(String description, Location loc)
,该方法仅出于在子类中被覆盖的目的. description
是KeyStroke
,其格式为
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)
因此,简而言之,您应该在自定义CharacterWorld extends ActorWorld
类中扩展KeyPressed
.
So in short, you should extend KeyPressed
in a Custom CharacterWorld extends ActorWorld
class.
这篇关于如何使用箭头键移动GridWorld中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!