posts - 0,  comments - 6,  trackbacks - 0

Keyboard Input 键盘输入

Our next step is to make the ship controllable. To do this we need to add a simple inner class to our main game. It looks like this:

译:我们的下一个步骤是使飞船可控。为了做到这一点,我们需要我们的主游戏类中添加一个简单的内部类。它看起来像这样:

private class KeyInputHandler extends KeyAdapter {

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_LEFT) {

leftPressed = true;

}

if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

rightPressed = true;

}

if (e.getKeyCode() == KeyEvent.VK_SPACE) {

firePressed = true;

}

public void keyReleased(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_LEFT) {

leftPressed = false;

}

if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

rightPressed = false;

}

if (e.getKeyCode() == KeyEvent.VK_SPACE) {

firePressed = false;

}

}

public void keyTyped(KeyEvent e) {

// if we hit escape, then quit the game

if (e.getKeyChar() == 27) {

System.exit(0);

}

}

}

This class simply picks up keys being pressed and released and records their states in a set of boolean variables in the main class. In addition it checks for escape being pressed (which in our case exits the game)

译:这个监听按键的按下和释放通过主类中一组boolean类型的变量记录对应按键的状态(true按下,false释放)。此外,它还检查esc按钮是否被按下(在本例中退出游戏)

To make this class work we need to add it to our canvas as a "KeyListener". This is done by adding the following line to the constructor of our Game class:

译:为了使这一类工作,我们需要将它作为 “KeyListener” 添加到我们的 canvas画布,其实就是Game类)通过Game构造函数添加下面一代码来实现

addKeyListener(new KeyInputHandler());

With this source code in place the boolean flags will be set to appropriate values as the keys are pressed. The next step is to respond to these boolean flags being set in the game loop. If we add this in the game loop we can add keyboard control to the ship:

译:上面的代码添加到Game类的构造函数中后,按键被按下时,对应的boolean类型的状态标识变量将被设置合适地值。下一步要做的在游戏运行过程中实时响应这些布尔标志设置如果我们在游戏中添加了这一功能,我们为玩家飞船添加键盘控制:

// resolve the movement of the ship. First assume the ship 

// isn't moving. If either cursor key is pressed then

// update the movement appropraitely  实现飞船的运动。开始时飞船不移动。如果任何一个方向键被按下,那么非常就向这个方向运动

ship.setHorizontalMovement(0);

if ((leftPressed) && (!rightPressed)) {

ship.setHorizontalMovement(-moveSpeed);

} else if ((rightPressed) && (!leftPressed)) {

ship.setHorizontalMovement(moveSpeed);

}

As you can see we check if left or right is pressed. If they are we update the movement values associated with the player's ship. Hence when we hit the entity movement code the ship will move left or right.

译:正如你看到的,我们检查建还是建被按下。如果是左键按下,我们新水平移动速度为负值,相反,为正值。因此,当对应的代码被执行,飞船将向左或右移动

If the player holds the fire key we'd like the ship to fire a shot up towards the aliens. However, we don't want to let the player just keep firing. It'd be good to limit how often they can fire. We'll put this functionality in a utility method called (somewhat logically) "tryToFire()"

译:如果玩家按下开火键我们就要使飞船向外星人发射一颗子弹。但是,我们并不想让玩家的飞船一直保持开火的状态。限制飞船每隔一定时间开一次火是个不错的主意。我们把这项功能交给一个叫“tryToFire”的实用方法来实现。

// if we're pressing fire, attempt to fire 如果我们按下开发键,尝试开火

if (firePressed) {

tryToFire();

}

To prevent the player firing too often we'll record the time whenever they take a shot and prevent them taking a shot if the last shot was less than a set interval ago. Hmm, sounds complicated, its not! really! Here it is:

译:为了阻止玩家过于频繁的射击,我们将记录射击时的时间,如果间隔时间少于规定间隔时间就不执行相应的射击操作。恩,这听起来很复杂,其实不是这样的。下面就是实现源代码:

public void tryToFire() {

// check that we have waiting long enough to fire 检查我们是否已经等待了足够长的时间了

if (System.currentTimeMillis() - lastFire < firingInterval) {

return;

}

// if we waited long enough, create the shot entity, and record the time.

//如果等候了足够的时间,创建子弹实体,并记录时间

lastFire = System.currentTimeMillis();

ShotEntity shot = new ShotEntity(this,"sprites/shot.gif",ship.getX()+10,ship.getY()-30);

entities.add(shot);

}

First we check if the last time the player took a shot was long enough ago. If it wasn't we don't bother firing and just return. If it was we record the current time. Next we create and add an entity to represent the shot fired by the player. Since our shot is setup to move up the screen it shoots off from the player towards the aliens.

译:首先,我们检查距离上次玩家射击的时间是否足够长了,如果小于规定的间隔时间就不执行射击的操作,并直接返回;反之,我们记录当前时间。下一步,创建并添加一个实体来表示一颗玩家发射的子弹。于是在屏幕上,我们的子弹由玩家飞船发射,射向外星人。

学软件开发,到蜂鸟科技!
超强的师资力量 、完善的课程体系 、超低的培训价格 、真实的企业项目。

网址:www.ntcsoft.com 
电话:0371-63839606 
郑州软件开发兴趣小组群:38236716

posted on 2010-11-25 23:59 whistler 阅读(313) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

留言簿(2)

我参与的团队

文章档案(22)

搜索

  •  

最新评论