简体   繁体   English

使圆圈变色,然后消失

[英]make circles change color before they disappear

I'm making a game where you jump on rocks and if you jump on the rock which is covered by water, you lose. 我正在做一个游戏,你跳到岩石上,如果你跳到被水覆盖的岩石上,那你就输了。 I made rocks disappear but now I'm want to make some kind of "warning" when the rock will disappear. 我使岩石消失了,但是现在我想在岩石消失时发出某种“警告”。 I thought I could make something like this: 我以为我可以做这样的事情:

if(now >= currentTime[ i ] + 1500) {
     //set color to ex. green
    //fill oval
}

Could you help me achieve that? 你能帮我实现吗? Thanks in advance! 提前致谢!

Here is my code: 这是我的代码:

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();
}
}

Similar to the comment I left above regarding using the Swing timer and the EDT for all rendering, you can use javax.swing.Timer to flash your rocks and make them disappear, like 与我上面关于使用Swing计时器和EDT进行所有渲染的注释类似,您可以使用javax.swing.Timer来闪烁岩石并使它们消失,例如

long stoneWarningTime = ...;
long stoneDisappearTime = stoneWarningTime + ...;

 ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };
  new Timer(stoneWarningTime, taskPerformer).start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM