Ciao 访客, welcome back to old school! :p
float xA,yA,xB,yB;float radiusA,widthB,heightB;void setup(){ size(480,360); background(255); radiusA = 50; xA = 425; yA = 310; xB = 318; yB = 300; widthB = 60; heightB = 36; frameRate(30); smooth();}void draw(){ //------------圆形按钮,点击随机切换背景色--------------- noStroke(); if(dist(mouseX,mouseY,xA,yA) < radiusA/2){ fill(#EEFC54); if(mousePressed){ fill(250,0,250); background(random(255),random(255),random(255)); } }else{ fill(#93843A); } ellipse(xA,yA,radiusA,radiusA); //-------------方形按钮,点击随机出现彩色小圆点------------- if(mouseX> xB && mouseX < xB + widthB && mouseY> yB && mouseY < yB + heightB){ fill(#EEFC54); if(mousePressed){ fill(random(255),random(255),random(255)); ellipse(random(360),random(480),10,10); } }else{ fill(#93843A); } rect(xB,yB,widthB,heightB);}
@光羽思路上没有问题。但是如果我希望鼠标点击作为事件来处理呢,譬如点一下按钮,背景随机颜色一次。而不是我鼠标一旦不松开,就会不停地随机颜色。试着改一下代码吧
float xA,yA,xB,yB;float radiusA,widthB,heightB;boolean shutter;void setup(){ size(480,360); background(255); radiusA = 50; xA = 425; yA = 310; xB = 318; yB = 300; widthB = 60; heightB = 36; shutter = false; frameRate(30); smooth();}void draw(){ //------------圆形按钮,点击随机切换背景色--------------- noStroke(); if(dist(mouseX,mouseY,xA,yA) < radiusA/2){ fill(#EEFC54); if(shutter){ fill(250,0,250); background(random(255),random(255),random(255)); } }else{ shutter = false; fill(#93843A); } ellipse(xA,yA,radiusA,radiusA); //-------------方形按钮,点击随机出现彩色小圆点------------- if(mouseX> xB && mouseX < xB + widthB && mouseY> yB && mouseY < yB + heightB){ fill(#EEFC54); if(shutter){ fill(random(255),random(255),random(255)); ellipse(random(360),random(480),10,10); } }else{ shutter = false; fill(#93843A); } rect(xB,yB,widthB,heightB);}void mousePressed(){ shutter = !shutter;}
void mousePressed(){ if(...){ ...... }}
float xA, yA, xB, yB;float radiusA, widthB, heightB;void setup() { size(500, 500); background(255); frameRate(30); smooth(); radiusA=40; xA=200; yA=200; xB=300; yB=180; widthB=100; heightB=50;}void draw() { noStroke(); //--circle-- fill(#ACF005); ellipse(xA, yA, radiusA*2, radiusA*2); //--square-- rect(xB, yB, widthB, heightB);}void mousePressed() { if (dist(mouseX, mouseY, xA, yA)<radiusA) { background(random(255), random(255), random(255)); } else if (mouseX>xB && mouseX< xB+widthB && mouseY>yB && mouseY< yB+heightB) { fill(255, random(255), random(255)); ellipse(random(500), random(500), 10, 10); }}
@光羽其实鼠标位置判断的逻辑可以写在事件结构内的。譬如程序代码void mousePressed(){ if(...){ ...... }}然后可以声明两个布尔值,在draw 结构内做判断并绘制对应的图形。 写写看吧。加油。:)
float xA,yA,xB,yB;float radiusA,widthB,heightB;boolean shutterRound,shutterRect;void setup(){ size(480,360); background(255); radiusA = 50; xA = 425; yA = 310; xB = 318; yB = 300; widthB = 60; heightB = 36; shutterRound = false; shutterRect = false; frameRate(30); smooth();}void draw(){ //------------圆形按钮,点击随机切换背景色--------------- noStroke(); if(shutterRound){ fill(250,0,250); background(random(255),random(255),random(255)); shutterRound = false; }else{ fill(#93843A); } ellipse(xA,yA,radiusA,radiusA); //-------------方形按钮,点击随机出现彩色小圆点------------- if(shutterRect){ fill(random(255),random(255),random(255)); ellipse(random(360),random(480),10,10); shutterRect = false; }else{ fill(#93843A); } rect(xB,yB,widthB,heightB);}void mousePressed(){ if(dist(mouseX,mouseY,xA,yA) < radiusA/2){ shutterRound = !shutterRound; } if(mouseX> xB && mouseX < xB + widthB && mouseY> yB && mouseY < yB + heightB){ shutterRect = !shutterRect; }}
float xA, yA, xB, yB;float radiusA, widthB, heightB;boolean shutterB;void setup() { size(500, 500); background(255); smooth(); radiusA=40; xA=200; yA=200; xB=300; yB=180; widthB=100; heightB=50;}void draw() { noStroke(); //--circle-- fill(#ACF005); if(shutterB){ fill(255, random(255), random(255)); ellipse(random(500), random(500), 10, 10); shutterB = false; } fill(#ACF005); ellipse(xA, yA, radiusA*2, radiusA*2); //--square-- rect(xB, yB, widthB, heightB);}void mousePressed() { if (dist(mouseX, mouseY, xA, yA)<radiusA) { background(random(255), random(255), random(255)); } else if (mouseX>xB && mouseX< xB+widthB && mouseY>yB && mouseY< yB+heightB) { shutterB = true; }}
void setup() { background(255); size(500, 500); smooth();}void draw() { //leg noStroke(); fill(#0C481A); rect(150, 350, 208, 50); //body stroke(#86FFE2); strokeWeight(70); strokeCap(ROUND); line(180, 280, 160, 300); stroke(#86FFE2); strokeWeight(70); strokeCap(ROUND); line(320, 280, 340, 300); noStroke(); fill(#86FFE2); beginShape(); vertex(200, 180); vertex(150, 350); vertex(360, 350); vertex(300, 180); endShape(CLOSE); noStroke(); fill(#86FFE2); ellipse(255, 350, 208, 50); //foot noStroke(); fill(#43361C); beginShape(); curveVertex(120, 400); curveVertex(320, 400); curveVertex(120, 400); curveVertex(280, 600); endShape(CLOSE); noStroke(); fill(#43361C); beginShape(); curveVertex(130, 400); curveVertex(130, 400); curveVertex(390, 400); curveVertex(350, 600); endShape(CLOSE); stroke(0); strokeWeight(5); strokeCap(ROUND); line(250, 270, 250, 373); stroke(0); strokeWeight(0.2); fill(#F0D39B); ellipse(270, 310, 40, 50); stroke(0); strokeWeight(0.2); fill(#F0D39B); ellipse(235, 310, 40, 50); stroke(0); strokeWeight(1); noFill(); ellipse(272, 295, 20, 20); stroke(0); strokeWeight(1); noFill(); ellipse(238, 295, 20, 20); //face noStroke(); fill(250, 213, 189); ellipse(250, 150, 250, 250); noStroke(); fill(#392806); ellipse(250, 250, 40, 10); stroke(#392806); strokeWeight(5); strokeCap(ROUND); line(232, 252, 268, 252); endShape(CLOSE); //hair noStroke(); fill(255, 255, 0); beginShape(); vertex(250, 25); vertex(150, 10); vertex(180, 40); vertex(150, 45); vertex(100, 100); vertex(180, 70); vertex(160, 120); vertex(200, 100); vertex(250, 70); vertex(350, 100); vertex(300, 50); vertex(360, 50); vertex(300, 30); vertex(320, 5); vertex(250, 25); endShape(CLOSE); //eye noStroke(); fill(255); translate(230, 150); rotate(PI/3); ellipse(0, 0, 60, 70); noStroke(); fill(255); translate(30, -60); rotate(PI/3); ellipse(0, 0, 60, 70); noStroke(); fill(0); ellipse(-10, 0, 10, 10); noStroke(); fill(0); ellipse(20, 40, 10, 10); stroke(0); strokeWeight(7); strokeCap(SQUARE); rotate(PI/3); translate(-30, 30); line(-10, -10, 30, 30); stroke(0); strokeWeight(7); strokeCap(SQUARE); rotate(PI/-2); translate(-20, 100); line(-10, -10, 30, 30); endShape(CLOSE);}
float xPos,yPos;float diam;float incrementX;float incrementY;float gravity;void setup(){ size(500,500); colorMode(HSB); smooth(); background(255); xPos = width/2; yPos = height/2; incrementX = 5; incrementY = 4; diam = 100;} void draw(){ fill(255,15); rect(0,0,width,height); fill(color(360*(yPos/height),255,200)); noStroke(); ellipse(xPos,yPos,diam,diam); // when hits bounds x if(xPos < diam/2 ){ incrementX=5; }else if(xPos > width-diam/2){ incrementX=-5; } // when hits bounds y if(yPos < diam/2 ){ incrementY=4; }else if(yPos > height-diam/2){ incrementY=-4; } gravity = yPos/height*5; xPos += incrementX; yPos += incrementY*gravity; }
float x1, x2, y1, y2;float button1Radius=100;float button2Width=200;float button2Height=200;void setup() { size(500, 500); background(255); smooth(); noStroke(); x1 = width/4; x2 = width/1.4; y1 = y2 = height/2; }void draw() { fill(200, 100, 200); ellipse(width/4, height/2, button1Radius*2, button1Radius*2); fill(200, 200, 100); rectMode(CENTER); rect(width/1.4, height/2, button2Width, button2Height);}void mousePressed(){ if(dist(mouseX,mouseY,x1,y1)<button1Radius){ fill(200, 100, 200); ellipse(random(500),random(500),20,20); }else if (mouseX>x2-button2Width/2 && mouseX< x2+button2Width/2 && mouseY>y2-button2Height/2 && mouseY< y2+button2Height/2) { fill(200, 200, 100); rect(random(500),random(500),20,20); }else{ background(255); } }
float xPos, yPos;float diam;float incrementX;float incrementY;float gravity;void setup() { size(500, 500); colorMode(HSB); smooth(); background(255); xPos = width/2; yPos = 0; incrementX = 5; incrementY = 0; diam = 100; gravity = 0.1;}void draw() { fill(255, 15); rect(0, 0, width, height); fill(color(360*(yPos/height), 255, 200)); noStroke(); ellipse(xPos, yPos, diam, diam); // when hits bounds x if (xPos < diam/2 ) { xPos = diam/2; incrementX *= -0.8; } else if (xPos > width-diam/2) { xPos = width-diam/2; incrementX *= -0.8; } // when hits bounds y if (yPos > height-diam/2) { yPos = height-diam/2; incrementY *= -0.8; } incrementY += gravity; xPos += incrementX; yPos += incrementY;}