processing鼠标拖拽事件求解答

发布于:2022-11-01 ⋅ 阅读:(325) ⋅ 点赞:(0)

事情是这样的

我绘制的人物是在旋转的 要求是点击右键重置绘制的人物到初始位置 也就是零度 到这里我还是会的

后面就是点击右键 鼠标拖拽绘制人物 绘制人物根据鼠标的(x,y)变化也上下左右移动 重点是从绘制人物所在的位置改变 不能突然间改变他的角度或者位置 要在原有的位置上根据鼠标的坐标改变而上下移动

我写的代码是让绘制人物以鼠标为中心移动 但是这样就不满足人物要在他原先所处的位置上下左右移动,因为当点击右键的时候所绘制的人物就会被鼠标拖动。但是我不知道怎么改。因为绘制人物还要满足旋转,第三秒消失,当鼠标在左下角时人物消失,点击右键归零,还要实现拖拽

以下是老师的要求和我的代码

//global variables

//global variables
int fc = 0;//frame counter, 60fps is 1 sec
float pikAngDeg = mouseX*-0.8;//rotating angles of pikachu
float pikX = 8;//translate pikachu 8 units right
float pikY = 60;//translate pikachu 60 units left


void setup()
{
  println("... starting setup() function");//this is a print line of text
  
  size(700, 700);//sets app's size
  ellipseMode(CENTER);//draw ellipses from their centers
  background(#B2FCFF);//cyan color background
  
}//end of setup()


void draw()
{
  fc++;//count anaother framm
 
      //draw the background if the mouse is in the bottom 3/4 or left 1/2 
      //don't show it until after 4 sec
      int length = 700;//variable of the canvas' length and width
      if (fc > 240) 
    {
        if (mouseX < length * 1/2 || mouseY > length * 1/4)
        {
            drawTile(0,0);
            drawPokeballWall();
            drawPokeballWallRotate();
        } 
       else 
       {
         background(#B2FCFF);
       }
    } 
      
      else 
      {
        background(#B2FCFF);
      }
  
    //only draw hero before 2 seconds or after 4 seconds
    //and only if the mouse is in the right 3/4 or top 1/4 of the application
    if (fc < 120 || fc > 240 ) 
{
    if (mouseX > length * 1/4 || mouseY < length * 1/4) 
   {
    drawPikWithRotation(pikX,pikY);//translate pikachu to the center of the canvas
  }
}

   
   
   
   //press the key "s" and save image named "screenshot_2022_SCIE204_YongyingYang_a3.jpg"
   //press 's' again, it overwrotes the current file
   if (keyPressed)
  { 
     if (key == 's')
     {
      save("screenshot_2022_SCIE204_YongyingYang_a3.jpg"); 
     }
  }
  
    
   //reset pikachu's angle at 0 when clicking the right button 
   mouseClicked();
   
}//end of draw

//draw a tile place holder at a location xxx, yyy 
//unsed for transalations
void drawTile(int xxx, int yyy)
{
  
  pushMatrix();//start of isolating memory
  
  
  //transformation here
  translate(xxx,yyy);
  
  //drawing 1 tile on the top left corner of the canvas
  noStroke();
  fill(#F5D699);//yellow background color
  rect(0,0,100,100);//top left tile
  stroke(0);  
  strokeWeight(3); 
  fill(255);  
  ellipse(50,50,65,65);//the whole poke ball in white
  fill(#D80B0B);  
  arc(50,50,65,65,PI,TWO_PI);//upper half part of the poke ball in red
  line(20,50,80,50);//the middle line devides the red and white part
  fill(0);
  ellipse(50,50,25,25);//the middle black ellipse
  fill(255);  
  ellipse(50,50,20,20);//white ellipse drawn on top of the black ellipse
  stroke(220);  
  strokeWeight(2);
  ellipse(50,50,10,10);//the button of the poke ball
  
  
  popMatrix();//done isolating memory for this drawing
}
  
  


//draw a static 2d pokeball tile wall
void drawPokeballWall()
{
 for (int i=0; i<100; i++)
 {
  for (int j=0; j<100; j++)
  {
    drawTile(i*100, j*100);//the size of the tile wall is 10000*10000
  }
 }
}

//draw a rotating 2d tile wwall 
void drawPokeballWallRotate()
{
  pushMatrix();
  
  //transformations here
  //Transformations happen in reverse
  //how to rotate the tile wall
  //1 translate so the center of the wall is at 5000,5000, 
  //the size of the wall is 10000*10000
  //2 rotate
  //3 move the rotating wall to the mouse position
  
  translate(mouseX, mouseY);
  rotate(radians(fc*0.1));//rotate in very slow pace
  translate(-5000,-5000);
  
  
 //drawing stuff here
 //make the static 2d pokeball wall rotate
 drawPokeballWall();
 
  popMatrix();
}

//draw a pikachu place holder at a location xxx, yyy
void drawPikWithRotation(float xxx, float yyy)
{
  pushMatrix();//start isolation of memory
  
  //Translation here
  translate(xxx, yyy);
   //Translations happen in reverse
  //1 move pikachu to the origin
  //2 rotate it
  //3 move back to center
  translate(width/2,height/2);//put pikachu back into center
  rotate(radians(pikAngDeg));//rotate pikachu to the origin
  translate(-width/2,-height/2);//move pik to origin
  pikAngDeg = pikAngDeg+1;//increasing rotating angle by 1 radian
 
  
  //drawing hero here
  //drawShadow()
  //colored muddy yellow
  fill(#BC8D09,200);
  noStroke();
  ellipse(330,400,200,120);
  
  //drawTail()
  //colored yellow
  fill(#FCEF78);
  stroke(#583D3A);
  strokeWeight(3);
  beginShape();
  vertex(390,290);
  vertex(490,265);
  vertex(510,320);
  vertex(440,330);
  vertex(450,360);
  vertex(430,365);
  vertex(435,380);
  vertex(420,385);
  endShape();
  
  //tail shadow
  fill(#FACF62);
  noStroke();
  beginShape();
  vertex(400,300);
  vertex(480,285);
  vertex(495,313);
  vertex(430,320);
  vertex(440,355);
  vertex(428,360);
  vertex(435,380);
  vertex(420,385);
  endShape();
  
  //brown patch of the tail
  fill(#583D3A);
  quad(420,385,420,370,435,375,435,380);
  
  //body and head
  fill(#FCEF78);
  stroke(#583D3A);
  strokeWeight(3);
  beginShape();
  curveVertex(260,280);
  curveVertex(260,280);
  curveVertex(250,270);
  curveVertex(240,255);
  curveVertex(238,240);
  curveVertex(250,220);
  curveVertex(250,210);
  curveVertex(250,180);
  curveVertex(280,150);
  curveVertex(320,140);
  curveVertex(350,145);
  curveVertex(380,160);
  curveVertex(400,195);
  curveVertex(400,200);
  curveVertex(405,220);
  curveVertex(405,250);
  curveVertex(390,280);
  curveVertex(390,280);
  curveVertex(390,280);
  curveVertex(415,340);
  curveVertex(415,400);
  curveVertex(380,430);
  curveVertex(340,420);
  curveVertex(310,400);
  curveVertex(285,410);
  curveVertex(270,410);
  curveVertex(250,400);
  curveVertex(240,380);
  curveVertex(242,360);
  curveVertex(248,340);
  curveVertex(255,327);
  curveVertex(255,327);
  endShape();
   
  //strip of body
  fill(#583D3A);
  triangle(420,370,405,365,415,355);
  triangle(410,330,400,335,406,315);
  
  //tummy shadow
  fill(#FACF62);
  noStroke();
  ellipse(323,335,70,100);
  
  //hands color in yellow, brown stroke and 3 units stoke wights
  fill(#FCEF78);
  stroke(#583D3A);
  strokeWeight(3);
  
  //left hand
  beginShape();
  curveVertex(250,275);
  curveVertex(255,283);
  curveVertex(255,320);
  curveVertex(260,330);
  curveVertex(280,325);
  curveVertex(288,322);
  endShape();
  
  //right hand
  beginShape();
  curveVertex(380,310);
  curveVertex(380,310);
  curveVertex(382,330);
  curveVertex(370,345);
  curveVertex(350,345);
  curveVertex(340,345);
  curveVertex(340,330);
  curveVertex(350,320);
  curveVertex(350,320);
  endShape();
  
  //draw brown part of left ear
  fill(#583D3A);
  stroke(#583D3A);
  strokeWeight(3);
  beginShape();
  curveVertex(282,150);
  curveVertex(282,150);
  curveVertex(285,100);
  curveVertex(315,40);
  curveVertex(322,100);
  curveVertex(320,150);
  curveVertex(320,150);
  endShape();
  
  
  //yellow part of the left ear
  fill(#FCEF78);
  stroke(#583D3A);
  strokeWeight(3);
  beginShape();
  curveVertex(282,150);
  curveVertex(282,150);
  curveVertex(285,100);
  curveVertex(310,80);
  curveVertex(322,90);
  curveVertex(322,100);
  curveVertex(320,150);
  curveVertex(320,150);
  endShape();
  
  //draw yellow part of right ear
  fill(#FCEF78);
  stroke(#583D3A);
  strokeWeight(3);
  beginShape();
  curveVertex(375,160);
  curveVertex(375,160);
  curveVertex(410,125);
  curveVertex(460,90);
  curveVertex(430,165);
  curveVertex(400,210);
  curveVertex(400,210);
  endShape();
  
  //draw brown patch of right ear
  fill(#583D3A);
  stroke(#583D3A);
  strokeWeight(3);
  beginShape();
  curveVertex(426,113);
  curveVertex(426,113);
  curveVertex(440,100);
  curveVertex(460,90);
  curveVertex(438,149);
  curveVertex(428,135);
  curveVertex(420,128);
  curveVertex(405,130);
  curveVertex(400,133);
  curveVertex(400,133);
  endShape();
 
  //draw yellow feet
  fill(#FCEF78);
  stroke(#583D3A);
  strokeWeight(3);
  ellipse(270,395,50,70);
  ellipse(360,410,50,70);
  
  //feet shadow
  fill(#FACF62);
  noStroke();
  ellipse(270,400,30,45);
  ellipse(360,415,30,45);

  //draw facial expressions
  //eyes
  stroke(#583D3A);
  line(260,200,285,210);
  line(330,215,360,210);
  
  //mouth
  line(285,235,300,230);
  line(300,230,320,245);
  
  //blush shadow
  noStroke();
  fill(#FCA17D);
  ellipse(265,228,32,32);
  ellipse(365,235,35,35);
  
  //blush
  fill(#F5774D);
  ellipse(265,228,28,28);
  ellipse(365,235,30,30);
  fill(#E88215);
  ellipse(268,225,20,20);
  ellipse(363,232,20,20);
  
  
  popMatrix();//end of isolating memory
  
}


//Click R button and reset pikachu's angle to 0.
//I succeed
//Press R button and drag, changing from current position based on mouse position changing
//I tired but failed, it was changed directly to the mouse position but not from the current position
void mouseClicked()
{
 if (mousePressed && mouseButton == RIGHT)
 {
   {
      pikAngDeg = 0;
      pikX = mouseX-width/2;//move the center of pikachu to mouse's position
      pikY = mouseY-height/2;
   }
 }
}
 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到