我有这个游戏,一个球落在屏幕上。问题是,球只会向右。我认为问题在于从 LR 方法到主游戏循环的过渡。我创建了一个变量,它采用 LR 方法并在每秒刷新和清除画布的循环中运行它。这是代码:
package cats;
public class BeanDrop {
public static void main(String[] args) throws InterruptedException {
mainGameLoop();
}
public static void mainGameLoop() throws InterruptedException{
double x = .5;
double y = .9;
while (true){
int choice = LR();
arena();
ball(x , y);
if (choice == 1){
// right outcome
x = x + .1;
}
else if(choice == 2){
//left outcome
x = x -.1;
}
y = y - .1;
Thread.sleep(1000);
StdDraw.clear();
}
}
public static void arena(){
StdDraw.picture(.5, .5, "balldrop.jpeg");
}
private static int LR(){
int choice = ((int) Math.random() * 2 + 1);
return choice;
}
public static void ball(double x , double y){
StdDraw.picture(x, y, "ball.jpeg",.05,.05);
}
}