我正在尝试制作我的第一个游戏。它由两个玩家四处移动,试图躲避黄色立方体。我已经投了几滴:健康,盾牌,硬币。健康为捡起它的玩家增加了健康,并且硬币增加了分数。但是我遇到了盾牌拾音器的问题。
我希望拿起盾牌掉落的玩家在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);
}
}
如果更容易看到,这就是碰撞方法:
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);
}
}
}
答案 0 :(得分:0)
在与敌人发生碰撞时,您不会检查您是否处于“青色”模式。 尝试这样的事情:
if(tempObject.getID() == ID.Enemy && ((id == ID.Player && !one.equals(Color.cyan) || (id == ID.Player2 && !two.equals(Color.cyan))){