2015-11-02 121 views
3

我必須做一個帶有碰撞檢測的tron類型的遊戲。我開始讓p1p2移動並在他們身後留下蹤跡。處理顏色碰撞檢測(java)

我試圖做一個顏色碰撞檢測,但它不工作。我有不同的變量,例如「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) 
{ 

} 

} 

我知道它很長,但你應該能夠將它複製並粘貼到處理草圖中,並且它將起作用。我也留下了評論,所以你知道爲什麼我添加了每一個代碼。

回答

3

這是一個真正的頭部劃痕 - 很好的問題。

拋棄你的最大問題是Processing默認情況下啓用了抗鋸齒功能。這會導致您的顏色與您所期望的顏色稍有不同。在大多數草圖中,這是一件好事,因爲它使事情看起來更好。但是因爲你使用的是確切的顏色值,所以這會讓你感到困擾。您可以通過截取草圖並隨後採樣顏色來證明這一點。

要禁用此功能,只需在草圖開始處撥打noSmooth()即可。更多信息可以在參考here中找到。

接下來讓你感到困擾的是你的中風重量設置爲3,但是你一次只移動一個像素。這會導致玩家「留在」最後一點的內部,這就是爲什麼他們會不斷跑到自己身邊。

要修復,您可以在draw()函數的開頭簡單地調用strokeWeight(1);。或者,如果您需要3的中風重量,那麼請確保您將播放器移動到剛剛繪製的圓圈之外。

這會解決你的問題,但從長遠來看,你可能會更好地跟蹤數據結構中的以前的玩家位置,如的ArrayList。然後,每次調用draw()時,都會重新繪製它們,而不是僅繪製一次。而不是嘗試手動檢查顏色,只會在路徑的某些部分進行碰撞檢查,以避免上述情況。

+0

啊謝謝,把strokeweight改爲1固定它。這只是一個小小的任務,下個星期我們必須做蛇,所以生病可能不得不使用像你所說的pvectors的陣列列表。再次感謝信息 – Will

+0

你可以用'noSmooth'來鏡像PGraphics並在那裏檢測。 –

+0

@ Will Yep,Snake可能需要'ArrayList'方法,因爲尾巴不會不斷增長。 –