【问题标题】:Need help using a KeyListener to begin my game需要帮助使用 KeyListener 来开始我的游戏
【发布时间】:2013-12-25 22:50:56
【问题描述】:

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

代码:

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

【问题讨论】:

    标签: java applet keylistener


    【解决方案1】:

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

    【讨论】:

    • 好主意,可以发誓这会起作用,但现在它只是开始一个空白屏幕:P 感谢您的建议!
    【解决方案2】:

    但是,当我运行程序时,字符已经在屏幕上移动,按 Enter 只会加快它们的速度

    小程序加载时会调用 start() 方法,因此您的游戏线程会自动启动。

    按回车键只会加快速度

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

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

    【讨论】:

    • 感谢您的回复!我按照你的建议做了,但我不确定如何确保游戏尚未运行。现在还有一条错误消息:
    • 错误消息是:sun.awt.RepaintArea.paintComponent(Unknown Source) 处 ZombieAttackMain.paint(ZombieAttackMain.java:144) 线程“AWT-EventQueue-1”中的异常 java.lang.NullPointerException ) 在 sun.awt.RepaintArea.paint(Unknown Source) at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source)
    • 开个玩笑!它工作得很好,但我仍然收到错误消息?抱歉,我对编程很陌生,我不太了解:P 再次感谢您的帮助!
    • 我不知道第 144 行是什么。您需要检查变量以查看哪个为空。我猜小程序第一次绘制时没有初始化某些东西,因为你不再自动启动游戏了。
    • 第 144 行是 p.paint(g);它只是绘制 Player 类的一个实例。我所有的类都有一个paint() 方法,每个类都绘制它各自的变量,并且它们在同一个地方都有null。例如: public void paint(Graphics g){ g.drawImage(player,PLAYER_X_POSITION,playerYPosition, null); }
    猜你喜欢
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2013-04-02
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多