我是java新手并为我的大学项目制作这个游戏
它基本上是用户与cpu游戏。 所以一旦用户点击它(即代码中的第一个方法),如何在用户转向后让系统自己调用该方法?
我在这里列出的代码是,我需要将鼠标移出开始按钮以使系统播放所以它的输入类型......还有其他出路吗?
private void singlePlayerModeGame(final Stage primaryStage) {
System.out.println("singlePlayerModeGame is called");
foreground_startButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
turnIndicator.setText("USER START");
System.out.println("USER!");
System.out.println("togglePlayer -- " + togglePlayer);
if (togglePlayer == 0) {
do {
togglePlayer = 1;
face = r.nextInt(7);
switch (face) {
case 1:
rollDice1(primaryStage);
face = 1;
break;
case 2:
rollDice2(primaryStage);
face = 2;
break;
case 3:
rollDice3(primaryStage);
face = 3;
break;
case 4:
rollDice4(primaryStage);
face = 4;
break;
case 5:
rollDice5(primaryStage);
face = 5;
break;
case 6:
rollDice6(primaryStage);
face = 6;
break;
}
} while (face == 6);
}
}
});
}
private void singlePlayerSystemGame(final Stage primaryStage) {
System.out.println("singlePlayerSystemGame is called");
foreground_startButton.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
if (togglePlayer == 1) {
do {
togglePlayer = 0;
turnIndicator.setText("System START");
face = r.nextInt(7);
switch (face) {
case 1:
rollDice1(primaryStage);
face = 1;
break;
case 2:
rollDice2(primaryStage);
face = 2;
break;
case 3:
rollDice3(primaryStage);
face = 3;
break;
case 4:
rollDice4(primaryStage);
face = 4;
break;
case 5:
rollDice5(primaryStage);
face = 5;
break;
case 6:
rollDice6(primaryStage);
face = 6;
break;
}
} while (face == 6);
}
}
});
}
答案 0 :(得分:0)
我会在单独的线程中调用这两个方法,并执行以下操作:
while(!exit) //your end of game condition
{
if(togglePlayer == 1){
do{
/*your logic*/
}while()//move tooglePlayer = 0 out of this loop
synchronized(lock){
tooglePlayer = 0;
lock.notify();
}
}
else{
synchronized (lock){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
对两种方法都这样做。两种方法也应该可以看到锁定和退出条件。声明锁定为:Object lock = new Lock();
根据需要使用修改器。