2016-12-02 150 views
0

您好我有這樣的代碼,我只是想在游泳池當他們到達的4個孔的一個消失了,如果白球去的任何孔的比賽應該重置或結束撞球遊戲不工作

即代碼: 除上述內容外,遊戲已經正常運行。我非常感謝任何幫助。

/** 
    * Welcome to the pool game * 
    * Click, drag and release on a ball to give it a push 
    * numerical keys 1-4 respawns different number of balls **/ 
    Table game; 
    //-------------- 
    void setup() 
    { 
     size(210, 330); 
     smooth(); 
     game = new Table(6, 5, 200, 300); 
     game.startGame(); 
    } 
    //-------------- 
    void draw() { 
     background(32); 
     game.update(); 
     game.visualize(); 
     fill(0); 
     ellipse(6, 7, 20, 20); 
     ellipse(195, 7, 20, 20); 
     ellipse(6, 295, 20, 20); 
     ellipse(195, 295, 20, 20); 

    } 
    //-------------- 
    void keyPressed() { 
     println(game.kineticEnergy()); 
    } 
    //-------------- 
    void mousePressed() { 
     game.mousePressed(mouseX-game.x, mouseY-game.y); 
    } 
    //-------------- 
    void mouseReleased() { 
     game.mouseReleased(mouseX-game.x, mouseY-game.y); 
    } 
    //============================= 
    class Table { 
     float drag = 0.985; 
     float elasticity = 0.9; 
     float wallElasticity = 0.8; 
     float pushFactor = 0.05; 
     float maxPush = 10; color[] ballColors =new color[] {color(192),color(192,64,32), color(64, 192, 0)}; 
     Ball[] balls; 
     Ball selectedBall; 
     int x, y, width, height; 
     //-------------- 
     Table(int x, int y, int w, int h) { 
      this.x = x; 
      this.y = y; 
      width = w; 
      height = h; 
     } 
     //-------------- 
     void startGame() { 
      buildBalls(8); 
     }//-------------- 
     void buildBalls(int count) 
     { 
      balls = new Ball[2*count+1]; 
      for (int i=0; i<count; i++) 
       balls[i] = new Ball(random(width), random(height), 1, this); 
      for (int i=0; i<count; i++) 
       balls[count+i] = new Ball(random(width), random(height), 2, this); 
      balls[2*count] = new Ball(0.5*(width), 0.5*(height), 0, this); 
     } 
     //-------------- 
     void update() { 
      //simulation 
      for (int i=0; i<balls.length; i++) 
       balls[i].update(); 
      //collision detection 
      for (int i=0; i<balls.length; i++) 
       for (int j=i+1; j<balls.length; j++) 
        balls[i].collisionDetect(balls[j]); 
     } 
     //-------------- 
     void visualize() { 
      translate(x, y); 
      noStroke(); 
      fill(0, 128, 0); 
      rect(0, 0, width, height); 
      //draw que 
      stroke(255); 
      if (mousePressed && selectedBall != null) 
       line(selectedBall.x, selectedBall.y, mouseX-x, mouseY-y); 
      //drawing 
      for (int i=0; i<balls.length; i++) 
       balls[i].visualize(); 
     } 
     //-------------- 
     float kineticEnergy() { 
      float energy=0; 
      for (int i=0; i<balls.length; i++) 
       energy += mag(balls[i].vx, balls[i].vy); 
      return energy; 
     } 
     //-------------- 
     void mousePressed(int mx, int my) 
     { 
      for (int i=0; i<balls.length; i++) 
       if (dist(balls[i].x, balls[i].y, mx, my) < balls[i].radius){      
        selectedBall = balls[i]; 
        break; 
       } 
     } 
     //-------------- 
     void mouseReleased(int mx, int my) { 
      if (selectedBall != null) { 
       float px = (selectedBall.x-mx) * pushFactor; 
       float py = (selectedBall.y-my) * pushFactor; 
       float push = mag(px, py); 
       if (push > maxPush) { 
        px = maxPush*px/push; 
        py = maxPush*py/push; 
       } 
       selectedBall.push(px, py); 
      } 
      selectedBall = null; 
     } 
    } 
+0

在班級'Ball'之前放置一個新行,以便縮進可以看到 – Monasha

回答

0

在未來,請儘量縮小問題下降到MCVE。你只是在問一個具體的事情,當它碰到一個角落時消失了一個球,所以我們確實不需要看到任何其他的邏輯。

但基本上,您需要使用dist()函數並檢查球距離每個角的距離。這裏有一個小例子,做與單球和單角落:

//keep track of whether the ball is disappeared 
boolean alive = true; 

//keep track of the hole's location and size 
float holeX; 
float holeY; 
float holeSize; 

//keep track of the ball size 
float ballSize = 20; 

void setup() { 
    size(200, 200); 

    holeX = width*.75; 
    holeY = height*.75; 
    holeSize = width/4; 
} 

void draw() { 

    background(0); 

    //is the ball still on the table? 
    if (alive) { 

    //draw the ball 
    fill(0, 255, 0); 
    ellipse(mouseX, mouseY, ballSize, ballSize); 

    //check the ball against the hole 
    if (dist(mouseX, mouseY, holeX, holeY) < ballSize/2 + holeSize/2) { 
     //if they collide, disappear the ball 
     alive = false; 
    } 
    } 

    //draw the hole 
    fill(255, 0, 0); 
    ellipse(holeX, holeY, holeSize, holeSize); 
} 

你會做同樣的事情,每個球各個角落。如果您仍然卡住,請嘗試從這個小例子開始,而不是整個草圖。祝你好運。