对象在处理过程中相遇时的短语

问题描述

我一直试图弄清楚如何在处理代码中的对象相互碰撞时添加某些单词。有谁知道当发射的红球触及屏幕上半部分的黑色圆圈时,我如何让我的屏幕说“你赢了”?我想知道需要做什么才能实现这一目标。

这是我的代码

Ball mainBall; // Horizontal ball
ArrayList<Ball> balls; //Red balls

PVector oldSpeed;

void setup () {  
  size (1280,960);
  
  //Create main ball
  mainBall = new Ball(200,700,3,140,color(255,200,200));
  
  // Create list of balls
  balls = new ArrayList<Ball>();
}

void draw() {
  background(255);

  fill (0);
  rect (0,550,width,height);
  ellipse(240,240,100,100);
  
  ellipseMode(CENTER);


  mainBall.collisionUpdate();  //reverse speed of ball if hits side window
  mainBall.drawBall();   //Draw the main ball
  
  //Create arraylist to store balls to get removed from the list
  ArrayList<Ball> toRemove = new ArrayList<Ball>();
  
  // For every ball
  for(Ball ball : balls){
    // Balls bounce within the screen
    ball.collisionUpdate();
    
    ball.slowSpeed(random(0.98,0.999)); //  Speed of the ball
    
    ball.drawBall();
    

    if(ball.isNotVisible()){
      toRemove.add(ball); //Removal of ball if off screen
    }
  }
  

  balls.removeAll(toRemove);   // Remove all the non-visible balls
}

void launch(){
  if(!mainBall.isstationary()){
    oldSpeed = mainBall.speed;
    mainBall.setSpeed(0,0);
  }else{
    // Add a new ball to the list
    balls.add(new Ball(mainBall.position.x,mainBall.position.y,random(-8,-5),70,0)));
    //Make the main ball move again
    mainBall.setSpeed(oldSpeed);
  }
}

void mouseClicked(){
  launch();
}

void keypressed(){
  launch();
}

// Ball class
class Ball{
  PVector position;
  PVector speed;
  float diameter;
  color c;
  
  Ball(float x,float y,float xSpeed,float ySpeed,float _diameter,color _c){
    position = new PVector(x,y);
    speed = new PVector(xSpeed,ySpeed);
    diameter = _diameter;
    c = _c;
  }
  
  void drawBall(){
    fill(c);
    circle(position.x,position.y,diameter);
    // Update position of ball
    position.add(speed);
  }
  
  void setSpeed(float xSpeed,float ySpeed){
    speed = new PVector(xSpeed,ySpeed);
  }
  
  void setSpeed(PVector _speed){
    speed = _speed;
  }
  
  boolean isstationary(){
    return speed.x == 0 && speed.y == 0;
  }

  void collisionUpdate(){
    if(position.x < diameter/2 || position.x > width-diameter/2 || position.y < diameter/2 || position.y > height-diameter/2 ){
      //Reverse speed
      speed.mult(-1);
    }
  }
  
  void slowSpeed(float deceleration){
      if(speed.mag() < 0.5){
          speed = new PVector(0,0);
      }else{
          speed.mult(deceleration);
      }
  }
  
  // Return true or false if the ball is off the screen
  boolean isNotVisible(){
     return position.x > width+diameter || position.x < -diameter || position.y > height+diameter || position.y < -diameter;
  }
}

解决方法

这可能有效:

boolean winning = false;

void draw() {
  for (Ball ball: balls) {
    if (sq(ball.position.x - 240) + sq(ball.position.y - 240) < sq(ball.diameter/2 + 50)) {
      winning = true;//test each ball to see if it's close enough to the black one
    }
  }
  if (winning) {
    fill(255,0);
    stroke(255,0);
    textSize(70);
    textAlign(CENTER,CENTER);
    text("You Have Won",width/2,height/2);
  }
}