Processing 基本台球游戏问题

Processing 基本台球游戏问题,processing,Processing,我试图创建一个基本的台球游戏,其中一个球击中另一个球,并使第二个球继续在同一方向以相同的速度移动,移动的距离与第一个球移动的距离相同。到目前为止,除了让第二个球继续进行,我已经让一切都开始了。有人能帮我做这件事吗?我认为我的问题在于代码的click==4部分,但我不知道如何修复它/添加到它上 Ball cue, billiard; boolean fired = false; String msg; int click; int steps = 20; int difx, dify; Boole

我试图创建一个基本的台球游戏,其中一个球击中另一个球,并使第二个球继续在同一方向以相同的速度移动,移动的距离与第一个球移动的距离相同。到目前为止,除了让第二个球继续进行,我已经让一切都开始了。有人能帮我做这件事吗?我认为我的问题在于代码的click==4部分,但我不知道如何修复它/添加到它上

Ball cue, billiard;
boolean fired = false;
String msg;
int click;
int steps = 20;
int difx, dify;
Boolean move = false;

void setup(){
msg = "";
size(600,300);
click = 0;
cue = new Ball(30, #FFFFFF);
billiard = new Ball(30, #000000);
}

void draw(){
background(#009900);
if(click == 0){
cue.xpos = mouseX;
cue.ypos = mouseY;
billiard.xpos = -15;
billiard.ypos = -15;
msg = "please place the cue ball";
}else if(click == 1){
billiard.xpos = mouseX;
billiard.ypos = mouseY;
msg = "click again to place billiard ball";
}else if(click ==2){
difx = cue.xpos-billiard.xpos;
dify = cue.ypos-billiard.ypos;
}else if(click == 3){
float cdistance = dist(cue.xpos,cue.ypos,billiard.xpos,billiard.ypos);
if(cdistance>billiard.ballDiam/2){
  move = true;
  cue.xpos-=difx/steps;
  cue.ypos-=dify/steps;
  msg = "You got it! Push c on your keyboard to restart";  
}else{
move = false;
cue.visible = true;
click = 4;
}
}else if(click == 4){
float cdistance = dist(cue.xpos,cue.ypos,billiard.xpos,billiard.ypos);
if(cdistance<billiard.ballDiam){
  if (dist(cue.xpos, cue.ypos, billiard.xpos, billiard.ypos) < sqrt(sq(difx)+sq(dify))) {
  move = true;
  billiard.xpos-=difx/steps;
  billiard.ypos-=difx/steps;
  }
}
}
cue.update();
billiard.update();
textSize(20);
text(msg,0,height-5);
}

void mouseClicked(){
 if(!move){
 click++;
 }
}

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

void update(){
  if(visible){
  fill(myColor);
  ellipse(xpos,ypos,ballDiam,ballDiam);
  }
 }
}
球杆、台球;
布尔值=假;
串味精;
点击;
int步数=20;
int-difx,dify;
布尔移动=假;
无效设置(){
msg=“”;
尺寸(600300);
单击=0;
球杆=新球(30,#FFFFFF);
台球=新球(30,#000000);
}
作废提款(){
背景(#009900);
如果(单击==0){
cue.xpos=mouseX;
cue.ypos=mouseY;
billiard.xpos=-15;
台球:ypos=-15;
msg=“请放置主球”;
}否则如果(单击==1){
billiard.xpos=mouseX;
billiard.ypos=鼠标;
msg=“再次单击以放置台球”;
}否则如果(单击==2){
difx=cue.xpos-billiard.xpos;
dify=cue.ypos-billiard.ypos;
}否则如果(单击==3){
float cdistance=dist(cue.xpos、cue.ypos、billiar.xpos、billiar.ypos);
if(距离>台球直径/2){
move=true;
cue.xpos-=difx/步;
提示ypos-=dify/步数;
msg=“明白了!按键盘上的c键重新启动”;
}否则{
move=false;
cue.visible=true;
点击=4;
}
}否则如果(单击==4){
float cdistance=dist(cue.xpos、cue.ypos、billiar.xpos、billiar.ypos);
如果(cdistance这是您的解决方案(但您也必须重新设计您的程序):

球杆、台球;
布尔值=假;
串味精;
点击;
int步数=20;
int-difx,dify;
布尔移动=假;
布尔连续移动;
无效设置(){
msg=“”;
大小(600300);
单击=0;
球杆=新球(30,#FFFFFF);
台球=新球(30,#000000);
继续前进=错误;
}
作废提款(){
背景(#009900);
如果(单击==0){
cue.xpos=mouseX;
cue.ypos=mouseY;
billiard.xpos=-15;
台球:ypos=-15;
msg=“请放置主球”;
}
否则如果(单击==1){
billiard.xpos=mouseX;
billiard.ypos=鼠标;
msg=“再次单击以放置台球”;
}
否则如果(单击==2){
difx=cue.xpos-billiard.xpos;
dify=cue.ypos-billiard.ypos;
}
否则如果(单击==3){
浮动距离=距离(cue.xpos、cue.ypos、billiard.xpos、billiard.ypos);
if(距离>台球直径/2){
move=true;
cue.xpos-=difx/步;
提示ypos-=dify/步数;
msg=“明白了!按键盘上的c键重新启动”;
}
否则{
move=false;
cue.visible=true;
点击=4;
}
}
否则如果(单击==4)
{
浮动距离=距离(cue.xpos、cue.ypos、billiard.xpos、billiard.ypos);

如果(cdistance您的第一站应该是处理过程中附带的示例,特别是运动部分中的CircleCollision示例。该示例包括使用或多或少真实的物理将球弹起的所有逻辑

/**
 * Circle Collision with Swapping Velocities
 * by Ira Greenberg. 
 * 
 * Based on Keith Peter's Solution in
 * Foundation Actionscript Animation: Making Things Move!
 */

Ball[] balls =  { 
  new Ball(100, 400, 20), 
  new Ball(700, 400, 80) 
};

void setup() {
  size(640, 360);
}

void draw() {
  background(51);

  for (Ball b : balls) {
    b.update();
    b.display();
    b.checkBoundaryCollision();
  }

  balls[0].checkCollision(balls[1]);
}

class Ball {
  PVector position;
  PVector velocity;

  float r, m;

  Ball(float x, float y, float r_) {
    position = new PVector(x, y);
    velocity = PVector.random2D();
    velocity.mult(3);
    r = r_;
    m = r*.1;
  }

  void update() {
    position.add(velocity);
  }

  void checkBoundaryCollision() {
    if (position.x > width-r) {
      position.x = width-r;
      velocity.x *= -1;
    } 
    else if (position.x < r) {
      position.x = r;
      velocity.x *= -1;
    } 
    else if (position.y > height-r) {
      position.y = height-r;
      velocity.y *= -1;
    } 
    else if (position.y < r) {
      position.y = r;
      velocity.y *= -1;
    }
  }

  void checkCollision(Ball other) {

    // get distances between the balls components
    PVector bVect = PVector.sub(other.position, position);

    // calculate magnitude of the vector separating the balls
    float bVectMag = bVect.mag();

    if (bVectMag < r + other.r) {
      // get angle of bVect
      float theta  = bVect.heading();
      // precalculate trig values
      float sine = sin(theta);
      float cosine = cos(theta);

      /* bTemp will hold rotated ball positions. You 
       just need to worry about bTemp[1] position*/
      PVector[] bTemp = {
        new PVector(), new PVector()
        };

        /* this ball's position is relative to the other
         so you can use the vector between them (bVect) as the 
         reference point in the rotation expressions.
         bTemp[0].position.x and bTemp[0].position.y will initialize
         automatically to 0.0, which is what you want
         since b[1] will rotate around b[0] */
        bTemp[1].x  = cosine * bVect.x + sine * bVect.y;
      bTemp[1].y  = cosine * bVect.y - sine * bVect.x;

      // rotate Temporary velocities
      PVector[] vTemp = {
        new PVector(), new PVector()
        };

        vTemp[0].x  = cosine * velocity.x + sine * velocity.y;
      vTemp[0].y  = cosine * velocity.y - sine * velocity.x;
      vTemp[1].x  = cosine * other.velocity.x + sine * other.velocity.y;
      vTemp[1].y  = cosine * other.velocity.y - sine * other.velocity.x;

      /* Now that velocities are rotated, you can use 1D
       conservation of momentum equations to calculate 
       the final velocity along the x-axis. */
      PVector[] vFinal = {  
        new PVector(), new PVector()
        };

      // final rotated velocity for b[0]
      vFinal[0].x = ((m - other.m) * vTemp[0].x + 2 * other.m * vTemp[1].x) / (m + other.m);
      vFinal[0].y = vTemp[0].y;

      // final rotated velocity for b[0]
      vFinal[1].x = ((other.m - m) * vTemp[1].x + 2 * m * vTemp[0].x) / (m + other.m);
      vFinal[1].y = vTemp[1].y;

      // hack to avoid clumping
      bTemp[0].x += vFinal[0].x;
      bTemp[1].x += vFinal[1].x;

      /* Rotate ball positions and velocities back
       Reverse signs in trig expressions to rotate 
       in the opposite direction */
      // rotate balls
      PVector[] bFinal = { 
        new PVector(), new PVector()
        };

      bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y;
      bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x;
      bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y;
      bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x;

      // update balls to screen position
      other.position.x = position.x + bFinal[1].x;
      other.position.y = position.y + bFinal[1].y;

      position.add(bFinal[0]);

      // update velocities
      velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y;
      velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x;
      other.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y;
      other.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x;
    }
  }


  void display() {
    noStroke();
    fill(204);
    ellipse(position.x, position.y, r*2, r*2);
  }
}
/**
*具有交换速度的圆碰撞
*艾拉·格林伯格。
* 
*基于Keith Peter在
*基础动作脚本动画:让事物移动!
*/
Ball[]balls={
新球(10040020),
新球(700、400、80)
};
无效设置(){
尺寸(640360);
}
作废提款(){
背景(51);
用于(球b:球){
b、 更新();
b、 显示();
b、 checkBoundaryCollision();
}
balls[0]。选中碰撞(balls[1]);
}
班级舞会{
PVector位置;
PVector速度;
浮动r,m;
球(浮子x、浮子y、浮子r){
位置=新的PVector(x,y);
速度=PVector.random2D();
速度mult(3);
r=r;
m=r*.1;
}
无效更新(){
位置。添加(速度);
}
void checkBoundaryCollision(){
如果(位置x>宽度r){
位置x=宽度-r;
速度x*=-1;
} 
否则如果(位置x高度r){
位置y=高度-r;
速度y*=-1;
} 
否则如果(位置y/**
 * Circle Collision with Swapping Velocities
 * by Ira Greenberg. 
 * 
 * Based on Keith Peter's Solution in
 * Foundation Actionscript Animation: Making Things Move!
 */

Ball[] balls =  { 
  new Ball(100, 400, 20), 
  new Ball(700, 400, 80) 
};

void setup() {
  size(640, 360);
}

void draw() {
  background(51);

  for (Ball b : balls) {
    b.update();
    b.display();
    b.checkBoundaryCollision();
  }

  balls[0].checkCollision(balls[1]);
}

class Ball {
  PVector position;
  PVector velocity;

  float r, m;

  Ball(float x, float y, float r_) {
    position = new PVector(x, y);
    velocity = PVector.random2D();
    velocity.mult(3);
    r = r_;
    m = r*.1;
  }

  void update() {
    position.add(velocity);
  }

  void checkBoundaryCollision() {
    if (position.x > width-r) {
      position.x = width-r;
      velocity.x *= -1;
    } 
    else if (position.x < r) {
      position.x = r;
      velocity.x *= -1;
    } 
    else if (position.y > height-r) {
      position.y = height-r;
      velocity.y *= -1;
    } 
    else if (position.y < r) {
      position.y = r;
      velocity.y *= -1;
    }
  }

  void checkCollision(Ball other) {

    // get distances between the balls components
    PVector bVect = PVector.sub(other.position, position);

    // calculate magnitude of the vector separating the balls
    float bVectMag = bVect.mag();

    if (bVectMag < r + other.r) {
      // get angle of bVect
      float theta  = bVect.heading();
      // precalculate trig values
      float sine = sin(theta);
      float cosine = cos(theta);

      /* bTemp will hold rotated ball positions. You 
       just need to worry about bTemp[1] position*/
      PVector[] bTemp = {
        new PVector(), new PVector()
        };

        /* this ball's position is relative to the other
         so you can use the vector between them (bVect) as the 
         reference point in the rotation expressions.
         bTemp[0].position.x and bTemp[0].position.y will initialize
         automatically to 0.0, which is what you want
         since b[1] will rotate around b[0] */
        bTemp[1].x  = cosine * bVect.x + sine * bVect.y;
      bTemp[1].y  = cosine * bVect.y - sine * bVect.x;

      // rotate Temporary velocities
      PVector[] vTemp = {
        new PVector(), new PVector()
        };

        vTemp[0].x  = cosine * velocity.x + sine * velocity.y;
      vTemp[0].y  = cosine * velocity.y - sine * velocity.x;
      vTemp[1].x  = cosine * other.velocity.x + sine * other.velocity.y;
      vTemp[1].y  = cosine * other.velocity.y - sine * other.velocity.x;

      /* Now that velocities are rotated, you can use 1D
       conservation of momentum equations to calculate 
       the final velocity along the x-axis. */
      PVector[] vFinal = {  
        new PVector(), new PVector()
        };

      // final rotated velocity for b[0]
      vFinal[0].x = ((m - other.m) * vTemp[0].x + 2 * other.m * vTemp[1].x) / (m + other.m);
      vFinal[0].y = vTemp[0].y;

      // final rotated velocity for b[0]
      vFinal[1].x = ((other.m - m) * vTemp[1].x + 2 * m * vTemp[0].x) / (m + other.m);
      vFinal[1].y = vTemp[1].y;

      // hack to avoid clumping
      bTemp[0].x += vFinal[0].x;
      bTemp[1].x += vFinal[1].x;

      /* Rotate ball positions and velocities back
       Reverse signs in trig expressions to rotate 
       in the opposite direction */
      // rotate balls
      PVector[] bFinal = { 
        new PVector(), new PVector()
        };

      bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y;
      bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x;
      bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y;
      bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x;

      // update balls to screen position
      other.position.x = position.x + bFinal[1].x;
      other.position.y = position.y + bFinal[1].y;

      position.add(bFinal[0]);

      // update velocities
      velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y;
      velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x;
      other.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y;
      other.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x;
    }
  }


  void display() {
    noStroke();
    fill(204);
    ellipse(position.x, position.y, r*2, r*2);
  }
}