/** * Bounce. * * When the shape hits the edge of the window, it reverses its direction. */ import ddf.minim.*; Minim minim; AudioSample kick; int size = 60; // Width of the shape float xpos, ypos; // Starting position of shape float xspeed; // Speed of the shape float yspeed; // Speed of the shape int xdirection = 1; // Left or Right int ydirection = 1; // Top to Bottom int fillcolor = 255; String gamestate; int keyvalue; float counter,angle,speed; PImage back; void setup() { size(640, 633); background(102); back = loadImage("chessboard.jpg"); minim = new Minim(this); kick = minim.loadSample("BD.mp3", 2048); frameRate(30); smooth(); // Set the starting position of the shape xpos = width/2; ypos = height/2; gamestate="start"; } void draw() { // background(102); image(back,0,0); if(gamestate=="start") { if (keyvalue==10) { counter++; angle = ((float)mouseY/(float)width) * 2 * PI; speed = 100-100*exp(-(counter/100)); yspeed = speed*sin(angle); xspeed = speed*cos(angle); if (yspeed>0) ydirection = 1; else ydirection = -1; if (xspeed>0) xdirection = 1; else xdirection = -1; fill(255,0,0,255); stroke(255,0,0,255); line(xpos+size/2,ypos+size/2,xpos+size/2+200*cos(angle),ypos+size/2+200*sin(angle)); rect(0,height-(speed*(height/100)),20,speed*(height/100)); noStroke(); } if (keyvalue==100) gamestate="go"; } if(gamestate=="go"){ // Update the position of the shape xpos = xpos + ( xspeed); ypos = ypos + ( yspeed); yspeed = yspeed / 1.01; xspeed = xspeed / 1.01; if (abs(yspeed)<0.05 && abs(xspeed)<0.05) {background(fillcolor); tint(255,100);image(back,0,0);yspeed = 0; xspeed = 0; counter=0;gamestate="start";} // Test to see if the shape exceeds the boundaries of the screen // If it does, reverse its direction by multiplying by -1 if (xpos < 0) { kick.trigger(); xspeed *= -1; xpos = 0; if(fillcolor==0) fillcolor=255; else fillcolor=0; fill(fillcolor); } if (xpos > width-size) { kick.trigger(); xspeed *= -1; xpos = width-size; if(fillcolor==0) fillcolor=255; else fillcolor=0; fill(fillcolor); } // constrain(xpos,0,width-size); if (ypos < 0) { kick.trigger(); yspeed *= -1; ypos = 0; if(fillcolor==0) fillcolor=255; else fillcolor=0; fill(fillcolor); } if (ypos > height-size) { kick.trigger(); ypos = height-size; yspeed *= -1; if(fillcolor==0) fillcolor=255; else fillcolor=0; fill(fillcolor); } // constrain(ypos,0,height-size); } // Draw the shape ellipse(xpos+size/2, ypos+size/2, size, size); } void keyPressed() {keyvalue = 10;} void keyReleased() {keyvalue=100;} void mouseReleased() {keyvalue=100;} void mousePressed() {keyvalue = 10;} void stop() { // always close Minim audio classes when you are done with them kick.close(); minim.stop(); super.stop(); }