如何在两个 类 之间进行碰撞?

How do I make collision between two classes?

我需要帮助并尽快<3 所以这是代码,我希望游戏在脸 class 接触到敌人 class 时停止!!碰撞或其他原因。我有脸、主要、眼睛、敌人、消息 class。我已经问过这个问题,但我尝试将它们放在一起但没有用。请帮助我,非常感谢<3

Face f1;
Enemy e1,e2,e3,e4;
Message m1, m2, m3, m4;
int currentTime;
int enemy;


void setup()
{
  size (600,600);
  f1=new Face(50,200,300,0,255,0); 
  e1 = new Enemy(200,300,30,-5,255,0,0);
  e2=new Enemy(400,200,40,-8,255,0,0);
  e3=new Enemy(100,500,50,-7,255,0,0);
  e4=new Enemy(550,400,30,-6,255,0,0);
  m1 = new Message(width/2, height/2,    1000,4000, "wear a mask");
  m2 = new Message(width/2, height/2, 5000,9000, "avoid contacts");
  m3 = new Message(width/2, height/2, 10000,14000, "wash your hands");
  m4 = new Message(width/2, height/2, 15000,19000, "stay safe");
  
}
 



void draw()
{
  background (0);
  currentTime = millis();
  f1.display();
  e1.move();
  e1.display();
  e2.display();
  e2.move();
  e3.display();
  e3.move();
  e4.display();
  e4.move();
  m1.setTime(currentTime);
  m2.setTime(currentTime);
  m3.setTime(currentTime);
  m4.setTime(currentTime);
  m1.display();
  m2.display();
  m3.display();
  m4.display();
  
 



  fill(35, 20, 219);
  rect(200, 240, 180, 30); 
  rect(0, 0, 600, 10); 
  rect(0, 0, 10, 600); 
  rect(0, 590, 600, 10);
  rect(590, 0, 10, 600);
  rect(40, 50, 50, 20);
  rect(140, 50, 50, 20);
  rect(0, 220, 40, 140);
  rect(0, 400, 40, 20);
  rect(80, 580, 200, 20);
  rect(340, 580, 200, 20);
  rect(240, 500, 150, 20);
  rect(300, 500, 20, 50);
  rect(240, 380, 150, 20);
  rect(300, 380, 20, 70);
  rect(390, 450, 60, 15);
  rect(500, 450, 90, 15);
  rect(500, 480, 20, 50);
  rect(120, 420, 20, 80);
  rect(90, 500, 90, 20);
  rect(280, 0, 20, 60);
  rect(380, 50, 40, 20);
  rect(480, 50, 40, 20);
  rect(480, 280, 20, 100);
  rect(450, 260, 80, 20);
  rect(150, 120, 300, 20);
  rect(275, 140, 25, 40);
  rect(100, 270, 20, 100);fill(35, 20, 219);
  rect(200, 240, 180, 30); 
  rect(0, 0, 600, 10); 
  rect(0, 0, 10, 600); 
  rect(0, 590, 600, 10);
  rect(590, 0, 10, 600);
  rect(40, 50, 50, 20);
  rect(140, 50, 50, 20);
  rect(0, 220, 40, 140);
  rect(0, 400, 40, 20);
  rect(80, 580, 200, 20);
  rect(340, 580, 200, 20);
  rect(240, 500, 150, 20);
  rect(300, 500, 20, 50);
  rect(240, 380, 150, 20);
  rect(300, 380, 20, 70);
  rect(390, 450, 60, 15);
  rect(500, 450, 90, 15);
  rect(500, 480, 20, 50);
  rect(120, 420, 20, 80);
  rect(90, 500, 90, 20);
  rect(280, 0, 20, 60);
  rect(380, 50, 40, 20);
  rect(480, 50, 40, 20);
  rect(480, 280, 20, 100);
  rect(450, 260, 80, 20);
  rect(150, 120, 300, 20);
  rect(275, 140, 25, 40);
  rect(100, 270, 20, 100);
  
 
}
 
  
 
     
      
  

  

void keyPressed()
{
  if(key==CODED)
  {
    if(keyCode==LEFT)
    {
     f1.moveLeft();
    }
    if(keyCode==RIGHT)
    {
      f1.moveRight();
    }
    if(keyCode==UP)
    {
     f1.moveUp();
    }
    if(keyCode==DOWN)
    {
     f1.moveDown();
    }
    
 
  }

}
----------------------------------------------
class Eye
{
  int diameter;
  int x;
  int y;
  int grey;
  
  Eye(int d, int xcor, int ycor, int g)
  {
    grey=g;
    diameter=d;
    x=xcor;
    y=ycor;
  }
  void display()
  {
    fill(grey);
    ellipse(x,y,diameter,diameter);
  }
  int getX()
  {
    return x;
  }
  void setX(int xx)
  {
    x=xx;
 }
   int getY()
  {
    return y;
  }
  void setY(int yy)
  {
    y=yy;
 }
 
 
 
 
}
----------------------------------------------
class Enemy
{
  int x;
  int y;
  int diameter;
  int step;
  int r;
  int g;
  int b;
  
  
  Enemy(int a,int b,int c,int d,int rc,int gc,int bc)
  {
    x=a;
    y=a;
    diameter=c;
    step=d;
    r=rc;
    g=gc;
    b=bc;
}
void display()
{ 
  fill(r, g, b);
  stroke(0,0,255);
  ellipse(x,y,diameter,diameter);
  
}

void move()
{
  y=y+step;
  if(y<diameter)
  step=-step;
  if(y>600)
  step=-step;
}
void moveUp()
{ 
  if(y>=5+diameter/2)
  {
    y=y-5;
  }
}
void moveDown()
{
  if(y<=height-5-diameter/2)
  {
    y=y+5;
  }
}

----------------------------------------------
class Face
{
  int diameter;
  int x;
  int y;
  int r;
  int g;
  int b;
  Eye leftEye;
  Eye rightEye;
  
  Face(int d, int xcor, int ycor,int rc,int gc,int bc)
  {
    diameter=d;
    y=ycor;
    x=xcor;
    r=rc;
    g=gc;
    b=bc;
    leftEye=new Eye(10,x-10,y-10,255);
    rightEye=new Eye(10,x+10,y-10,250);
  }
  void display()
  {
    fill(255, g, b);
    stroke(255,0,0);
    ellipse(x,y,diameter,diameter);
    leftEye.display();
    rightEye.display();
  }
  void moveLeft()
  {
    if(x>diameter/2)
    {
      x=x-10;
      leftEye.setX(leftEye.getX()-10);
      rightEye.setX(rightEye.getX()-10);
   }
  }
  void moveRight()
  {
    if(x<width-diameter/2)
    {
    x=x+10;
      leftEye.setX(leftEye.getX()+10);
      rightEye.setX(rightEye.getX()+10);
    }
  }
  void moveUp()
{ 
  if(y>=5+diameter/2)
  {
    y=y-10;
    leftEye.setY(leftEye.getY()-10);
    rightEye.setY(rightEye.getY()-10);
  }
}
 
    void moveDown()
{
  if(y<=height-5-diameter/2)
  {
    y=y+10;
    leftEye.setY(leftEye.getY()+10);
    rightEye.setY(rightEye.getY()+10);
    
  }
}
void changeColor()
{
  r=(r+20)%255;
}



  }
  --------------------------------------------
class Message
{
int startTime;
int stopTime;
String str;
int x;
int y;
boolean visible;

Message(int xcor, int ycor, int t1,int t2, String s)
{
  x = xcor;
  y = ycor;
  startTime = t1;
  stopTime = t2;
  str = s;
  visible = false;
}

void display()
{
  textSize(24);
  if(visible == true)
   {
     text(str, x, y);
   }
}
  
void setTime(int time)
{
    if(time>=startTime && time<= stopTime)
    {
      visible = true;   
    }
    else
    {
      visible = false;   
    }
}

----------------------------------------------

一般来说,最好使用较小的程序来缩小问题范围并测试假设,重点放在该问题上。目前你已经发布了很多有很多语法错误的代码。您需要一次修复一个语法错误。

关于您的家庭作业目标:

the game to stop when the face class touches the enemy class

这应该是直截了当的,因为:

  • 你知道面部实例(不是class)和敌人实例的位置。
  • 你知道每个的直径
  • 如果两者之间的距离小于它们的半径之和,则一定是碰撞

半径只是直径的一半,关于距离,Processing 使用 dist(x1, y1, x2, y2) 函数使这变得微不足道。只需给它两个实例(脸和敌人)的 x,y 坐标对,它就会 return 距离。 (无需手动计算毕达哥拉斯定理,dist() 为您代劳)

检查此 Processing rollover example 并特别注意 overCircle 函数。在您的场景中:

if(dist(f1.x, f1.y, e1.x, e1.y) < ( (f1.diameter + e1.diameter) / 2) ){
  noLoop();
}

不清楚您是否已经学习了数组和 for 循环。这将有助于避免手动复制粘贴和修改其余敌人的条件。