2024年12月9日 星期一

BK Weel14 水滴落下+mouse圖案

Processing 程式碼

利用 mouseDragged() 按下滑鼠右鍵拖曳一一下 讓水滴自動落下
void setup() {
  size(500, 500);
}
int x = -1, y = -1;
void draw() {
  if (x != -1) {
    ellipse(x, y, 1, 1);
    y++; //往下滴 (x, y)座標中, y慢慢變大, 就是往下滴的感覺
  }
}
void mouseDragged() { //要用右鍵拖曳, 才會滴下水滴
  if (mouseButton==LEFT) line(mouseX, mouseY, pmouseX, pmouseY);
  if (mouseButton==RIGHT) {
    x = mouseX;
    y = mouseY;
  }
}


滑鼠左鍵按下去拖曳可以畫出電視壞掉顏色的線
void setup() {
  size(500, 500);
  background(255);
}
void draw() {
}
void mouseDragged() {
  if (mouseButton==LEFT) { //在左鍵dragged拖曳時
    line(mouseX, mouseY, pmouseX, pmouseY); //本來的線, 是灰黑色, 不是白色
    loadPixels(); //取出畫面像素
    //println(pixels[0]); //白色是-1
    for (int i=0; i < width*height; i++) { //全部的點, 都去巡一下顏色
      if (pixels[i] != -1) pixels[i] = color(random(255), random(255), random(255));
    } //如果「不是白色」就是有畫圖碰到pixels, 都變成亂數彩色
    updatePixels(); //若有修改, 要再放回去
  }
}


執行後 生成一顆球 往下墜落 可以畫一條線 當球碰到線會停下來
PImage img;
void setup() {
  size(500, 500);
  img = createImage(500, 500, RGB); //用來畫圖的畫布
  img.loadPixels();
  for (int i=0; i<500*500; i++) img.pixels[i] = color(#FFFFF2); //-14
  img.updatePixels();
}
int x = 250, y = 0;
void draw() {
  background(img);
  ellipse(x, y, 20, 20);
  if (y<498 && img.pixels[x+y*500]==-14) y++;
}
void mouseDragged() {
  background(img);
  line(mouseX, mouseY, pmouseX, pmouseY);
  loadPixels();
  img.loadPixels();
  for (int i=0; i<500*500; i++) img.pixels[i] = pixels[i]; //把畫面的色彩, 搬到img裡
  img.updatePixels();
  updatePixels();
}


執行後  背景變黑色的 按滑鼠左鍵拖曳 向刮畫 按下右鍵可以產生好多球
往下墜落直到碰到線才停下
PImage img;
void setup() {
  size(500, 500);
  img = createImage(500, 500, RGB);
} //換黑背景
//int x = 250, y = 0;
ArrayList<PVector> p = new ArrayList<PVector>(); //很多個點p
void draw() {
  background(img);
  for (PVector pp : p) {
    ellipse(pp.x, pp.y, 20, 20);
    if (pp.y<498 && img.pixels[int(pp.x)+int(pp.y)*500]==-16777216) pp.y++;
  }
}
void mouseDragged() {
  background(img);
  stroke(random(255), random(255), random(255));
  line(mouseX, mouseY, pmouseX, pmouseY);
  loadPixels();
  img.loadPixels();
  for (int i=0; i<500*500; i++) img.pixels[i] = pixels[i];
  img.updatePixels();
  updatePixels();
  for (PVector pp : p) {
    ellipse(pp.x, pp.y, 20, 20);
  }
}
void mousePressed() {
  if (mouseButton==RIGHT) {
    p.add( new PVector(mouseX, mouseY));
  }
}


執行後 可以滑鼠左鍵框出小範圍 右鍵在範圍內點一下 會隨機填滿顏色
PImage img;
void setup() {
  size(500, 500);
  background(#FFFFF2); //先畫淡黃色背景
}
void draw() {
  if (mousePressed && mouseButton==LEFT) line(mouseX, mouseY, pmouseX, pmouseY);
  if (mousePressed && mouseButton==RIGHT) {
    loadPixels(); //把畫面讀入 pixels[] 陣列裡
    color c1 = pixels[mouseX+mouseY*500]; //原本的色彩
    color c2 = color(random(255), random(255), random(255)); //亂數新色彩
    if (c1 != c2) myFloodFill(mouseX, mouseY, c1, c2);
    updatePixels(); //把陣列的值, 放回畫面
  }
}
boolean isOK(int x, int y, color c1) {
  if (x<0 || y<0 || x>=500 || y>=500) return false; //超過邊界不能做哦
  if (pixels[x + y*500] != c1) return false; //色彩原本的 c1 色彩不同, 就不要變色
  return true;
}
void myFloodFill(int x, int y, color c1, color c2) {
  pixels[x + y * 500] = c2; //這1格設成新色彩
  if (isOK(x+1, y, c1)) myFloodFill(x+1, y, c1, c2); //試試右邊色彩對嗎? 對就做
  if (isOK(x-1, y, c1)) myFloodFill(x-1, y, c1, c2); //試試左邊色彩對嗎? 對就做
  if (isOK(x, y+1, c1)) myFloodFill(x, y+1, c1, c2); //試試上面色彩對嗎? 對就做
  if (isOK(x, y-1, c1)) myFloodFill(x, y-1, c1, c2); //試試下面色彩對嗎? 對就做//不要點太大的區域, 因為「函式呼叫函式」太多層, 會被警告出錯


改鼠標的圖案
void setup() {
  size(500, 500);
  PImage img = loadImage("kitty.png");
  cursor(img);
}
void draw() {
  background(#FFFFF2);
}

改鼠標的圖案 兩個圖案切換
PImage imgCute, imgKitty;
void setup() {
  size(500, 500);
  imgCute = loadImage("cute.png");
  imgKitty = loadImage("kitty.png");
  cursor(imgKitty);
}
void draw() {
  background(#FFFFF2);
  if (frameCount%20==0) cursor(imgCute);
  if (frameCount%120==60) cursor(imgKitty);
}
讓mouse更大
PImage imgBigCute, imgBigKitty, imgCursor;
void setup() {
  size(500, 500);
  imgBigCute = loadImage("bigcute.png");
  imgBigKitty = loadImage("bigKitty.png");
  imgCursor = imgBigKitty;
}
void draw() {
  background(#FFFFF2);
  imageMode(CENTER); //畫圓的系統, 該成以正中心為座標
  image(imgCursor, mouseX, mouseY);
  imageMode(CORNER); //畫圓的系統, 改成左上角的座標
  if (frameCount%120==0) imgCursor = imgBigCute;
  if (frameCount%120==60) imgCursor = imgBigKitty;
}

沒有留言:

張貼留言