Proceesing编程【4】

processing……有毒= =!!!!!

 一个移动的小球==

int num=2;//the number of lines
int width=500;
int height=500;
color c=color((int)random(255), (int)random(255), (int)random(255));//init color
int Xinit=width/4, Yinit=height/4;//init place
int r=150;//init radius
int X=0, Y=0,Y1=0,X1=0;//XY:dis,X1,X2:varaXY
boolean ifReleased=false,ifColor=false,ifClicked=false;

class ball
{
  int r=0;//radius
  color co;//color
  float X=0,Y=0;//location
  ball(color c, int radius,float x,float y)
  {
    co=c;
    r=radius;
    X=x;
    Y=y;
  }
  void display() {
    background(255);
    fill(co);
    noStroke();
    ellipse(X,Y,r,r);
    drawgrid();
  }
}

void setup() 
{
  size(500, 500);
  background(255);
  drawgrid();
  noStroke();
  fill(c);
  ellipse(width/4, height/4, r, r);

}

void draw(){
  if(mousePressed&&(dist(Xinit,Yinit,mouseX,mouseY)<=r/2))
  {
    print("----------------");
    Xinit=mouseX;
  Yinit=mouseY;
    ball b=new ball(c,r,-X+mouseX,-Y+mouseY);
    b.display();
  }

}

void mousePressed()
{
   X=mouseX-Xinit;
   X1=Xinit;
  Y=mouseY-Yinit;
  Y1=Yinit;
  if(dist(Xinit,Yinit,mouseX,mouseY)<=r/2)
  {
    print("PPPPPPPPPPPPPPPPPPPPPPPP");
  ifReleased=false;

  }
}

void mouseReleased() {
  if(dist(Xinit,Yinit,mouseX,mouseY)<=r/2)
  {
  print("zzzzzzzzzzzzzzzzzzzzzzz");
  ifReleased=true;
  int x=mouseX;
  int y=mouseY;
  color c1=c;
  if((dist(mouseX,mouseY,X1,Y1)<width/3)||x<=0||y<=0||x>=width||y>=height)
  {
  ifColor=true;
  }
  else
  ifColor=false;
  print(Xinit);
  print(X*X+Y*Y<=r*r);
  if(!(ifColor&&(X*X+Y*Y<=r*r)))
  {
    print(ifColor);
    c1=color((int)random(255), (int)random(255), (int)random(255));
  }
    if (x>width/2&&y>height/2)
    {  
      Xinit=width*3/4;
      Yinit=height*3/4;
      background(255);
      noStroke();
      fill(c1);
      ellipse(width*3/4, height*3/4, 150, 150);
      drawgrid();
    } else if (x>width/2&&y<height/2)
    { 
      Xinit=width*3/4;
      Yinit=height/4;
      background(255);
      noStroke();
      fill(c1);
      ellipse(width*3/4, height/4, 150, 150);
      drawgrid();
    } else if (x<width/2&&y>height/2)
    {  
      Xinit=width/4;
      Yinit=height*3/4;
      background(255);
      noStroke();
      fill(c1);
      ellipse(width/4, height*3/4, 150, 150);
      drawgrid();
    } else 
    { Xinit=width/4;
      Yinit=height/4;
      background(255);
      noStroke();
      fill(c1);
      ellipse(width/4, height/4, 150, 150);
      drawgrid();
    }
    c=c1;
    }

  }

void drawgrid() {
  int i;
  for (i=1; i<num; i++)
  {
    stroke(0);
    strokeWeight(2);
    line(0, height*i/num, width, height*i/num);
    line(width*i/num, 0, width*i/num, height);
  }

原文地址:https://www.cnblogs.com/hitWTJ/p/9865445.html