在java中等待并更改颜色

在java中等待并更改颜色,java,jframe,wait,Java,Jframe,Wait,我正在尝试我的第一场比赛。它由两名玩家组成,他们四处移动,试图躲避黄色立方体。我滴了几滴:健康、盾牌、硬币。生命值增加了捡起它的玩家的生命值,硬币增加了分数。但我对护盾车有问题 我希望拿起盾牌的玩家在5秒内变青,并且在相同的时间内不受来自黄色立方体的所有攻击 以下是我的一些代码: package com.main; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.aw

我正在尝试我的第一场比赛。它由两名玩家组成,他们四处移动,试图躲避黄色立方体。我滴了几滴:健康、盾牌、硬币。生命值增加了捡起它的玩家的生命值,硬币增加了分数。但我对护盾车有问题

我希望拿起盾牌的玩家在5秒内变青,并且在相同的时间内不受来自黄色立方体的所有攻击

以下是我的一些代码:

package com.main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Player extends GameObject {    

Color one = Color.white;
Color two = Color.red;
Random r = new Random();
Handler handler;

public Player(int x, int y, ID id, Handler handler) {
    super(x, y, id);
    this.handler = handler;     

}

public Rectangle getBounds(){
    return new Rectangle(x, y, 50, 50);
}

public void tick() throws InterruptedException{
    int a = (int) Math.ceil(Math.random() * 3);
    Color c = Color.white;
    switch (a){
    case(1): c = Color.red;
        break;
    case(2): c = Color.green;
        break;
    case(3): c = Color.blue;
        break;          

    }

    x += velX;
    y += velY;

    x = Game.clamp(x, 0, Game.WIDTH - 56);
    y = Game.clamp(y, 0, Game.HEIGHT - 79);


    /*
    if(speedBoost = 1){

    }
    */
    if(HUD.scoreOne == 1){
        handler.addObject(new Trail(x, y, ID.Trail, c, 50, 50, 0.04f, handler));
    }


    collision(handler, null);
}

private void collision(Handler handler, Graphics g) throws InterruptedException{

    this.handler = handler;

    for(int i = 0; i < handler.object.size(); i++){
        GameObject tempObject = handler.object.get(i);

        if(tempObject.getID() == ID.Enemy){

            if(getBounds().intersects(tempObject.getBounds())){

                if(id == ID.Player){
                    HUD.HEALTHone -= 2;

                }
                if(id == ID.Player2){
                    HUD.HEALTHtwo -= 2;
                }
                if(HUD.HEALTHone == 0 || HUD.HEALTHtwo == 0){

                    System.exit(1);
                }
            }
        }else if(tempObject.getID() == ID.HealthPickup){

            if(getBounds().intersects(tempObject.getBounds())){

                if(id == ID.Player){
                    HUD.HEALTHone += 15;
                    handler.removeObject(tempObject);
                }
                if(id == ID.Player2){
                    HUD.HEALTHtwo += 15;
                    handler.removeObject(tempObject);

                }
            }
        }else if(tempObject.getID() == ID.Coin){

            if(getBounds().intersects(tempObject.getBounds())){

                if(id == ID.Player){
                    HUD.scoreOne += 1;
                    handler.removeObject(tempObject);
                }
                if(id == ID.Player2){
                    HUD.scoreTwo += 1;
                    handler.removeObject(tempObject);

                }
            }
        }else if(tempObject.getID() == ID.ShieldPickup){

            if(getBounds().intersects(tempObject.getBounds())){

                if(id == ID.Player){
                    one = Color.cyan;
                    HUD.HEALTHone += 15;
                    handler.removeObject(tempObject);
                }
                if(id == ID.Player2){
                    two = Color.cyan;
                    for(int j = 0; j < 2;j++){
                    final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
                    executorService.scheduleAtFixedRate(new Runnable() {
                        @Override
                        public void run() {
                            changeBackTwoColor();
                        }
                    }, 0, 1, TimeUnit.SECONDS);
                }
                }   

                    HUD.HEALTHtwo += 15;
                    handler.removeObject(tempObject);

                }
            }

        }
    }


public void changeBackOneColor(){
    one = Color.white;
}


public void changeBackTwoColor(){
    two = Color.red;
}

public void render(Graphics g){
    if(id == ID.Player) g.setColor(one);
    //if(id == ID.Player2) g.setColor(two);
    g.fillRect(x, y, 50, 50);
}
package.com.main;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Rectangle;
导入java.awt.event.KeyEvent;
导入java.util.Random;
导入java.util.concurrent.Executors;
导入java.util.concurrent.ScheduledExecutorService;
导入java.util.concurrent.TimeUnit;
公共类玩家扩展游戏对象{
颜色一=颜色。白色;
颜色二=颜色。红色;
随机r=新随机();
处理者;
公共播放器(int x,int y,ID,处理程序){
super(x,y,id);
this.handler=handler;
}
公共矩形getBounds(){
返回新矩形(x,y,50,50);
}
public void tick()引发InterruptedException{
inta=(int)Math.ceil(Math.random()*3);
颜色c=颜色。白色;
开关(a){
案例(1):c=颜色。红色;
打破
案例(2):c=颜色。绿色;
打破
案例(3):c=颜色。蓝色;
打破
}
x+=velX;
y+=0;
x=游戏夹(x,0,游戏宽度-56);
y=游戏夹(y,0,游戏高度-79);
/*
如果(速度提升=1){
}
*/
如果(HUD.scoreOne==1){
addObject(新轨迹(x,y,ID.Trail,c,50,50,0.04f,handler));
}
冲突(handler,null);
}
私有无效冲突(处理程序,图形g)引发InterruptedException{
this.handler=handler;
对于(int i=0;i
}

如果比较容易看的话,下面是碰撞方法:

private void collision(Handler handler, Graphics g) throws InterruptedException{

this.handler = handler;

for(int i = 0; i < handler.object.size(); i++){
    GameObject tempObject = handler.object.get(i);

    if(tempObject.getID() == ID.Enemy){

        if(getBounds().intersects(tempObject.getBounds())){

            if(id == ID.Player){
                HUD.HEALTHone -= 2;

            }
            if(id == ID.Player2){
                HUD.HEALTHtwo -= 2;
            }
            if(HUD.HEALTHone == 0 || HUD.HEALTHtwo == 0){

                System.exit(1);
            }
        }
    }else if(tempObject.getID() == ID.HealthPickup){

        if(getBounds().intersects(tempObject.getBounds())){

            if(id == ID.Player){
                HUD.HEALTHone += 15;
                handler.removeObject(tempObject);
            }
            if(id == ID.Player2){
                HUD.HEALTHtwo += 15;
                handler.removeObject(tempObject);

            }
        }
    }else if(tempObject.getID() == ID.Coin){

        if(getBounds().intersects(tempObject.getBounds())){

            if(id == ID.Player){
                HUD.scoreOne += 1;
                handler.removeObject(tempObject);
            }
            if(id == ID.Player2){
                HUD.scoreTwo += 1;
                handler.removeObject(tempObject);

            }
        }
    }else if(tempObject.getID() == ID.ShieldPickup){

        if(getBounds().intersects(tempObject.getBounds())){

            if(id == ID.Player){
                one = Color.cyan;
                HUD.HEALTHone += 15;
                handler.removeObject(tempObject);
            }
            if(id == ID.Player2){
                two = Color.cyan;
                for(int j = 0; j < 2;j++){
                final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
                executorService.scheduleAtFixedRate(new Runnable() {
                    @Override
                    public void run() {
                        changeBackTwoColor();
                    }
                }, 0, 1, TimeUnit.SECONDS);
            }
            }   

                HUD.HEALTHtwo += 15;
                handler.removeObject(tempObject);

            }
        }

    }
private void冲突(处理程序,图形g)抛出InterruptedException{
this.handler=handler;
对于(int i=0;i if(tempObject.getID() == ID.Enemy && ((id == ID.Player && !one.equals(Color.cyan) || (id == ID.Player2 && !two.equals(Color.cyan))){