2015-10-29 60 views
2

我正在使用處理,我想創建一個遊戲,玩家在一個房間裏,如果他們撞牆,他們會像普通牆壁一樣停下來。不過,我真的不知道如何做到這一點。使用我目前的方法,一旦他們撞上了牆,他們就無法再在X軸上移動了。任何幫助將不勝感激。碰撞檢測,然後讓玩家停止

PVector playerPosition; 
PVector barrierPosition; 

float velocity = 4; 

boolean goLeft=false; 
boolean goRight=false; 
boolean goUp=false; 
boolean goDown=false; 

float playerWidth = 10; 
float playerHeight = 10; 

float barrierWidth = 10; 
float barrierHeight = 600; 



void setup() { 
    size(800,600); 
    rectMode(CORNER); 
    playerPosition = new PVector(200,200); 
    barrierPosition = new PVector(0,0); 
} 

void draw() { 
background(255); 
drawPlayer(); 
movePlayerPosition(); 
drawBarrier(); 
checkCollide(); 



} 
    //check for collision with barrier then stop player moving 
    void checkCollide() { 
    if (playerPosition.y <= barrierHeight + playerHeight && playerPosition.x <= barrierWidth + playerWidth) 
    { 
    println("COLLIDED"); 
    playerPosition.x = barrierPosition.x + 10; 
    } 

    } 

void drawPlayer() { 
    fill(255,0,0); 
    rect(playerPosition.x,playerPosition.y,playerWidth,playerHeight); 
} 
void drawBarrier() { 
    fill(0); 
    rect(barrierPosition.x,barrierPosition.y,barrierWidth,barrierHeight); 
} 

void movePlayerPosition() { 

    //moves up and down 
    if (goUp && playerPosition.y > 0) 
    { 
     playerPosition.y = playerPosition.y - velocity; 
    } else if (goDown && playerPosition.y < 598-playerHeight) 
    { 
     playerPosition.y = playerPosition.y + velocity; 
    } 

    //moves right and left 
    if (goLeft && playerPosition.x > 0) 
    { 
     playerPosition.x = playerPosition.x -velocity; 
    } else if (goRight && playerPosition.x <800-playerWidth) 
    { 
     playerPosition.x = playerPosition.x + velocity; 
    } 
    } 

    //if the keys are pressed move in that direction 
void keyPressed() { 
    if (key == 'w') { 
    goUp=true; 
    } else if (key == 'a') { 
    goLeft=true; 
    } else if (key == 's') { 
    goDown=true; 
    } else if (key == 'd') { 
    goRight=true; 
    } 
} 
//if the keys are released stop moving 
void keyReleased() { 
    if (key == 'w') { 
    goUp=false; 
    } else if (key == 'a') { 
    goLeft=false; 
    } else if (key == 's') { 
    goDown=false; 
    } else if (key == 'd') { 
    goRight=false; 
    } 
} 
+0

這是一個非常廣泛的問題。碰撞檢測不是微不足道的。你有沒有學過任何教程?你能否將問題縮小到幾行不符合預期的代碼? –

回答

0

第一個簡單的答案;你的checkForCollide方法應該返回一個布爾值而不是void,那麼你可以使用這個方法和movePlayerPosition,並且只有在沒有碰撞時才執行X軸移動。

現在,在一個更完整的答案;您應該多關注一下這個主題,有很多模式和庫可以更好,更輕鬆地處理這個主題。 我推薦你Nature of code,你可以免費得到這個pdf,你有很多關於使用第5章中的物理圖書館,還有很多關於編程遊戲的樣本。

希望這會有所幫助,祝你好運。 Regards Jose

+0

感謝您的幫助!這解決了它。再次感謝Jose – Buzzbrad

+0

如果有效,請將答案標記爲未來尋找答案的用戶所接受的答案。我很樂意提供幫助。 –

0

你太親近了!

這一行後:

println("COLLIDED"); 

設置像這樣的新座標:

playerPosition.x = barrierPosition.x + 10; 

這意味着第一次衝突發生的地方之後,X位置總是被設置爲barrierPosition.x + 10;

你可能想要做的是將播放器移動10個像素,但相對於它的當前位置:

playerPosition.x = playerPosition.x + 10; 

或:

playerPosition.x += 10; 

玩得開心!

僅供參考,您有幾個字符完整的代碼修改:

PVector playerPosition; 
PVector barrierPosition; 

float velocity = 4; 

boolean goLeft=false; 
boolean goRight=false; 
boolean goUp=false; 
boolean goDown=false; 

float playerWidth = 10; 
float playerHeight = 10; 

float barrierWidth = 10; 
float barrierHeight = 600; 



void setup() { 
    size(800,600); 
    rectMode(CORNER); 
    playerPosition = new PVector(200,200); 
    barrierPosition = new PVector(0,0); 
} 

void draw() { 
background(255); 
drawPlayer(); 
movePlayerPosition(); 
drawBarrier(); 
checkCollide(); 



} 
    //check for collision with barrier then stop player moving 
    void checkCollide() { 
    if (playerPosition.y <= barrierHeight + playerHeight && playerPosition.x <= barrierWidth + playerWidth) 
    { 
    println("COLLIDED"); 
    playerPosition.x += 10; 
    } 

    } 

void drawPlayer() { 
    fill(255,0,0); 
    rect(playerPosition.x,playerPosition.y,playerWidth,playerHeight); 
} 
void drawBarrier() { 
    fill(0); 
    rect(barrierPosition.x,barrierPosition.y,barrierWidth,barrierHeight); 
} 

void movePlayerPosition() { 

    //moves up and down 
    if (goUp && playerPosition.y > 0) 
    { 
     playerPosition.y = playerPosition.y - velocity; 
    } else if (goDown && playerPosition.y < 598-playerHeight) 
    { 
     playerPosition.y = playerPosition.y + velocity; 
    } 

    //moves right and left 
    if (goLeft && playerPosition.x > 0) 
    { 
     playerPosition.x = playerPosition.x -velocity; 
    } else if (goRight && playerPosition.x <800-playerWidth) 
    { 
     playerPosition.x = playerPosition.x + velocity; 
    } 
    } 

    //if the keys are pressed move in that direction 
void keyPressed() { 
    if (key == 'w') { 
    goUp=true; 
    } else if (key == 'a') { 
    goLeft=true; 
    } else if (key == 's') { 
    goDown=true; 
    } else if (key == 'd') { 
    goRight=true; 
    } 
} 
//if the keys are released stop moving 
void keyReleased() { 
    if (key == 'w') { 
    goUp=false; 
    } else if (key == 'a') { 
    goLeft=false; 
    } else if (key == 's') { 
    goDown=false; 
    } else if (key == 'd') { 
    goRight=false; 
    } 
}