【发布时间】:2019-04-17 03:17:58
【问题描述】:
我正在编写一个处理球/粒子运动的程序。我想对逻辑进行编程,使“热”球每秒移动 4 厘米到 6 厘米,而“冷”球每秒移动 2 厘米到 4 厘米。如何使用每厘米像素 (113/2.54)(其中 113 是我的每英寸屏幕分辨率)来设置速度?
我只是为球的 vx 和 vy 值使用一个固定数字。
这是我的 Ball 构造函数
public Ball(Side s, Color color) { //Side character used to determine if ball should spawn on left or right side.
//makeRandom method gives positive or negative direction for each ball
if (color == Color.RED){
this.vx = 8 * makeRandom();
this.vy = 8 * makeRandom();
} else {
this.vx = 5 * makeRandom();
this.vy = 5 * makeRandom();
}
speed = Math.sqrt(Math.pow(this.vx, 2)+Math.pow(this.vy, 2));
//position is randomized for each ball
if(s == Side.LEFT) {
this.x = leftSideBallX + (int) (50 * Math.random());
this.y = leftSideBallY + (int) (50 * Math.random());
} else {
this.x = rightSideBallX + (int) (50 * Math.random());
this.y = rightSideBallY + (int) (50 * Math.random());
}
}
这是我的游戏循环
@Override
public void run() {
running = true;
addParticlesToVector();
long start;
long elapsed;
long wait;
while (running) {
start = System.nanoTime();
gameUpdate();
this.repaint();
leftTemp.setText("Temperature: " + df.format(lTemp));
rightTemp.setText("Temperature: " + df.format(rTemp));
elapsed = (start - System.nanoTime());
wait = targetTime - elapsed/1000000;
try {
Thread.sleep(wait);
} catch (Exception e) {
e.printStackTrace();
}
}
}
【问题讨论】:
标签: java loops user-interface