2014-04-22 91 views
1

現在我確定你們都聽說過這些愚蠢的檯球遊戲問題,這應該有希望成爲我的最後一個,我已經得到了碰撞,但把它保持在屏幕上是一點點的問題。我該如何去做呢?保持對象在屏幕上

class Ball { 
    int xpos, ypos; 
    int ballDiam; 
    color myColor; 
    Ball(int tempdiam, color tempColor) { 
    ballDiam=tempdiam; 
    myColor=tempColor; 
    } 

    void update() { 
    fill(myColor); 
    ellipse(xpos, ypos, ballDiam, ballDiam); 
    } 
} 

Ball b1, b2; 
int click; 
String msg; 
int steps = 30; 
int difx,dify; 
Boolean moving = false; 
void setup() { 
    msg=""; 
    click=0; 
    size(600, 300); 
    b2= new Ball(50, #000000); 
    b1 = new Ball(50, #ffffff); 
} 
void draw() { 
    background(#009900); 
    b1.update(); 
    b2.update(); 

    if (click==0) { 
    b1.xpos=mouseX; 
    b1.ypos=mouseY; 

    msg="click to place ball"; 
    } 
    if (click==0) { 
    b1.xpos=mouseX; 
    b1.ypos=mouseY; 
    b1.update(); 
    } 
    else if (click==1) { 
    b2.xpos=mouseX; 
    b2.ypos=mouseY; 
    b2.update(); 
    difx = b1.xpos-b2.xpos; 
    dify = b1.ypos-b2.ypos; 
    msg="click to place eightball and shoot"; 
    } else if(click==2){ 
    b1.xpos-=difx/steps; 
    b1.ypos-=dify/steps; 
    b1.update(); 
    //cdistance = dist(b1.xpos,b1.ypos,b2.xpos,b2.ypos); 
    float distance = dist(b1.xpos,b1.ypos,b2.xpos,b2.ypos); 
    if(distance>b2.ballDiam/2){ 
     moving = true; 
     b1.xpos-=difx/steps; 
     b1.ypos-=dify/steps; 
    } 
    else{ 
     moving = false; 
     msg="new"; 
     click=3; 

    } 

    } 

    else if(click==3){ 
    if (b1.xpos<b2.xpos && b1.ypos<b2.ypos){ 
     b2.xpos+=5; 
     b2.ypos+=5; 
     } 
    if (b1.xpos>b2.xpos && b1.ypos>b2.ypos){ 
     b2.xpos-=5; 
     b2.ypos-=5; 
     } 
    if (b1.xpos>b2.xpos && b1.ypos<b2.ypos){ 
     b2.xpos-=5; 
     b2.ypos+=5; 
     } 
    if (b1.xpos<b2.xpos && b1.ypos>b2.ypos){ 
     b2.xpos+=5; 
     b2.ypos-=5; 
     } 
    msg="click again to start over"; 
    } 
else if(click==4){ 
    setup(); 
} 


    textSize(20); 
    text(msg, 0, height-5); 
} 


void mouseClicked() { 
    if(!moving){ 
    click++; 
    } 
} 
+0

你在用什麼語言工作? – hurnhu

+0

我正在使用程序處理。 –

+0

嘿請考慮upvoting和/或接受我的答案,如果它幫助你... –

回答

0

你可以做到這一點幾乎是你正在做的兩個球之間的碰撞檢測以同樣的方式,希望您需要的球,你的屏幕的界限之間的測試...... 你也想一定要利用你的球類的直徑屬性,以確保你真正得到球的邊緣,並沒有反彈一半出來,回來...

if(b1.xpos + b1.ballDiam >= width) { // middle of the ball plus its diameter is over the right screen bounds 
    println("ball hit the right side"); 
    b1.xpos = width - b1.ballDiam; // clamping the position to the bounds of the screen 
    b1.reverse(); // you probably want to do something about making it bounce back at this point 
} 

if(b1.xpos - b1.ballDiam <= 0) { // middle of the ball minus its parameter is over the left screen bounds 
    println("ball hit the left side"); 
    // all the other stuff here 
} 

if(b1.ypos - b1.ballDiam <= 0) { // middle of the ball minus its parameter is over the top screen bounds 
    println("ball hit the top side"); 
    // all the other stuff here 
} 

if(b1.ypos + b1.ballDiam >= height) { // middle of the ball minus its parameter is over the bottom screen bounds 
    println("ball hit the bottom side"); 
    // all the other stuff here 
} 

有意義嗎?