nfg_day6

code float posX = 320; float posY = 240; float delta = 20; float spread = 2; float boundaryX1 = 40; float boundaryX2 = 600; PImage[] bob = new PImage[3]; int currentFrame = 1; int currentFrameMax = 2; int currentFrameDelta = 1; boolean clicked = false; float targetX = 590; float targetY = 430; float targetSize = 40;
 * Ex.5 accomplishing a goal**

void setup{ size(640,480); frameRate(24); for(int i=0;i<bob.length;i++){ bob[i] = loadImage("elvis"+ i +".png"); } } void draw{ background(200); imageMode(CENTER); image(bob[currentFrame],posX,posY); if(currentFrame==currentFrameMax||currentFrame==0){ currentFrameDelta *= -1; } currentFrame+=currentFrameDelta;

posX += random(spread)-random(spread); posY += random(spread)-random(spread);

fill(255,0,0); ellipseMode(CENTER); ellipse(targetX,targetY,targetSize,targetSize);

if(hitDetect(posX,posY,bob[0].width,bob[0].height,targetX,targetY,targetSize,targetSize)){ fill(0,255,0); rect(0,0,width,height); } }

void keyPressed{ if(keyCode==LEFT){ posX -= delta; } if(keyCode==RIGHT){ posX += delta; } if(keyCode==UP){ posY -= delta; } if(keyCode==DOWN){ posY += delta; } }

boolean hitDetect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) { w1 /= 2; h1 /= 2; w2 /= 2; h2 /= 2; if(x1 + w1 >= x2 - w2 && x1 - w1 <= x2 + w2 && y1 + h1 >= y2 - h2 && y1 - h1 <= y2 + h2) { return true; } else { return false; } } code

code float posX1 = 320; float posY1 = 180; float ellipseWidth1 = 15; float ellipseHeight1 = 20;
 * Ex.4 hit detect function**

float posX2 = 100; float posY2 = 100; float ellipseWidth2 = 40; float ellipseHeight2 = 30;

boolean clicked = false;

//

void setup { size(640,360); frameRate(60); smooth; noCursor; }

//---

void draw { background(50,0,100);

posX1 = mouseX; posY1 = mouseY;

if(clicked) { posX2 = mouseX; posY2 = mouseY; }

ellipseMode(CENTER); strokeWeight(2); stroke(50,150,0); fill(100,255,0); ellipse(posX2, posY2, ellipseWidth2, ellipseHeight2); stroke(250,150,0,180); fill(255,100,0,180); ellipse(posX1, posY1, ellipseWidth1, ellipseHeight1); }

//

void mousePressed { if(hitDetect(posX1,posY1,ellipseWidth1,ellipseHeight1,posX2,posY2,ellipseWidth2,ellipseHeight2)) { clicked = true; } }

//---

void mouseReleased { clicked = false; }

//---

boolean hitDetect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) { w1 /= 2; h1 /= 2; w2 /= 2; h2 /= 2; if(x1 + w1 >= x2 - w2 && x1 - w1 <= x2 + w2 && y1 + h1 >= y2 - h2 && y1 - h1 <= y2 + h2) { return true; } else { return false; } }

code

code void setup{ size(640,480); }
 * Ex.3 function with arguments**

void draw{ bob(color(255,0,0),color(255)); ellipseMode(CENTER); ellipse(width/2,height/2,100,100); }

void bob(color c1, color c2){ if(mousePressed){ fill(c1); } else { fill(c2); } } code

code float posX = 320; float posY = 240; float deltaX = 10; float spread = 2; float boundaryX1 = 40; float boundaryX2 = 600; PImage[] bob = new PImage[3]; int currentFrame = 1; int currentFrameMax = 2; int currentFrameDelta = 1; boolean clicked = false;
 * Ex.2 image loop with hit detection**

void setup{ size(640,480); frameRate(24); for(int i=0;iboundaryX2){ deltaX *= -1; } } }

void mousePressed{ if(mouseXposX-(bob[0].width/2)){ if(mouseYposY-(bob[0].height/2)){ clicked=true; } } }

void mouseReleased{ clicked = false; } code


 * [|Ex.1 sliding gallery]**