swing—如何在java中使用keylistener移动对象?

yi0zb3m4  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(231)

我想为我的钥匙(w,a,s,d)做物体(警察)移动。
当我在eclipse上运行这些代码时,没有任何错误,除了移动之外,它工作得很好。尽管我按了(w,a,s,d)键,对象(police)并没有React。
正如我所想的,在公共场合使用keylister似乎有问题
请帮帮我。谢谢您。

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class ChaseThief extends JFrame {
    private Image bufferImage;
    private Graphics screenGraphic;

    private Image background = new ImageIcon("src/images/background.jpg").getImage();
    private Image police = new ImageIcon("src/images/police.jpg").getImage();
    private Image thief = new ImageIcon("src/images/thief.png").getImage();

    private int policeX, policeY;
    private int policeWidth = police.getWidth(null);
    private int policeHeight = police.getWidth(null);
    private int thiefX, thiefY;
    private int thiefWidth = thief.getWidth(null);
    private int thiefHeight = thief.getWidth(null);

    private int score;

    private boolean up, down, left, right;
    private KeyListener listener;

    public ChaseThief() {
        setTitle("Game : 도둑잡기");

        setSize(800,600);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        start();
        setVisible(true);
        listener = new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
                switch(e.getKeyCode()) {
                case KeyEvent.VK_W:
                    up=true;
                    break;
                case KeyEvent.VK_S:
                    down=true;
                    break;
                case KeyEvent.VK_A:
                    left=true;
                    break;
                case KeyEvent.VK_D:
                    right=true;
                    break;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                switch(e.getKeyCode()) {
                case KeyEvent.VK_W:
                    up=false;
                    break;
                case KeyEvent.VK_S:
                    down=false;
                    break;
                case KeyEvent.VK_A:
                    left=false;
                    break;
                case KeyEvent.VK_D:
                    right=false;
                    break;
                }
            }

            @Override
            public void keyTyped(KeyEvent e) {
            }
        };
        while(true) {
            catchThief();
        }
    }

    public void start() {
        score = 0;
        policeX = (800-policeWidth)/2;
        policeY = (600-policeHeight)/2;
        thiefX=(int)(Math.random()*800);
        thiefY=(int)(Math.random()*600);
    }

    public void MovePolice() {
        if (up && policeY - 3 > 50) policeY -=3;
        if (down && policeY + policeHeight + 3 < 600) policeY+=3;
        if (left && policeX - 3 > 0) policeX-=3;
        if(right && policeX+policeWidth +3 < 800) policeX+=3;

    }

    public void catchThief() {
        if(policeX + policeWidth > thiefX && thiefX + thiefWidth > policeX && policeY + policeHeight > thiefY && thiefY + thiefHeight > policeY ) {
            score += 1;
            thiefX=(int)(Math.random()*800);
            thiefY=(int)(Math.random()*600);
        }
    }

    public void paint(Graphics g) {
        bufferImage = createImage(800,600);
        screenGraphic = bufferImage.getGraphics();
        draw(screenGraphic);
        g.drawImage(bufferImage, 0, 0, null);
    }

    public void draw(Graphics g) {
        g.drawImage(background,0,0,null);
        g.drawImage(police,policeX,policeY,null);
        g.drawImage(thief,thiefX,thiefY,null);
        g.setColor(Color.BLACK);
        g.setFont(new Font("Arial", Font.BOLD, 40));
        g.drawString("SCORE : "+score, 50, 80);
        this.repaint();
    }
    public static void main(String[] args) {
        new ChaseThief();
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题