【问题标题】:Processing color collision detection (java)处理颜色碰撞检测(java)
【发布时间】:2015-11-03 06:39:54
【问题描述】:

我必须制作一个带有碰撞检测的电子游戏。我首先让p1p2 移动并在它们身后留下痕迹。

我尝试进行颜色冲突检测,但它不起作用。我有不同的变量,例如“P1_edge”,这是p1 碰到屏幕边缘的时候。我用红色边框标记了它。

但似乎唯一起作用的是他们在游戏开始时撞到自己。它立即完成,因为他们已经在自己身上了。如果我把那一点拿走,其他什么都不会检测到。

int P1_XPos; //Player 1 position
int P1_YPos;

boolean Player1_Up = false; //Determins what way P1 is going
boolean Player1_Down = false;
boolean Player1_Left = false;
boolean Player1_Right = true;

int P2_XPos; //Player 2 position
int P2_YPos;

boolean Player2_Up = false; //Determins what way P2 is going
boolean Player2_Down = false;
boolean Player2_Left = true;
boolean Player2_Right = false;

boolean Game_State = false; //see if the game is over (false = over)

final int P1_Edge = 0; //P1 crashed into the edge
final int P1_P1 = 1; //P1 crashed into P1
final int P1_P2 = 2; //P1 crashed into P2

final int P2_Edge = 3; //P2 crashed into the edge
final int P2_P2 = 4; //P2 crashed into P2
final int P2_P1 = 5; //P2 crashed into P1

final int Crash = 6; //Other

void setup()
{
size(700,600); //Set screen dimensions
background(0, 0, 0); 

P1_XPos = 100; //set P1 and P2 posision
P1_YPos = 100;

P2_XPos = 600;
P2_YPos = 500;

strokeWeight(3); //Draw the edge of the screen
stroke(255, 0, 0);
line(1, 1, 1, height - 1);
line(1, 1, width - 1, 1);
line(width - 2, 1, width - 2, height - 1);
line(1, height - 2, width - 1, height - 2);

stroke(0, 255, 0); //Draw the starting positions
point(P1_XPos, P1_YPos);

stroke(0, 0, 255); 
point(P2_XPos, P2_YPos);
}
void draw()
{
strokeWeight(3);

if (Game_State == true) //if the game is not over
{
    stroke(0, 255, 0); //Draw P1
    point(P1_XPos, P1_YPos);
    MovePlayer1(); //Move P1

    stroke(0, 0, 255); //Draw P2
    point(P2_XPos, P2_YPos);
    MovePlayer2(); //Move P2

    Collision_Detection(); //Detect any crashes
}


}
void keyPressed()
{
if(key == CODED) //Controls P1 movement
 {
     if (keyCode == UP)
     { 
         Player1_Up = true;
         Player1_Down = false;
         Player1_Left = false;
         Player1_Right = false;
     }

     else if(keyCode == DOWN)
     {
         Player1_Down = true;
         Player1_Up = false;
         Player1_Left = false;
         Player1_Right = false;
     }

     else if (keyCode == LEFT)
     {
         Player1_Left = true;
         Player1_Down = false;
         Player1_Up = false;
         Player1_Right = false;
     }

     else if (keyCode == RIGHT)
     {
         Player1_Right = true;
         Player1_Down = false;
         Player1_Left = false;
         Player1_Up = false;
     }
 }

 if ((key == 'W') || (key == 'w')) //Controls P2 movement
     { 
         Player2_Up = true;
         Player2_Down = false;
         Player2_Left = false;
         Player2_Right = false;
     }

     else if((key == 'S') || (key == 's'))
     {
         Player2_Down = true;
         Player2_Up = false;
         Player2_Left = false;
         Player2_Right = false;
     }

     else if ((key == 'A') || (key == 'a'))
     {
         Player2_Left = true;
         Player2_Down = false;
         Player2_Up = false;
         Player2_Right = false;
     }

     else if ((key == 'D') || (key == 'd'))
     {
         Player2_Right = true;
         Player2_Down = false;
         Player2_Left = false;
         Player2_Up = false;
     }

     if (key == ' ')
     {
         Game_State = true;
     }
}

void MovePlayer1() //Moves P1
{
if(Player1_Up == true)
{
    P1_YPos -= 1;
}

if(Player1_Down == true)
{
  P1_YPos += 1;
}

if(Player1_Left == true)
{
  P1_XPos -= 1;
}

if(Player1_Right == true)
{
  P1_XPos += 1;
}
}

void MovePlayer2() //Moves P2
{
if(Player2_Up == true)
{
    P2_YPos -= 1;
}

if(Player2_Down == true)
{
  P2_YPos += 1;
}

if(Player2_Left == true)
{
  P2_XPos -= 1;
}

if(Player2_Right == true)
{
  P2_XPos += 1;
}
}

int TestColorP1 (color P1Test) //Detect what color P1 is touching
{

 if (P1Test == color (255,0,0)) 
 return P1_Edge; 
 else if (P1Test == color(0,255,0))
 return P1_P1;
 else if (P1Test == color(0,0,255))
 return P1_P2;
 else return Crash;
}

int TestColorP2 (color P2Test) //Detect what color P2 is touching
{

 if (P2Test == color (255,0,0)) 
 return P2_Edge; 
 else if (P2Test == color(0,255,0))
 return P2_P1;
 else if (P2Test == color(0,0,255))
 return P2_P2;
 else return Crash;
}

void Collision_Detection()
{
color P1_Pixel; //This is the color P1 is touching
color P2_Pixel;

P1_Pixel = get(P1_XPos, P1_YPos); //Declare is as P1 position
P2_Pixel = get(P2_XPos, P2_YPos);

 if (TestColorP1(P1_Pixel) == P1_Edge) //See if P1 has crashed
 { 
    background(0);
    Game_State = false;
 }
 else if (TestColorP1(P1_Pixel)== P1_P1)
 {
   // background(0);
   // Game_State = false;
 }
 else if (TestColorP1(P1_Pixel) == P1_P2)
 {
    background(0);
    Game_State = false;
 }

 if (TestColorP2(P2_Pixel) == P2_Edge) //See if P2 has crashed
 { 
    background(0);
    Game_State = false;
 }
 else if (TestColorP2(P2_Pixel)== P2_P1)
 {
    background(0);
    Game_State = false;
 }
 else if (TestColorP2(P2_Pixel) == P2_P2)
 {
   // background(0);
  //  Game_State = false;
 }
 else if (TestColorP2(P2_Pixel) == Crash)
 {

 }

}

我知道它很长,但是您应该可以将其复制并粘贴到处理草图中,它会起作用。我也留下了 cmets,所以你知道我为什么要添加每一段代码。

【问题讨论】:

    标签: java processing collision


    【解决方案1】:

    这真是一个令人头疼的问题——好问题。

    让您失望的最大事情是 Processing 默认启用抗锯齿功能。这会导致您的颜色与您的预期略有不同。在大多数草图中,这是一件好事,因为它使事情看起来更好。但是,由于您使用的是确切的颜色值,这让您感到困惑。您可以通过截取草图然后对颜色进行采样来证明这一点。

    要禁用此功能,只需在草图开头调用noSmooth()。更多信息可以参考here

    接下来让您感到困惑的是,您的笔画权重设置为 3,但您一次只能移动玩家 1 个像素。这会导致玩家停留在最后一个被绘制的点“内部”,这就是他们不断撞到自己的原因。

    要修复那个,您可以简单地在draw() 函数的开头调用strokeWeight(1);。或者,如果您需要 3 的笔画权重,请确保将玩家移到刚刚绘制的圆圈之外。

    这将解决您的问题,但从长远来看,您最好在 ArrayListPVectors 之类的数据结构中跟踪以前的玩家位置。然后,每次调用 draw() 时都要重新绘制它们,而不是只绘制一次。与其尝试手动检查颜色,不如只在路径的某些部分进行碰撞检查,以避免上述情况。

    【讨论】:

    • 啊,谢谢,将 strokeweight 更改为 1 修复了它。这只是一个小任务,下周我们必须做蛇,所以我可能不得不像你说的那样使用 pvectors 的数组列表。再次感谢您的信息
    • 你可以有一个带有noSmooth 的镜像 PGraphics 并在那里进行检测。
    • @Will 是的,Snake 可能需要ArrayList 方法,因为尾巴不会不断增长。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 2013-01-31
    • 2011-03-28
    • 1970-01-01
    相关资源
    最近更新 更多