// bouncing ball example
var x, y, dx, dy;
var r = 30; // radius
var g = 0.1; // gravity
var mySnd;
function preload() {
mySnd = loadSound("https://courses.ideate.cmu.edu/15-104/f2018/wp-content/uploads/2018/12/139875__y89312__44-1.wav");
mySnd.setVolume(0.5);
}
function setup() {
createCanvas(400, 400);
x = 200;
y = 40;
dx = 0;
dy = 0;
}
function draw() {
background(200, 220, 250);
fill(0, 250, 0);
ellipse(x, y, r, r);
x = x + dx;
y = y + dy;
var travel_since_bounce = y - (height - r / 2);
if (travel_since_bounce > 0) {
// after bounce we should have gone in the other direction
// so undo travel and travel in the opposite direction,
// therefore we need to subtract travel * 2 from y
y = y - travel_since_bounce * 2;
dy = -dy; // change direction
mySnd.play();
}
dy = dy + g; // gravity
}
]]>This is a test. Line spacing is 100.
function setup() {
createCanvas(800, 800);
}
function draw() {
background(200, 200, 150);
for (var i = 0; i <= 800; i += 100) {
line(i, 0, i, 800);
line(0, i, 800, i);
}
noLoop();
}
]]>Sound Test
type 1 2 3 4 to sketch to play sounds
var one, two, three, four;
function preload() {
one = loadSound("https://courses.ideate.cmu.edu/15-104/f2017/wp-content/uploads/2017/12/one.wav");
two = loadSound("https://courses.ideate.cmu.edu/15-104/f2017/wp-content/uploads/2017/12/two.wav");
three = loadSound("https://courses.ideate.cmu.edu/15-104/f2017/wp-content/uploads/2017/12/three.wav");
four = loadSound("https://courses.ideate.cmu.edu/15-104/f2017/wp-content/uploads/2017/12/four.wav");
}
function setup() {
createCanvas(200, 200);
background(200);
}
function keyPressed() {
print("" + keyCode);
if (keyCode == 49) one.play();
else if (keyCode == 50) two.play();
else if (keyCode == 51) three.play();
else if (keyCode == 52) four.play();
}
]]>