1.跟著滑鼠畫方形

void setup(){//Star()跑一次 size(800,400); } void draw(){//Update()跑很多次 rect(mouseX,mouseY,20,20); }
2.填滿滑鼠移到的格子

void setup(){//Star() size(800,300);//打卡片左右留45的寬度,兩邊90,800-90=720,720/45=16 for(int x=0;x<800;x+=16){//一格16 for(int y=0;y<300;y+=30){//一格30 rect(x,y, 16,30); } } } void draw(){//Update() int nowX=mouseX/16*16, nowY=mouseY/30*30; //先清除多餘的尾數,再乘回去就會到每格的開頭座標 fill(0);//黑色 rect(nowX,nowY,16,30); }
3.按左鍵黑色填滿格子,右鍵變白

void setup(){//Star() size(800,300); for(int x=0;x<800;x+=16){ for(int y=0;y<300;y+=30){ rect(x,y, 16,30); } } } void draw(){//Update() int nowX=mouseX/16*16, nowY=mouseY/30*30; if(mousePressed&&mouseButton==LEFT){//按下左鍵 fill(0);//黑色 rect(nowX,nowY,16,30); } else if(mousePressed&&mouseButton==RIGHT){//按下右鍵 fill(255);//白色 rect(nowX,nowY,16,30); } }
4.圖片跟隨滑鼠
(方法一)開始跑時圖片會延遲出現

PImage img;//宣告在外面 void setup(){ size(500,500); img=loadImage("圖片網址"); } void draw(){ image( img, mouseX,mouseY, 200,200); }(方法二)不會延遲的方法






5.畫卡片

PImage imgBG; int [][]table=new int[45][10]; void setup() {//Star() size(711, 377);//背景圖的大小(去照片看) imgBG=loadImage("card.jpg");//圖片檔名 } void draw() {//Update() background(imgBG); int nowI=mouseX/16, nowJ=mouseY/30; if (mousePressed&&mouseButton==LEFT) { table[nowI][nowJ]=1; } else if (mousePressed&&mouseButton==RIGHT) { table[nowI][nowJ]=0; } for (int i=0; i<45; i++) { for (int j=0; j<10; j++) { fill(0); if (table[i][j]==1)rect(i*16, j*30, 16, 30); } } }
沒有留言:
張貼留言