我正在做一个游戏,你跳上岩石,如果你跳上被水覆盖的岩石,你会失去。 我做了石头消失,但现在我想要做出某种“警告”,当岩石将消失。 我以为我可以做这样的事情:
if(now >= currentTime[ i ] + 1500) { //set color to ex. green //fill oval }
你能帮我实现吗? 提前致谢!
这是我的代码:
package com.pitcher654.main; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyListener; import java.awt.image.BufferStrategy; import java.util.Random; import com.pitcher654.main.Game.STATE; public class Game extends Canvas implements Runnable{ private static final long serialVersionUID = -7800496711589684767L; public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9; private Thread thread; private boolean running = false; private static final int NUM_STONES = 5; private boolean[] visible = new boolean[NUM_STONES]; private long[] changeTimes = new long[NUM_STONES]; private Random r; private Handler handler; //private HUD hud; private Menu menu; public enum STATE { Menu, Help, Game }; public STATE gameState = STATE.Menu; public Game() { handler = new Handler(); menu = new Menu(this, handler); this.addKeyListener(new KeyInput(handler)); this.addMouseListener(menu); new Window(WIDTH, HEIGHT, "My game", this); //hud = new HUD(); r = new Random(); //if(gameState == STATE.Game) { //handler.addObject(new Player(100, 200, ID.Player)); for(int i=0; i<NUM_STONES; i++){ visible[i] = true; // each stone will be visible changeTimes[i] = System.currentTimeMillis() + r.nextInt(10000) + 3000; // every stone will disappear in less than 10 seconds //} } //handler.addObject(new Player(100, 200, ID.Player)); //handler.addObject(new BasicEnemy(100, 200, ID.BasicEnemy)); } public synchronized void start() { thread = new Thread(this); thread.start(); running = true; } public synchronized void stop() { try { thread.join(); running = false; }catch(Exception ex) { ex.printStackTrace(); } } public void run() { this.requestFocus(); long lastTime = System.nanoTime(); double amountOfTicks = 60.0; double ns = 1000000000 / amountOfTicks; double delta = 0; long timer = System.currentTimeMillis(); int frames = 0; while(running) { long now = System.nanoTime(); delta += (now - lastTime) / ns; lastTime = now; while(delta >=1) { tick(); delta--; } if(running) render(); frames++; if(System.currentTimeMillis() - timer > 1000) { timer += 1000; //System.out.println("FPS: "+ frames); frames = 0; } } stop(); } private void tick() { handler.tick(); //hud.tick(); if(gameState == STATE.Game) { Random r = new Random(); long now = System.currentTimeMillis(); int b = 0; //System.out.println(now); long extraSeconds = 500; for(int i=0; i<NUM_STONES; i++){ for(int j = 0; j < NUM_STONES; j++) { if(visible[ i ]) b++; } //if(b < 2) continue; if(now > changeTimes[i]){ // if the time has come if(visible[i]) { changeTimes[i] = now + extraSeconds + r.nextInt(5000); if(b < 2) {; visible[ i ] = !visible[ i ]; changeTimes[ i ] += 5000; } }// every stone will be invisible up to five seconds else { changeTimes[i] = now + extraSeconds + r.nextInt(10000); // every stone will be visible again up to 10 seconds } visible[i] = !visible[i]; //System.out.println(visible[ i ]);// switch the visibility state }else if (now > changeTimes[ i ]) { System.out.println("Usli u zeleno"); } b = 0; } }else if(gameState == STATE.Menu) { menu.tick(); } } private void render() { BufferStrategy bs = this.getBufferStrategy(); if(bs == null) { this.createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); long now = System.currentTimeMillis(); g.setColor(new Color(87, 124, 212)); g.fillRect(0, 0, WIDTH, HEIGHT); if(gameState == STATE.Game) { g.setColor(new Color(209, 155, 29)); for(int i = 0; i < 5; i++) { g.fillOval(80 + (100 * i), 325, 70, 20); } if(gameState == STATE.Game) { for(int i = 0; i < NUM_STONES; i++) { if(visible[i]) g.setColor(new Color(209, 155, 29)); else g.setColor(new Color(107, 155, 170)); g.fillOval(80 + (100 * i), 325, 70, 20); } } }else if(gameState == STATE.Menu || gameState == STATE.Help){ menu.render(g); } handler.render(g); if(gameState == STATE.Game) { } //hud.render(g); g.dispose(); bs.show(); } public static int clamp(int var, int min, int max) { if(var >= max) return var = max; else if(var <= max) return var = min; else return var; } public static void main(String[] args) { new Game(); } }
与上面关于使用Swing计时器和EDT进行所有渲染的注释类似,可以使用javax.swing.Timer来使您的岩石闪烁并使其消失
long stoneWarningTime = ...; long stoneDisappearTime = stoneWarningTime + ...; Actionlistner taskPerformer = new Actionlistner() { public void actionPerformed(ActionEvent evt) { //...Perform a task... } }; new Timer(stoneWarningTime, taskPerformer).start();