2013-12-08 39 views
0

我希望用戶能夠選擇按Enter鍵開始遊戲。在keyPressed方法中,我已將其設置爲盡我所知。但是,當我運行程序時,角色已經在屏幕上移動,按下回車鍵只會加快速度。如果有人能讓我知道哪裏出了問題,並指出我朝着正確的方向,我將非常感激!謝謝。使用KeyListener開始我的遊戲需要幫助

代碼:

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ZombieAttackMain extends Applet implements Runnable, KeyListener, ActionListener{ 

    private Image background; 

    Player p; 
    NormalZombie nz; 
    FastZombie fz; 
    TankyZombie tz; 
    Cow c1; 
    Cow c2; 
    Cow c3; 
    Cow c4; 
    Bullet b; 

    private Graphics bufferGraphics; 
    private Image offScreen; 

    public final static int POSITION_1_Y = 100; 
    public final static int POSITION_2_Y = 255; 
    public final static int POSITION_3_Y = 400; 
    public final static int POSITION_4_Y = 550; 

    private boolean running = true; 

    public void init(){ 
     setSize(1100, 700); 
     background = getImage(getCodeBase(), "Background.jpg"); 
     p.player = getImage(getCodeBase(), "Player.png"); 
     nz.normalZombie = getImage(getCodeBase(), "NormalZombie.png"); 
     fz.fastZombie = getImage(getCodeBase(), "FastZombie.png"); 
     tz.tankyZombie = getImage(getCodeBase(), "TankyZombie.png"); 
     c1.cow = getImage(getCodeBase(), "Cow.png"); 
     c2.cow = getImage(getCodeBase(), "Cow.png"); 
     c3.cow = getImage(getCodeBase(), "Cow.png"); 
     c4.cow = getImage(getCodeBase(), "Cow.png"); 
     b.bullet = getImage(getCodeBase(), "Bullet.png"); 
     addKeyListener(this); 
    } 

    public void start() { 
     p = new Player(); 
     nz = new NormalZombie(); 
     fz = new FastZombie(); 
     tz = new TankyZombie(); 
     c1 = new Cow(); 
     c2 = new Cow(); 
     c3 = new Cow(); 
     c4 = new Cow(); 
     b = new Bullet(); 
     Thread thread = new Thread(this); 
     thread.start(); 
    } 

    public void run() { 
     while(running){ 
      if(nz.getNormalZombieXPosition() <= b.getBulletXPosition() && nz.getNormalZombieYPosition() >= b.getBulletYPosition() - 85 
        && nz.getNormalZombieYPosition() <= b.getBulletYPosition() + 85){ 
       nz.setShowNormalZombie(false); 
       nz.setNormalZombieXPosition(1200); 
       b.setShowBullet(false); 
       b.setBulletXPosition(1200); 
      } 
      if(fz.getFastZombieXPosition() <= b.getBulletXPosition() && fz.getFastZombieYPosition() >= b.getBulletYPosition() - 85 
        && fz.getFastZombieYPosition() <= b.getBulletYPosition() + 85){ 
       fz.setShowFastZombie(false); 
       fz.setFastZombieXPosition(1200); 
       b.setShowBullet(false); 
       b.setBulletXPosition(-500); 
      } 
      if(tz.getTankyZombieHP() == 2 && tz.getTankyZombieXPosition() <= b.getBulletXPosition() && tz.getTankyZombieYPosition() 
        >= b.getBulletYPosition() - 85 && tz.getTankyZombieYPosition() <= b.getBulletYPosition() + 85){ 
       b.setBulletXPosition(-500); 
       b.setShowBullet(false); 
       tz.setTankyZombieHP(1); 
      } 
      if(tz.getTankyZombieHP() == 1 && tz.getTankyZombieXPosition() <= b.getBulletXPosition() && tz.getTankyZombieYPosition() 
        >= b.getBulletYPosition() - 85 && tz.getTankyZombieYPosition() <= b.getBulletYPosition() + 85){ 
       b.setShowBullet(false); 
       b.setBulletXPosition(-500); 
       tz.setShowTankyZombie(false); 
       tz.setTankyZombieXPosition(1200); 
      } 

      if(nz.getNormalZombieXPosition() <= c1.getCowXPosition() + 190 && nz.getNormalZombieYPosition() == POSITION_1_Y){ 
       c1.setShowCow1(false); 
      } 
      if(fz.getFastZombieXPosition() <= c1.getCowXPosition() + 190 && fz.getFastZombieYPosition() == POSITION_1_Y){ 
       c1.setShowCow1(false); 
      } 
      if(tz.getTankyZombieXPosition() <= c1.getCowXPosition() + 190 && tz.getTankyZombieYPosition() == POSITION_1_Y){ 
       c1.setShowCow1(false); 
      } 
      if(nz.getNormalZombieXPosition() <= c2.getCowXPosition() + 190 && nz.getNormalZombieYPosition() == POSITION_2_Y){ 
       c2.setShowCow2(false); 
      } 
      if(fz.getFastZombieXPosition() <= c2.getCowXPosition() + 190 && fz.getFastZombieYPosition() == POSITION_2_Y){ 
       c2.setShowCow2(false); 
      } 
      if(tz.getTankyZombieXPosition() <= c2.getCowXPosition() + 190 && tz.getTankyZombieYPosition() == POSITION_2_Y){ 
       c2.setShowCow2(false); 
      }   
     if(nz.getNormalZombieXPosition() <= c3.getCowXPosition() + 190 && nz.getNormalZombieYPosition() == POSITION_3_Y){ 
       c3.setShowCow3(false); 
      } 
      if(fz.getFastZombieXPosition() <= c3.getCowXPosition() + 190 && fz.getFastZombieYPosition() == POSITION_3_Y){ 
       c3.setShowCow3(false); 
      } 
      if(tz.getTankyZombieXPosition() <= c3.getCowXPosition() + 190 && tz.getTankyZombieYPosition() == POSITION_3_Y){ 
       c3.setShowCow3(false); 
      } 
      if(nz.getNormalZombieXPosition() <= c4.getCowXPosition() + 190 && nz.getNormalZombieYPosition() == POSITION_4_Y){ 
       c4.setShowCow4(false); 
      } 
      if(fz.getFastZombieXPosition() <= c4.getCowXPosition() + 190 && fz.getFastZombieYPosition() == POSITION_4_Y){ 
       c4.setShowCow4(false); 
      } 
      if(tz.getTankyZombieXPosition() <= c4.getCowXPosition() + 190 && tz.getTankyZombieYPosition() == POSITION_4_Y){ 
       c4.setShowCow4(false); 
      } 

      repaint(); 
      try { 
       Thread.sleep(17); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void paint(Graphics g){ 
     g.drawImage(background,0,0,null); 
     p.paint(g); 
     if(nz.getShowNormalZombie() == true){ 
      nz.paint(g); 
      nz.setNormalZombieXPosition(nz.getNormalZombieXPosition() + nz.getNormalZombieDx()); 
     } 
     if(fz.getShowFastZombie() == true){ 
      fz.paint(g); 
      fz.setFastZombieXPosition(fz.getFastZombieXPosition() + fz.getFastZombieDx()); 
     } 
     if(tz.getShowTankyZombie() == true){ 
      tz.paint(g); 
      tz.setTankyZombieXPosition(tz.getTankyZombieXPosition() +  tz.getTankyZombieDx()); 
     } 
     if(c1.getShowCow1() == true){ 
      c1.paint(g); 
      c1.setCowYPosition(POSITION_1_Y); 
     } 
     if(c2.getShowCow2() == true){ 
      c2.paint(g); 
      c2.setCowYPosition(POSITION_2_Y); 
     } 
     if(c3.getShowCow3() == true){ 
      c3.paint(g); 
      c3.setCowYPosition(POSITION_3_Y); 
     } 
     if(c4.getShowCow4() == true){ 
      c4.paint(g); 
      c4.setCowYPosition(POSITION_4_Y); 
     } 
     if(b.getShowBullet()){ 
      b.paint(g); 
      b.setBulletXPosition(b.getBulletXPosition() + b.getBulletDX()); 
      if(b.getBulletXPosition() > 1100){ 
       b.setShowBullet(false); 
      } 
     } 
    } 

    public void keyPressed(KeyEvent e) { 
     switch(e.getKeyCode()){ 
     case KeyEvent.VK_UP: 
      p.moveUp(p); 
      break; 
     case KeyEvent.VK_DOWN: 
      p.moveDown(p); 
      break; 
     case KeyEvent.VK_SPACE: 
      b.spawnBullet(); 
      break; 
     case KeyEvent.VK_ENTER: 
      start(); 
      break; 
     } 
    } 

    public void update(Graphics g){ 
     if(offScreen == null){ 
      offScreen = createImage(this.getWidth(), this.getHeight()); 
      bufferGraphics = offScreen.getGraphics(); 
     } 
     bufferGraphics.setColor(getBackground()); 
     bufferGraphics.fillRect(0,0,this.getWidth(),this.getHeight()); 
     bufferGraphics.setColor(getForeground()); 
     paint(bufferGraphics); 
     g.drawImage(offScreen,0,0,this); 
    } 

    public static int getPosition1Y() { 
     return POSITION_1_Y; 
    } 

    public static int getPosition2Y() { 
     return POSITION_2_Y; 
    } 

    public static int getPosition3Y() { 
     return POSITION_3_Y; 
    } 

    public static int getPosition4Y() { 
     return POSITION_4_Y; 
    } 

    public void stop() { 
     running = false; 
    } 

    public void keyReleased(KeyEvent e) { 
    } 
    public void keyTyped(KeyEvent e) { 
    } 
    public void actionPerformed(ActionEvent arg0) {  
    } 
} 

回答

1

然而,當我運行的字符在屏幕上已經移動的程序,然後按Enter只有加快起來

的start()方法是當它被裝載的小應用程序調用,所以你遊戲線程自動啓動。

按只輸入速度起來

您再次調用start方法,所以現在你必須運行兩個線程遊戲。

例如,創建一個名爲startGame()的方法。將代碼從start()移到該方法中。然後在按Enter鍵時調用startGame()。你也希望你的代碼確保遊戲目前沒有運行,所以你不會開始遊戲兩次。

+0

感謝您的迴應!我按照你的建議做了,但我不確定如何確保遊戲尚未運行。現在也有一個錯誤消息: – Eric

+0

的錯誤信息是:在sun.awt.RepaintArea異常在線程 「AWT-EventQueue的-1」 顯示java.lang.NullPointerException \t在ZombieAttackMain.paint(ZombieAttackMain.java:144) \t .paintComponent(未知來源) \t在sun.awt.RepaintArea.paint(未知來源) \t在sun.awt.windows.WComponentPeer.handleEvent(未知來源) \t在java.awt.Component.dispatchEventImpl(未知來源) – Eric

+0

只是在開玩笑!它完美的工作,但我仍然收到錯誤信息?對不起,我對編程相當陌生,不太瞭解:P再次感謝您的幫助! – Eric

0

嗯...我不是100%肯定,但它可能與變量 「暗戰」 的事情。嘗試將其設置爲false,然後在用戶點擊「enter」時將其設置爲true。

+0

好主意,可以發誓,可以工作,但現在它只是開始一個黑屏:P感謝您的建議! – Eric