2012-01-11 7 views
2

그래서, 처리 중 (프로그래밍 언어)으로 게임 브레이크 아웃을 만들지 만 배트와의 충돌을 확인하는 기능을 파악할 수는 없습니다.처리 중 브레이크 아웃 (게임)

지금까지 내가 박쥐와의 충돌을 위해 작성한 섹션에서는 볼을베이스에 충돌시키고 반대 방향으로 되돌립니다. 현재 게임은 볼이 벽과 충돌하는 끝이없는 현상입니다. 내가하려고하는 것은 공을 박쥐와 충돌시키는 것입니다.

오, 이건 내 숙제이기 때문에 나를 위해 올바른 방향으로 나를 가리 키시길 바랍니다. 브레이크 아웃의 방망이가 고정 폭이기 때문에 (의사 코드에서) 충돌 감지는 매우 간단 할 수있다,

// Basic Breakout game 
// Code from Matthre Yee-King 

// brick position 
float brickX; 
float brickY; 

// brick width and height 
float brickH; 
float brickW; 

// ball position 
float ballX; 
float ballY; 

// ball diameter 
float ballD; 

// ball direction 
float ballDx; 
float ballDy; 

// bat position 
float batX; 

//bat width 
float batW; 
float batH; 

//bat colour 
float batB; 

void setup() { 
    size (500, 500, P2D); 

    // set sizes of game items 
    brickW = 100; 
    brickH = 50; 
    batW = 100; 
    batH = 25; 
    ballD = 25; 
    batB = 255; 

    // random brick position 
    brickX = random(0, width - brickW); 
    brickY = random (0, height/2); 

    // bat in the centre 
    batX = (width/2) - (batW/2); 

    // ball atop bat 
    ballX = batX + (batW/2); 
    ballY = height - batH - (ballD/2); 

    // ball movement 
    ballDx = random(-5, 5); 
    ballDy = -5; 
    rectMode(CORNER); 
    ellipseMode(CENTER); 
} 

void draw() { 
    // check for ball collision 
    // with top or sides of bat 
    checkBallAgainstBat(); 

    // check for ball collision with 
    // left right and top walls 
    // and bounce 
    checkBallAgainstWalls(); 

    // check ball against brick 
    checkBallAgainstBrick(); 

    // move the ball 
    ballX += ballDx; 
    ballY += ballDy; 
    background(0); 

    // draw the bat 
    fill(0, 255, 0); 
    rect(batX, height - batH, batW, batH); 

    // draw the brick 
    fill(0, 0, batB); 
    batB = (batB + 10) % 255; 
    rect(brickX, brickY, brickW, brickH); 

    // draw the ball 
    fill(255, 0, 0); 
    ellipse(ballX, ballY, ballD, ballD); 

    if (keyCode == 37) { // left cursor key 
    batX -= 10; 

    // keep it on the screen 
    if (batX < 0) { 
     batX = 0; 
    } 
    } 

    if (keyCode == 39) { 
    batX += 10; 
    if (batX > (width - batW)) { 
     batX = width - batW; 
    } 
    } 
} 

// when they let go of the key, reset the keyCode 
void keyReleased() { 
    keyCode = -1; 
} 

// this function checks if the ball has hit the top or sides of the bat and 
// updates its direction as appropriate so the ball bouncs off the bat 
void checkBallAgainstBat() { 
    if (ballY + ballD > height - batH) { 
    ballDy *= -1; 
    } 
} 

// this function checks if the ball has hit the brick. It should bounce off 
// the brick and return true if so 
boolean checkBallAgainstBrick() { 
    return false; 
} 

// this function checks if the ball has hit the top, left or right 
// walls and update its 
// direction as appropriate so the ball bounces off the walls 
void checkBallAgainstWalls() { 
    if (ballX + ballD > width) { 
    ballDx *= -1; 
    } 
    if (ballX - ballD < 0) { 
    ballDx *= -1; 
    } 
    if (ballY - ballD < 0) { 
    ballDy *= -1; 
    } 
} 

답변

3

: 여기

코드의 영어

if (lower_edge(ball) > top_edge(bat)) { 
    // the ball has entered territory where it might have collided 
    if ((left_edge(ball) <= right_edge(bat)) && (right_edge(ball) >= left_edge(bat))) { 
     // the ball's within the horizontal bounds of the bat, so it's a "hit" 
     ... calculate deflection ... 
    } else { 
     // oops, ball's gone past the bat and wasn't hit 
     strike_out(); 
} else { 
    // ball's still above the bat somewhere. do nothing 
} 

: 만약 공의 아래쪽 가장자리가 박쥐의 꼭대기 가장자리가 지나간 곳을 지나쳤습니다. 우리는 아마도 충돌했을 것입니다. 이 필드는 경기 필드의 세로 축만 확인합니다. 그런 다음 볼의 왼쪽 또는 오른쪽 가장자리가 박쥐의 수평 위치에 있는지 확인합니다. 공의 어느 쪽도 배트와 겹치지 않으면 잃어버린 것입니다. 그렇지 않으면 당신은 충돌했고 당신은 충돌 탐지를합니다.

+1

감사합니다. 귀하의 도움말을 구현하도록하겠습니다. 제대로 작동한다면 답변을 수락하겠습니다. 답장을 보내 주셔서 감사합니다. –

+1

감사합니다. 귀하의 제안을 테스트했으며 작동합니다. –