xxxxxxxxxx
// TO MAKE SOUND THERE ARE FOUR THINGS!
//
// 1 - UPLOAD YOUR SOUND TO THE "FILES" TAB IN THE SIDEBAR
// 2 - TURN ON P5.SOUND IN THE LIBRARIES SECTION OF THE "SKETCH" TAB
// 3 - LOAD YOUR SOUND USING loadSound() IN THE preload() FUNCTION
// 4 - PLAY YOUR SOUND USING THE play() METHOD TO YOUR SOUND
var x, y;
var d = 50;
var velocity, angle;
var thesound, theimage;
var r = 0;
// like setup(), but first and blocking
function preload() {
thesound = loadSound("beep.m4a");
theimage = loadImage("cat.png");
}
function setup() {
createCanvas(windowWidth, windowHeight);
resetthatstuff();
}
function draw() {
background(0);
fill(255, 128, 0);
//ellipse(x, y, d, d);
drawcat(x, y, r);
x = x + velocity * cos(angle);
y = y + velocity * sin(angle);
// boundary checking:
// OPTION #3 : ANGULAR REFLECTION
if(x>width) angle = PI - angle;
if(x<0) angle = PI - angle;
if(y>height) angle = TWO_PI - angle;
if(y<0) angle = TWO_PI - angle;
if(x>width || x<0 || y>height || y<0) // || means "or"
{
thesound.rate(random(0.5, 2));
thesound.play();
}
r = r+0.01;
}
function keyTyped()
{
resetthatstuff();
}
function drawcat(x, y, r)
{
push();
translate(x, y);
rotate(r);
image(theimage, 0, 0, d, d);
pop();
}
function resetthatstuff()
{
x = width/2;
y = height/2;
velocity = random(10, 40); // how fast is the circle moving
angle = random(0, TWO_PI); // random angle
}