nfg_Day8

Day 8
<< main | course director notes

code float deg = 0; float degDelta = 0; float degDeltaMax = 10;
 * 8. Velocity and acceleration**

void setup{ size(640,480); }

void draw{ background(128);

pushMatrix; translate(320,240); rotate(radians(deg)); rectMode(CENTER); rect(0,0,100,100); popMatrix;

if(mousePressed){ if(degDeltadegDeltaMax){ degDelta=degDeltaMax; } }   else{ if(degDelta>0){ degDelta -= 0.1; }   else if (degDelta<0){ degDelta=0; } }

deg += degDelta; println(degDelta); } code

code color bgColor = color(40, 0, 0); color drawColor = color(100, 100, 255, 100); color eraseColor = color(40, 0, 0, 100); boolean drawMode = true; float brushSize = 4; float brushSizeMax = 100; float brushSizeMin = 2;
 * 7. Using mouse and keyboard input to make a basic drawing program**

void setup { size(640, 480); smooth; ellipseMode(CENTER); background(bgColor); }

void draw { if (mousePressed) { if (drawMode) { //this is your draw mode. float brushDiff = dist(mouseX, mouseY, pmouseX, pmouseY)/10; noFill; stroke(drawColor); if(brushSize - brushDiff > brushSizeMin){ strokeWeight(brushSize - brushDiff); }else{ strokeWeight(brushSizeMin); }     for (int i=0;i<10;i++) { line(pmouseX+random(5)-random(5), pmouseY+random(5)-random(5), mouseX+random(5)-random(5), mouseY+random(5)-random(5)); }   } else { //this is your erase mode. noStroke; fill(eraseColor); ellipse(mouseX, mouseY, 100, 100); } } }

void keyPressed { if (key=='d') { drawMode = true; } if (key=='e') { drawMode = false; } if (key==' ') { background(bgColor); } if (keyCode==UP) { brushSize++; if (brushSize>brushSizeMax) { brushSize=brushSizeMax; } }  if (keyCode==DOWN) { brushSize--; if (brushSize<brushSizeMin) { brushSize=brushSizeMin; } } }

code

code float posX = 320; float posY = 240;
 * 6. Using if(mousePressed) and if(keyPressed)**

void setup{ size(640,480); ellipseMode(CENTER); }

void draw{ if(mousePressed){ posY += 10; } if (keyPressed){ if(key==' '){ posX += 10; } } ellipse(posX,posY,40,40); } code

code float posX = 320; float posY = 240;
 * 5. Using void mousePressed and void keyPressed**

void setup{ size(640,480); ellipseMode(CENTER); }

void draw{ ellipse(posX,posY,40,40); }

void mousePressed{ posY += 10; }

void keyPressed{ if(key==' '){ posX += 10; } } code

code int numReps = 1000; int strokeSize = 100;
 * 4. Loop review**

void setup{ size(640, 480); }

void draw { background(128); for (int i=0; i< numReps; i++) { stroke(0, 10); strokeWeight(strokeSize); point(random(width), random(height)); } } code

code color bob, mary;
 * 3. Conditional, mouse input, colour variable**

void setup{ size(640,480); }

void draw{ bob = color(random(255),0,0); //random colour value mary = color(random(255)); //random greyscale value if(mousePressed){ fill(mary); }else{ fill(bob); } noStroke; ellipseMode(CENTER); ellipse(width/2,height/2,100,100); } code

code float posX = 320; float posY = 240; float speedX = -5; float speedY = -5; int fps = 60;
 * 2. Review: variables, functions, and conditionals**

void setup{ size(640,480); frameRate(fps); }

void draw{ background(128); if(posX<0 || posX > width){ speedX *= -1; } if(posY<0 || posY > height){ speedY *= -1; } posX += speedX; posY += speedY;

strokeWeight(20); point(posX,posY); } code

code import ddf.minim.*; Minim minim; AudioSnippet player;
 * 1. Sound example**

void setup { minim = new Minim(this); player = minim.loadSnippet("moo.wav"); }

void draw { }

void mousePressed{ player.rewind; player.play; }

void stop { player.close; minim.stop; super.stop; } code