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

时间:2011-06-10 13:08:19

标签: java applet

这是我的代码:

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

}
         }

如果没有按下任何按钮,我将如何使球停止移动?

2 个答案:

答案 0 :(得分:2)

在keyReleased方法下,您应该能够执行与keyPressed方法相反的操作。也就是说,如果按右箭头键,则向x_speed添加1,当您释放它时,从x_speed中减去1,使用与其他键类似的逻辑。

答案 1 :(得分:2)

你能否在keyReleased上使x_speed和y _speed等于0?