简体   繁体   English

没有按键时让球停止移动?

[英]Make ball stop moving when no key is being pressed?

Here is my code:这是我的代码:

public class Main extends Applet implements Runnable, KeyListener,
    java.awt.event.MouseListener {
int x_pos = 300;
int y_pos = 200;
int radius = 20;
int appletsize_x = 600;
int appletsize_y = 400;
double x_speed = 0;
double y_speed = 0;

private Image dbImage;
private Graphics dbg;

public void init() {
    this.setSize(600, 400);

}

public void start() {

    this.addKeyListener(this);
    this.addMouseListener(this);
    Thread th = new Thread(this);
    th.start();
}

public void stop() {

}

public void destroy() {

}

public void run() {

    // lower ThreadPriority
    this.requestFocusInWindow();
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

    while (true) {

        repaint();
        x_pos += x_speed;
        y_pos += y_speed;

        // Hitting (right)
        if (x_pos > this.getSize().width - radius) {

            //x_speed = -x_speed;
            x_speed = 0;

        }
        // Hitting (left)
        if (x_pos < 0 + radius) {

            //x_speed = -x_speed;
            x_speed = 0;

        }
        // Hitting top
        if (y_pos < 0 + radius) {


            //y_speed = -y_speed;
            y_speed = 0;

        }

        // Hitting bottom
        if (y_pos > this.getSize().height - radius) {

            //y_speed = -y_speed;
            y_speed = 0;

        }

        try {
            // Stop thread for 1 milliseconds
            Thread.sleep(20);
        } catch (InterruptedException ex) {
            // do nothing
        }

        // set ThreadPriority to maximum value
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

    }
}

public void paint(Graphics g) {
    // set colour
    g.setColor(Color.red);

    // paint a filled coloured circle
    g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}

public void update(Graphics g) {

    if (dbImage == null) {
        dbImage = createImage(this.getSize().width, this.getSize().height);
        dbg = dbImage.getGraphics();
    }

    dbg.setColor(getBackground());
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

    dbg.setColor(getForeground());
    paint(dbg);

    g.drawImage(dbImage, 0, 0, this);
}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        if (x_speed > 0) {
            x_speed = +x_speed;
            y_speed = 0;
        }
        if (x_speed == 0) {
            x_speed = -4;
            y_speed = 0;
        }
        if (x_speed < 0) {
            x_speed = +x_speed;
            y_speed = 0;
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        if (x_speed < 0) {
            x_speed = -x_speed;
            y_speed = 0;

            if (x_speed > 0) {
                x_speed = -x_speed;
                y_speed = 0;
            }
        }
        if (x_speed == 0) {
            x_speed = 4;
            y_speed = 0;
        }

    }
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        if (y_speed > 0) {
            y_speed = -y_speed;
            x_speed = 0;
        }
        if (y_speed < 0) {
            y_speed = +y_speed;
            x_speed = 0;
        }
        if (y_speed == 0) {
            y_speed = -4;
            x_speed = 0;
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        if (y_speed > 0) {
            y_speed = +y_speed;
            x_speed = 0;
        }
        if (y_speed < 0) {
            y_speed = -y_speed;
            x_speed = 0;
        }
        if (y_speed == 0) {
            y_speed = +4;
        }
        x_speed = 0;
    }
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent e) {
    System.out.println("HIT!");
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}
         }

How will I make the ball stop moving when no buttons are being pressed?当没有按下任何按钮时,如何让球停止移动?

Under your keyReleased method, you should be able to do the opposite of your keyPressed method.在您的 keyReleased 方法下,您应该能够执行与您的 keyPressed 方法相反的操作。 That is, if you press the right arrow key, add 1 to x_speed, and when you release it, subtract 1 from x_speed, using similar logic for the other keys.也就是说,如果你按下右箭头键,x_speed 加 1,当你松开它时,x_speed 减 1,对其他键使用类似的逻辑。

Could you make x_speed and y _speed equal 0 on keyReleased?你能让 keyReleased 上的 x_speed 和 y _speed 等于 0 吗?

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

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