需要帮助使用KeyListener开始我的游戏

时间:2013-12-08 22:55:54

标签: java applet keylistener

我希望用户能够通过按Enter键选择开始游戏。在keyPressed方法中,我已经根据我的知识进行了设置。但是,当我运行程序时,字符已经在屏幕上移动,按下输入只会加速它们。如果有人能让我知道出了什么问题,并指出我正确的方向,我将非常感激!感谢。

代码:

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) {     
    }
}

2 个答案:

答案 0 :(得分:1)

  

然而,当我运行该程序时,字符已经在屏幕上移动并且按下输入仅加速它们

applet在加载时调用start()方法,因此你的游戏Thread会自动启动。

  

按Enter键仅加快速度

你再次调用start方法,所以现在你有两个游戏线程正在运行。

例如,创建一个名为startGame()的方法。将代码从start()移动到该方法中。然后在按下Enter键时调用startGame()。您还希望您的代码确保游戏当前没有运行,因此您不会两次启动游戏。

答案 1 :(得分:0)

嗯......我不是百分百肯定,但它可能与变量“Running”有关。尝试将其设置为false,然后在用户点击“enter”时将其设置为true。