2012-01-11 17 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

ありがとう、私はあなたの提案をテストし、それが動作します。 –

関連する問題