-3

(游戏还没有完成,但是)使用键盘输入并没有做任何事情,我相信这是由于 KeyListener 没有正确定义。这是我的代码:mainClass:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;


public class mainClass extends JComponent{
    private static ballAndGameplay ball;
    private static calculateDrawPos blocks;

    public static void main(String[] a) {
        JFrame window = new JFrame("PONG");
        window.setSize(1280,720);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainClass mc = new mainClass();
        window.getContentPane().add(new mainClass());
        window.setVisible(true);
        ball = new ballAndGameplay(630,300,5,5);
        blocks = new calculateDrawPos(false,false,false,false);
        mc.addKeyListener(blocks);
        while (true){
            blocks.moveBlocks();
            ball.moveBall();
            mc.removeAll();
            mc.paint(window.getGraphics());
            try{
                Thread.sleep(10);
            }
            catch (Exception e){

            }
        }
    }
    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect (-10, -10, 1300, 740); 
        g.setColor(Color.WHITE);
        g.fillRect (10, blocks.PlayerOneY, 25, 125);  //left player
        g.fillRect (1230, blocks.PlayerTwoY, 25, 125);  //right player
        g.fillRect (638, -10, 4, 740); //middle line
        g.fillRect (ball.ballX, ball.ballY, 20, 20);  //ball
    }

}

计算绘制位置:

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class calculateDrawPos extends KeyAdapter implements KeyListener {
    int PlayerOneY=275;
    int PlayerTwoY=275;
    boolean wPressed;
    boolean sPressed;
    boolean upPressed;
    boolean downPressed;
    public calculateDrawPos (boolean a, boolean b, boolean c, boolean d) {
        this.wPressed=a;
        this.sPressed=b;
        this.upPressed=c;
        this.downPressed=d;
    }

    public void keyPressed(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.VK_W);
        {
            wPressed=true;
        }
        if (keyCode == KeyEvent.VK_S);
        {
            sPressed=true;
        }
        if (keyCode == KeyEvent.VK_UP);
        {
            upPressed=true;
        }
        if (keyCode == KeyEvent.VK_DOWN);
        {
            downPressed=true;
        }
    }

    public void keyReleased(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.VK_W);
        {
            wPressed=false;
        }
        if (keyCode == KeyEvent.VK_S);
        {
            sPressed=false;
        }
        if (keyCode == KeyEvent.VK_UP);
        {
            upPressed=true;
        }
        if (keyCode == KeyEvent.VK_DOWN);
        {
            downPressed=true;
        }
    }
    public void moveBlocks(){
        if (wPressed==(true)){
            PlayerOneY-=5;
        }
        if (sPressed==(true)){
            PlayerOneY+=5;
        }
        if (upPressed==(true)){
            PlayerTwoY-=5;
        }
        if (downPressed==(true)){
            PlayerTwoY+=5;
        }
    }
}

球和游戏:

import java.util.Random;

public class ballAndGameplay {
    private static Random rand;
    int ballX;
    int ballY;
    int ballXVelocity;
    int ballYVelocity;
    public ballAndGameplay (int a, int b, int c, int d) {
        rand = new Random();
        this.ballX=a;
        this.ballY=b;
        this.ballXVelocity=c;
        this.ballYVelocity=d;
    }
    public void moveBall() {

        boolean pointFinished = (ballX <= -20 || ballX >= 1280);


        if (ballY <= 20 || ballY >= 700) {
            ballYVelocity*=-1;
        }
//      if ((ballY >= PlayerOneY && ballY <= PlayerOneY+125 && ballX <= 35) || (ballY >= PlayerTwoY && ballY <= PlayerTwoY+125 && ballX >= 1245)){
//          ballXVelocity*=-1.2;
//      }


        if (pointFinished==(true)){
            try{
                Thread.sleep(500);
            }
            catch (Exception e){

            }   
            ballX=630;
            ballY=300;
            getBallX();
            getBallY();
        }
        ballX+=ballXVelocity;
        ballY+=ballYVelocity;
//      System.out.println("ballX: "+ballX+"  ballY: "+ballY+"  ballXVelocity: "+ballXVelocity+"  ballYVelocity: "+ballYVelocity);
    }       
    public int getBallX(){
        if (ballX != 630){
            return ballXVelocity;
        }
        int side = rand.nextInt(2);
        int number = rand.nextInt(5);
        number+=4;
        if (side==0){
            ballXVelocity=-1*number;
        }
        else if (side==1){
            ballXVelocity=1*number;
        }
        return ballXVelocity;

    }
    public int getBallY(){
        if (ballY != 300){
            return ballYVelocity;
        }
        int side = rand.nextInt(2);
        int number = rand.nextInt(5);
        number+=4;
        if (side==0){
            ballYVelocity=-1*number;
        }
        else if (side==1){
            ballYVelocity=1*number;
        }
        return ballYVelocity;
    }
}
4

2 回答 2

3

你写了

    mainClass mc = new mainClass();
    window.getContentPane().add(new mainClass());

这意味着mainClass您添加到窗口中的 与mc引用的不同。因此,您所做的所有这些操作mc都不会在您的窗口中进行任何更改。

你可以试试写

    mainClass mc = new mainClass();
    window.getContentPane().add(mc);

反而。

于 2015-07-15T12:13:27.573 回答
1

问题:

  • KeyListener 仅适用于具有当前焦点的组件。
  • 您将 KeyListener 添加到 JComponent,该组件默认情况下甚至无法获得焦点(与 JButton、JTextField 和其他通常允许键盘交互的组件不同。)
  • 一种短期解决方案是通过调用使您的 JComponent 成为可聚焦的mc变量mc.setFocusable(true);,然后通过调用赋予它系统焦点mc.requestFocusInWindow();
  • 但是话虽如此,使用键绑定仍然要好得多

其他问题:

  • 覆盖并在 JComponent 的paintComponent方法中绘制,而不是在paint 方法中绘制,如果只是为了避免使用paint 时会出现生涩的图形。
  • 不要忘记paintComponent在你的覆盖中调用 super 的方法来擦除旧的球图像。
  • 永远不要使用通过调用从组件获得的 Graphics 对象getGraphics(),因为这样获得的对象会随着时间的推移变得无效或变为空,从而导致无效的图形或更糟。
  • 永远不要像你正在做的那样直接调用paint(...)or方法。paintComponent(...)
  • 而是按照我敦促您阅读的Swing 图形教程使用被动图形。
  • 使用 Swing Timer 代替 while (true) 循环,因为您的while (true)循环可能会占用 Swing 事件线程,从而冻结您的应用程序。
于 2015-07-15T15:19:07.263 回答