【问题标题】:How to make image disappear upon collision with rectangle in processing?如何在处理中与矩形碰撞时使图像消失?
【发布时间】:2017-12-19 22:34:42
【问题描述】:

我正在为我的大学项目制作游戏,我需要帮助。我正在制作一个游戏,您可以在 y 坐标上用鼠标控制汽车。您必须避开人群并收集在 y 坐标上移动的硬币。我希望在 处理 3 中与我的汽车的矩形部分发生碰撞时,我放入的人和硬币的图像消失。这是我的代码:

PImage img,img1,img2,img3,img4,img5;
int x,y;
float ypos=0;
float ypos2=4;

void setup()
   {
     size(1000,585);
     img = loadImage("person1.png");
     img1 = loadImage("sun.png");
     img2 = loadImage("coin.png");
     img3 = loadImage("person2.png");
     img4 = loadImage("person3.png");
     img5 = loadImage("person4.png");
     x=width/2;
     y=height/2;
   }
void draw()
{
  background(170,200,255);
  image(img1,720,-30,160,160);
  fill(255,240,50);
  ellipse(800,50,85,85);
  fill(200,255,150);
  rect(0,200,1000,400);
  fill(0,0,0);


  rect((-frameCount%200)*10+1000,130,90,70);
  rect((-frameCount%200)*10+1090,100,50,100);
  rect((-frameCount%200)*10+1500,130,90,70);
  rect((-frameCount%200)*10+1900,100,50,100);


  fill(200,200,200);
  rect(0,250,1000,90);
  fill(200,200,200);
  rect(0,350,1000,90);
  fill(200,200,200);
  rect(0,450,1000,90);
  fill(255,100,100);


  rect(0,mouseY+0,200,80);
  fill(0);
  ellipse(50,mouseY+60,60,60);
  fill(0);
  ellipse(150,mouseY+60,60,60);
  fill(100);
  ellipse(50,mouseY+60,40,40);
  fill(100);
  ellipse(150,mouseY+60,40,40);
  fill(255,100,100);
  arc(100,mouseY+0,160,150,PI,TWO_PI);
  fill(160,210,300);
  arc(100,mouseY+0,130,130,PI,TWO_PI);
  fill(255,100,100);
  rect(95,mouseY+0,10,-75);
  fill(0);
  rect(99,mouseY+0,2,80);


  image(img5,(-frameCount%300)*10+2500,250+sin(ypos)*100,120,120);
  ypos +=0.01;
  image(img4,(-frameCount%450)*5+2000,400+sin(ypos)*140,120,120);
  ypos +=0.01;
  image(img,(-frameCount%300)*5+1000,300+sin(ypos)*50,120,120);
  ypos +=0.01;
  image(img2,(-frameCount%100)*20+1000,300-sin(ypos2)*130,50,50);
  ypos2 +=0.05;
  image(img3,(-frameCount%400)*5+1600,250-sin(ypos)*100,120,120);
  ypos +=0.08;
  image(img2,(-frameCount%300)*20+1900,300+sin(ypos2)*130,50,50);
  ypos2 +=0.05;
}

【问题讨论】:

    标签: image processing


    【解决方案1】:

    您需要执行矩形-矩形碰撞检测。 Google 是您的朋友,但基本情况如下所示:

    //evaluates to true if rectOne and rectTwo are colliding
    if(rectOneRight > rectTwoLeft && rectOneLeft < rectTwoRight && rectOneBottom > rectTwoTop && rectOneTop < rectTwoBottom){
    

    无耻的自我推销:我在Processing available here写了一个关于碰撞检测的教程。

    如果您仍然遇到问题,请在新问题帖子中发布 MCVE 以及更具体的问题。请注意,这不应该是您的完整项目,而只是一个小例子。尝试创建一个程序,显示两个硬编码的矩形,如果它们发生碰撞,它们会改变颜色。祝你好运。

    【讨论】:

    • 感谢您回答我的问题,但我不知道如何将其添加到我的代码中,您能否告诉我我需要在哪里添加它?我需要在代码中添加更多信息吗?矩形击中后如何使图像停止出现?是否有某种“停止”命令?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多