到达特定点后如何使图像消失

时间:2018-10-24 12:54:41

标签: processing

我坚持挑战,我想让图像在到达特定点后立即消失。

就我而言,我正在制造一只吃鱼的熊。您必须使用箭头键移动鱼,一旦鱼在熊的嘴里,我就希望鱼消失。

有人可以帮我吗?

(也欢迎提供有关我的代码的其他提示)

这是我的代码:

    import controlP5.*;
    import processing.serial.*;

    ControlP5 cp5;
    PImage vis;
    int pvisx=700;//horizontal position of the fish
    int pvisy=800;//vertical poition of the fish
    float angle;//angle between the branches of the tree


    void setup() {
       background(10, 216, 255);
       size(1000, 1000);
       vis = loadImage ("vis geknipt.png");

       cp5 = new ControlP5(this);
       cp5.addTextfield("name")
       .setPosition(350,25)
       .setSize(300,45);

        }

     void draw() {
       //draw a bear
       smooth();
       noStroke();
       fill(95, 12, 12);//make the bear brown
       ellipse(500, 550, 200, 200);//head of bear
       ellipse(500, 785, 250, 300);//body of bear
       ellipse(400, 470, 100, 100);//left ear
       ellipse(600, 470, 100, 100);//right ear
       ellipse(430, 950, 50, 100);//left leg
       ellipse(560, 950, 50, 100);//right leg
       ellipse(360, 700, 110, 60);//left arm
       ellipse(635, 700, 110, 60);//right arm
       fill(165, 42, 42);
       ellipse(500, 785, 165, 190);//innerbody of bear
       fill(250, 189, 242);//pink inner ears
       ellipse(600, 470, 60, 40);//inner ear right
       ellipse(400, 470, 60, 40);//inner ear left
       fill(165, 42, 42);//light brownish snout
       ellipse (500, 590, 150, 90);//ellipse for snout
       stroke(0);
       line(500, 547, 500, 600);//line for mouth
       noFill();
       beginShape();//smile to the left
       curveVertex(500, 600);
       curveVertex(500, 600);
       curveVertex(465, 600);
       curveVertex(445, 585);
       curveVertex(445, 585);
       endShape();
       beginShape();//smile to the right
       curveVertex(500, 600);
       curveVertex(500, 600);
       curveVertex(535, 600);
       curveVertex(555, 585);
       curveVertex(555, 585);
       endShape();
       fill(255);
       ellipse(465, 520, 40, 60);//big eyes (left)
       ellipse(530, 520, 40, 60);//big eyes (right)
       fill(0);
       noStroke();
       ellipse(465, 533, 35, 35);//small eyes(left)
       ellipse(530, 533, 35, 35);//small eyes (right)
       fill(255, 0, 0);//red nose
       ellipse(500, 558, 50, 30);//nose

       //body of the tree (main branch)
       pushMatrix();
       strokeWeight(5);
       angle = radians(35);//angle between each branch of the tree
       translate(width/100, height);//start tree 
       stroke(95, 12, 12);//green color of the lines of the tree
       line(0, 0, 0, -450);//main branch of tree
       translate(0, -450);//translate to the end of the line, the leaves 
       will grow from there
       branch(250);//start of the branching (second, third etc)
       popMatrix();


       smooth();
       noStroke();
         for (int i = 1; i < 150; i++) {//making a sun by a for loop in an 
      ellipse to get a color gradient inside
          fill(255, 180, float(i)*2);
         ellipse(900, 80, 300-(2*i), 300-(2*i));
       }
       }

    void branch(float len) {

       len *= 0.7;//each branch will be 2/3rds of the size of the previous
       if (len > 5) { //when the length of the branch is 5 pixels or less, 
      the condition stops
           pushMatrix();    // make a change in just a small piece of 
            information
             rotate(angle);   // Rotate by the angle

        line(0, 0, 0, -len);  // Draw the branch
        stroke(0, 255, 0);
        translate(0, -len); // Move to the end of the branch
        branch(len);       //new branch is drawn
        popMatrix();     // indicate the end of making the change of 
        information

               // Repeat the same thing, only branch off to the "left" this 
                 time!
        pushMatrix();
        rotate(-angle);
        stroke(36, 198, 61);
        line(0, 0, 0, -len);
        translate(0, -len);
        branch(len);
        popMatrix();
         }
        }

     void name (String value) {
            println("My name is", value);
            }

       void keyPressed() {
        background(10, 216, 255);//the fishes won't repeat
        if (key == CODED) {//indicates which key is coded
        if (keyCode == UP) {//move fish up
          image(vis, pvisx, pvisy, width/4, height/8); 
          pvisy-=10;
        }
       }
      if (keyCode == DOWN) {//move fish down
        image(vis, pvisx, pvisy, width/4, height/8);
        pvisy += 10;
      }
      if (keyCode == RIGHT) {//move fish to the right
        image(vis, pvisx, pvisy, width/4, height/8);
        pvisx += 10;
       }
       if (keyCode == LEFT) {// move fish to the left
         image(vis, pvisx, pvisy, width/4, height/8);
        pvisx -= 10;
        }
       }

1 个答案:

答案 0 :(得分:0)

听起来您正在寻找碰撞检测,特别是矩形-矩形碰撞检测

Google和搜索Stack Overflow是您最好的朋友,因为该主题已经讨论了很多次。

我能为您提供的最佳建议是break your problem down into smaller steps,一次一次地执行这些步骤。例如,从一个简单的草图开始,该草图显示两个硬编码的矩形,并编写一些在相交时会更改其颜色的代码。在尝试将其集成到更大的代码库之前,先使它工作。

无耻的自我促进:here是有关处理中冲突检测的教程。