私は非常に単純なスネーク ゲームを作成しています。X 秒ごとにランダムな位置に移動したい Apple というオブジェクトがあります。私の質問は、このコードを X 秒ごとに実行する最も簡単な方法は何ですか?
apple.x = rg.nextInt(470);
apple.y = rg.nextInt(470);
ありがとう。
編集:
さて、すでにこのようなタイマーがあります:
Timer t = new Timer(10,this);
t.start();
ゲームの開始時にグラフィック要素を描画し、次のコードを実行します。
@Override
public void actionPerformed(ActionEvent arg0) {
Graphics g = this.getGraphics();
Graphics e = this.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
e.fillRect(0, 0, this.getWidth(), this.getHeight());
ep.drawApple(e);
se.drawMe(g);