本文介绍了让圆圈在消失之前改变颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个游戏,你跳到岩石上,如果你跳到被水覆盖的岩石上,你就输了.我让岩石消失了,但现在我想在岩石消失时发出某种警告".我以为我可以做这样的事情:

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!

这是我的代码:

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 来闪烁您的岩石并制作它们消失了,就像

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

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

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

这篇关于让圆圈在消失之前改变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 14:19