<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è)簡(jiǎn)單的內(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)等待了足夠長(zhǎ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í)間是否足夠長(zhǎng)了,如果小于規(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 閱讀(336) 評(píng)論(0)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(2)

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

    文章檔案(22)

    搜索

    •  

    最新評(píng)論

    主站蜘蛛池模板: 亚洲爆乳无码一区二区三区| 亚洲不卡中文字幕| 日本免费电影一区二区| 精品福利一区二区三区免费视频| 亚洲国产成人精品激情| 97视频热人人精品免费| 免费人成再在线观看网站| 亚洲v高清理论电影| 国产精品无码一二区免费| 免费91最新地址永久入口 | 亚洲性日韩精品一区二区三区| 久久黄色免费网站| 亚洲.国产.欧美一区二区三区| 国产在线观看免费视频播放器| 永久免费A∨片在线观看| 亚洲AV无码AV男人的天堂不卡| 亚洲AV日韩AV天堂久久| 免费A级毛片无码久久版| 免费观看激色视频网站(性色)| 国产99精品一区二区三区免费 | 免费一区二区无码视频在线播放| 亚洲视频在线观看视频| 精品国产亚洲男女在线线电影 | 日本免费人成黄页在线观看视频 | AV在线播放日韩亚洲欧| 国产一精品一AV一免费孕妇| 视频免费在线观看| 国产精品亚洲专区一区| 亚洲婷婷综合色高清在线| 国产亚洲自拍一区| 国产亚洲福利一区二区免费看| 亚洲高清中文字幕免费| 少妇性饥渴无码A区免费| 黄床大片30分钟免费看| 亚洲私人无码综合久久网| 亚洲大香伊人蕉在人依线| 亚洲av永久无码精品古装片| 亚洲精品一级无码鲁丝片 | 中文字幕无码精品亚洲资源网久久 | 一级毛片免费播放男男| 亚洲av永久无码天堂网|