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

标签 java applet keylistener

我希望用户能够通过按 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) {     
    }
}

最佳答案

However when I run the program the characters are already moving across the screen and pressing enter only speeds them up

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

pressing enter only speeds them up

您再次调用 start 方法,因此现在有两个游戏线程正在运行。

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

关于java - 需要帮助使用 KeyListener 来开始我的游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20460284/

相关文章:

java - 如何检查 Twitter 用户声明的位置是否存在?

java - Scene2D Actor 在本应不可见的情况下在屏幕上闪烁 1 帧

java - 在浏览器(chrome/firefox)中运行本地java小程序 "Your security settings have blocked a local application from running"

javascript - Java Applet 未定义

java - 简单的关键监听器

Java 按键事件

java - 如何使用 JIIC 生成 ISO

java - 是否将 war 部署到 Nexus 存储库

java - 在java中使用ActionListener

java - 在类中创建新的 KeyListener 时出现问题