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 閱讀(336)
評論(0) 編輯 收藏