38 lines
663 B
Plaintext
38 lines
663 B
Plaintext
// Based on Example 20-2: Doorbell with Sonia in Learning Processing
|
|
|
|
import processing.sound.*;
|
|
SoundFile dingdong;
|
|
int x, y, d;
|
|
|
|
void setup() {
|
|
size(200, 200);
|
|
// Load the sound file
|
|
dingdong = new SoundFile(this, "doorbell.mp3");
|
|
x = width/2;
|
|
y = height/2;
|
|
d = 64;
|
|
strokeWeight(4);
|
|
}
|
|
|
|
void draw() {
|
|
background(255);
|
|
if (checkInCircle())
|
|
fill(100);
|
|
else
|
|
fill(175);
|
|
ellipse(x, y, d, d);
|
|
}
|
|
|
|
void mousePressed() {
|
|
// If the user clicks on the doorbell, play the sound!
|
|
if (checkInCircle()) {
|
|
dingdong.play();
|
|
}
|
|
}
|
|
|
|
boolean checkInCircle() {
|
|
if (dist(mouseX, mouseY, x, y) <= d/2)
|
|
return true;
|
|
else
|
|
return false;
|
|
} |