2015年11月1日 星期日

各種程式

碰撞偵測
PImage wm;
int mouse=1;
int size=100;
int x=500;
int y=250;
void setup()
{
     size(1200, 600);
     wm=loadImage("WM.png");
}
void draw()
{
     background(255);
     if(mouse==0);
     else if(mouse==1)image(wm,500,250,size,size);
     Collision();
}
void Collision()
{
   if(mouseX>=x&&mouseX<=x+size&&mouseY>=y&&mouseY<=y+size)mouse=0;
}

//////////////////
button
int rectX, rectY;
int rectSize = 90;
int rectHeight=100;
int rectWeight=300;
color rectColor, baseColor;
color rectHighlight;
color currentColor;
boolean rectOver = false;
int i=0;
void setup() {
  size(640, 360);
  rectColor = color(0);
  rectHighlight = color(51);
  baseColor = color(102);
  currentColor = baseColor;
  rectX =160;
  rectY =90;
  ellipseMode(CENTER);
}
void draw() {
    background(255);
  update(mouseX, mouseY);
  if (rectOver) {
    fill(255,255,0);
  } else {
    fill(0,255,0);
  }
    textSize(100);
   text(""+i,10,100);
  rect(rectX, rectY,rectWeight , rectHeight);
}

void update(int x, int y) {

 if ( overRect(rectX, rectY,rectWeight, rectHeight) )
 {
    rectOver = true;
 }
  else
  {
  rectOver = false;
  }
}
boolean overRect(int x, int y, int width, int height)  {
  if (mouseX >= x && mouseX <= x+width &&
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}
void mousePressed() {
if(mouseButton==LEFT)
  if (rectOver) {
i++;  
  }
}



沒有留言: