我只是Java的初学者。我们有一个大学项目,我们正在制作一个名为othello(也称为reversi)的游戏,我们正在使用Java applet进行游戏。电路板设计和游戏玩法已经完成,工作正常,但我们遇到了计时器的问题。
问题是在计时器完全消失之前未检测到玩家的移动,即假设每次移动的计时器为10秒,如果用户将其移动放置在第5秒,则他的移动将仅在10秒后被检测到。没有任何目的。
我遵循的方法是:
整个事情都放在实际创建问题的循环中。我需要一个更好的,实际上是对此代码的工作修改。
以下是两个相关功能的代码
void startTimer() {
Graphics g = getGraphics();
for(sec =0 ; sec < 10 ; sec++){
String time = new String();
time = Integer.toString(sec);
g.drawString(time, WIDTH+80, 145);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(NewApplet.class.getName()).log(Level.SEVERE, null, ex);
}
update(getGraphics());
}
}
public boolean mouseUp(Event e, int x, int y) {
int column = (int)(x / (WIDTH / 8));
int row = (int)(y / (WIDTH / 8));
if (turn == BLACK) {
if (placeLegalCheck(column, row, turn) == true){
flipDisk(column, row, turn);
turn = - turn;
diskCount();
startTimer();
update(getGraphics());
try {
Thread.sleep(500);
} catch (Exception excep){
}
}
}
if (turn == WHITE) {
if (placeLegalCheck(column, row, turn) == true){
flipDisk(column, row, turn);
turn = - turn;
diskCount();
startTimer();
update(getGraphics());
try {
Thread.sleep(500);
} catch (Exception excep){
}
}
}
return true;
}