简体   繁体   English

在使用KeyListener进行游戏时需要帮助

[英]Need help using a KeyListener to begin my game

I would like the user to be able when to choose to start the game by pressing enter. 我希望用户能够在何时选择通过按Enter键开始游戏。 In the keyPressed method, I've set it up to the best of my knowledge. 在keyPressed方法中,我已尽其所能对其进行了设置。 However when I run the program the characters are already moving across the screen and pressing enter only speeds them up. 但是,当我运行程序时,字符已经在屏幕上移动,按Enter只能加快它们的速度。 If anyone could let me know what is going wrong and point me in the right direction I would be so grateful! 如果有人能让我知道出了什么问题并指出正确的方向,我将非常感激! Thanks. 谢谢。

Code: 码:

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 但是,当我运行程序时,字符已经在屏幕上移动,按Enter只能加快它们的速度

The start() method is invoked by the applet when it is loaded, so your game Thread starts automatically. 小应用程序在加载时将调用start()方法,因此您的游戏线程将自动启动。

pressing enter only speeds them up 按下Enter只会加快速度

You invoke the start method again, so now you have two game Threads running. 您再次调用start方法,因此现在您正在运行两个游戏线程。

Create a method called startGame() for example. 例如,创建一个名为startGame()的方法。 Move the code from start() into that method. 将代码从start()移到该方法中。 Then invoke startGame() when the Enter key is pressed. 然后在按下Enter键时调用startGame()。 You would also want your code to make sure the game is currently not running so you don't start the game twice. 您还希望您的代码确保游戏当前未在运行,因此您不必两次启动游戏。

Hmmm... I'm not 100% sure, but it may have to do with the variable "Running". 嗯...我不确定100%,但这可能与变量“运行”有关。 Try setting that to false, and then upon the user hitting "enter" set it to true. 尝试将其设置为false,然后在用户单击“ enter”时将其设置为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM