I am trying to make a doorbell in processing that plays a note at the click of the mouse. The doorbell is supposed to jiggle while the note is playing, but Im having difficulty with a few minor problems that I cant seem to figure out. If anyone could please help me out in telling me what it is I did wrong with my code, it would be greatly appreciated. Thank you!!!!
Code:
import ddf.minim.*;
// A doorbell object (that will trigger the sound)
Doorbell doorbell;
Minim minim;
void setup() {
size(200,200) ;
// start up minim
Minim = new Minim(this) ;
// Create a new doorbell
doorbell = new Doorbell(150, 100, 32, "note.wav") ;
// Show the doorbell
doorbell.display(mouseX,mouseY) ;
doorbell.jiggle() ;
}
void mousePressed() {
// If the user clicks on the doorbell, play the sound!
if (doorbell.contains(mouseX,mouseY)) {
doorbell.ring();
}
}
// Close the sound files
public void stop() {
doorbell.close() ;
super.stop() ;
}
class Doorbell {
// Location and size
float x;
float y;
float r;
// An audio snippet object
AudioSnippet note;
// Create the doorbell
Doorbell (float x_, float y_, float r_, String filename) {
x = x_;
y = y_;
r = r_;
// load note.wav into a new Audiosnippet
note = Minim.loadSnippet(filename) ;
}
// If the "doorbell" is ringing, the shape jiggles
void jiggle() {
if (note.isPlaying()) {
x += random(-1,1) ;
y += random(-1,1) ;
r = constrain(r + random(-2,2) , 10, 100);
}
}
// The doorbell rings!
void ring() {
if (!note.isPlaying()) {
note.rewind();
note.play();
}
}
void draw() {
background(100,100,126) ;
// Is a point inside the doorbell (used for mouse rollover, etc.)
boolean contains(float mx, my) {
if (dist (mx,my,x,y) < r) {
return true;
} else {
return false;
}
}
// Show the doorbell (hardcoded colors, could be improved)
void display(float mx, float my) {
if (contains (mx,my)) {
fill ( 126,114,100);
} else {
fill(119, 152, 202);
}
stroke(202,175,142);
ellipse(x,y,r,r);
}
I am trying to make a small game consisting of two lanes with one car being controlled by the keypad and continuous cars driving in the opposite direction. I am trying to make it so that if the car crashes into the cars going in the opposite direction the game is over. If someone could help me out or give me some kind of idea on how to do this it would be greatly appreciated!!!! Thank you!