nfg_day9

code import ddf.minim.*;
 * Ex.6 stop and start two sounds**

Minim minim; AudioPlayer player1, player2;

boolean bob = false; boolean mary = false;

color onColor = color(0,255,0); color offColor = color(255,0,0); color rect1Color = offColor; color rect2Color = offColor;

void setup { size(200,200); minim = new Minim(this); player1 = minim.loadFile("colunda final.mp3"); player2 = minim.loadFile("groove.mp3"); }

void draw { if(bob){ player1.play; rect1Color=onColor; }  else if(!bob){ player1.pause; rect1Color=offColor; } if(mary){ player2.play; rect2Color=onColor; }  else if(!bob){ player2.pause; rect2Color=offColor; } fill(rect1Color); rect(0,0,100,200); fill(rect2Color); rect(100,0,100,200); }

void stop { player1.close; player2.close; minim.stop; super.stop; }

void keyPressed{ if(key==' '){ if(!bob){ bob=true; }    else if(bob){ bob=false; } }  if(key=='a'){ if(!mary){ mary=true; }    else if(mary){ mary=false; } } } code

code import ddf.minim.*;
 * Ex.5 stop and start single sound**

Minim minim; AudioPlayer player;

boolean bob = false;

void setup { minim = new Minim(this); player = minim.loadFile("colunda final.mp3"); }

void draw { if(bob){ player.play; } else if(!bob){ player.pause; } }

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

void keyPressed{ if(key==' '){ if(!bob){ bob=true; } else if(bob){ bob=false; } } } code

code import gifAnimation.*; PImage b; Gif sonic, zelda, current;
 * Ex.4 Swapping GIFs**

void setup{ size(302,450); frameRate(12); sonic = new Gif(this, "cellphone3.gif"); zelda = new Gif(this, "cellphone3.gif"); current = sonic; current.play; b = loadImage("bed.png"); fill(0); }

void draw { background(b); image(current, 10,10); }

void keyPressed{ if(keyCode == DELETE || keyCode == BACKSPACE){ current=zelda; current.play; } if(key==' '){ current.stop; } } code

code boolean switchRoom = true; boolean switchMasking = false;
 * Ex.3 Tabs**

void setup{ size(600,600); setupRoom; }

void draw{ if(switchRoom){ drawRoom; } if(switchMasking){ drawMasking; } /*  rectMode(CENTER); rect(300,300,150,150); */ }

void mouseReleased{ if(hitDetect(mouseX,mouseY,0,0,300,300,150,150)){ switchRoom=false; setupMasking; switchMasking=true; } }

void keyReleased{ if(switchRoom){ // }  if(switchMasking){ if (key == ENTER); setup; } }

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 color moving = color(255,0,0); color waiting = color(0,0,255); color current = waiting; int counterMax = 200; int counter = counterMax;
 * Ex.2 KeyUp Timer**

void setup{ size(300,300); }

void draw{ if(!keyPressed && counter>=counterMax){ current = waiting; } else { current = moving; } fill(current); rectMode(CENTER); rect(150,150,50,50); counter++; println(counter); }

void keyPressed{ }

void keyReleased{ counter=0; } code

code PFont font; int fontSize = 32; int fps = 24; int counter = 10 * fps; boolean timeUp = false;
 * Ex.1 "Game Over" Timer**

void setup{ size(200,200); frameRate(fps); font = createFont("Arial",fontSize); }

void draw{ background(200); textFont(font,fontSize); timer; if(!timeUp){ text(int(counter/fps),100,100); } else if(timeUp) { text("game over",50,100); } }

void timer{ if(counter>0){ counter--; }else{ timeUp=true; } } code