<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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:

    譯:我們的下一個(gè)步驟是使飛船可控。為了做到這一點(diǎn),我們需要我們的主游戲類中添加一個(gè)簡單的內(nèi)部類。它看起來像這樣:

    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)

    譯:這個(gè)監(jiān)聽按鍵的按下和釋放通過主類中一組boolean類型的變量記錄對(duì)應(yīng)按鍵的狀態(tài)(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畫布,其實(shí)就是Game類)通過Game構(gòu)造函數(shù)添加下面一代碼來實(shí)現(xiàn)

    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類的構(gòu)造函數(shù)中后,當(dāng)按鍵被按下時(shí),對(duì)應(yīng)的boolean類型的狀態(tài)標(biāo)識(shí)變量將被設(shè)置合適地值。下一步要做的在游戲運(yùn)行過程中實(shí)時(shí)響應(yīng)這些布爾標(biāo)志設(shè)置如果我們?cè)谟螒蛑刑砑?/span>了這一功能,我們為玩家飛船添加鍵盤控制:

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

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

    // update the movement appropraitely  實(shí)現(xiàn)飛船的運(yùn)動(dòng)。開始時(shí)飛船不移動(dòng)。如果任何一個(gè)方向鍵被按下,那么非常就向這個(gè)方向運(yùn)動(dòng)

    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.

    譯:正如你看到的,我們檢查建還是建被按下。如果是左鍵按下,我們新水平移動(dòng)速度為負(fù)值,相反,為正值。因此,當(dāng)對(duì)應(yīng)的代碼被執(zhí)行,飛船將向左或右移動(dòng)

    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()"

    譯:如果玩家按下開火鍵我們就要使飛船向外星人發(fā)射一顆子彈。但是,我們并不想讓玩家的飛船一直保持開火的狀態(tài)。限制飛船每隔一定時(shí)間開一次火是個(gè)不錯(cuò)的主意。我們把這項(xiàng)功能交給一個(gè)叫“tryToFire”的實(shí)用方法來實(shí)現(xiàn)。

    // if we're pressing fire, attempt to fire 如果我們按下開發(fā)鍵,嘗試開火

    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:

    譯:為了阻止玩家過于頻繁的射擊,我們將記錄射擊時(shí)的時(shí)間,如果間隔時(shí)間少于規(guī)定間隔時(shí)間就不執(zhí)行相應(yīng)的射擊操作。恩,這聽起來很復(fù)雜,其實(shí)不是這樣的。下面就是實(shí)現(xiàn)源代碼:

    public void tryToFire() {

    // check that we have waiting long enough to fire 檢查我們是否已經(jīng)等待了足夠長的時(shí)間了

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

    return;

    }

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

    //如果等候了足夠的時(shí)間,創(chuàng)建子彈實(shí)體,并記錄時(shí)間

    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.

    譯:首先,我們檢查距離上次玩家射擊的時(shí)間是否足夠長了,如果小于規(guī)定的間隔時(shí)間就不執(zhí)行射擊的操作,并直接返回;反之,我們記錄當(dāng)前時(shí)間。下一步,創(chuàng)建并添加一個(gè)實(shí)體來表示一顆玩家發(fā)射的子彈。于是在屏幕上,我們的子彈由玩家飛船發(fā)射,射向外星人。

    學(xué)軟件開發(fā),到蜂鳥科技!
    超強(qiáng)的師資力量 、完善的課程體系 、超低的培訓(xùn)價(jià)格 、真實(shí)的企業(yè)項(xiàng)目。

    網(wǎng)址:www.ntcsoft.com 
    電話:0371-63839606 
    鄭州軟件開發(fā)興趣小組群:38236716

    posted on 2010-11-25 23:59 whistler 閱讀(346) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    留言簿(2)

    我參與的團(tuán)隊(duì)

    文章檔案(22)

    搜索

    •  

    最新評(píng)論

    主站蜘蛛池模板: 亚洲黄色免费网站| 国产视频精品免费视频| 日韩人妻无码精品久久免费一| 亚洲国产成人精品女人久久久 | 精品国产人成亚洲区| 精品亚洲福利一区二区| 日本牲交大片免费观看| 亚洲最大的成人网| 看全色黄大色大片免费久久 | gogo免费在线观看| 国产成人亚洲精品91专区手机| 一级做a爱过程免费视| 亚洲国产午夜中文字幕精品黄网站| 在线播放免费人成视频网站| 伊在人亚洲香蕉精品区麻豆| 污视频网站免费观看| jlzzjlzz亚洲乱熟在线播放| 成在人线av无码免费高潮喷水| 亚洲国产一区国产亚洲| 99久久免费看国产精品| 亚洲人成网站色在线观看| 日韩一区二区在线免费观看| 免费人成在线观看播放a| 亚洲一区二区三区在线观看精品中文 | 亚洲高清偷拍一区二区三区| 五月天婷婷免费视频| 亚洲成AV人片一区二区| 91短视频在线免费观看| 亚洲中文字幕一二三四区苍井空| 女人18特级一级毛片免费视频| 久久亚洲色WWW成人欧美| 亚洲中文字幕在线第六区| 免费国产污网站在线观看15 | 老司机亚洲精品影视www| 91精品国产免费| 相泽南亚洲一区二区在线播放| 久久久久无码专区亚洲av| 四虎在线成人免费网站| 猫咪www免费人成网站| 亚洲久本草在线中文字幕| 国产精品另类激情久久久免费|