gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:56:10 27 4
gpt4 key购买 nike

这是我的代码:

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

}
}

当没有按下任何按钮时,如何让球停止移动?

最佳答案

在您的 keyReleased 方法下,您应该能够执行与 keyPressed 方法相反的操作。也就是说,如果按下右箭头键,x_speed 加 1,松开时,x_speed 减 1,其他键使用类似的逻辑。

关于java - 没有按键时让球停止移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6306796/

27 4 0
文章推荐: mysql - 我如何使用 PuPHPet 制作 Sequel Pro?
文章推荐: android - 将基于 TabViewController 的应用程序移植到 Android
文章推荐: java - 有没有办法根据电话号码从电话中获取 WAN IP 地址?
文章推荐: java - 用于提取某些
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com