schets met een dansende bal terug naar de inleiding
float x = 0;
float y = 0;
float xsnelh = 4;
float ysnelh = 1;
void setup() {
size(960, 540);
}
void draw() {
background(255);
fill(255,0,0);
ellipse(x, y, 20, 20);
x = x + xsnelh;
y = y + ysnelh;
if(x > width || x < 0) xsnelh = xsnelh*-1;
if(y > height || y < 0) ysnelh = ysnelh*-1;
}
schets met de dansende bal nu met functies die in de draw functie worden aangeroepen
float x;
float y;
float xsnelh;
float ysnelh;
void setup() {
size(960, 540);
x = 0;
y = 0;
xsnelh = 4;
ysnelh = 1;
}
void draw() {
background(#E6FBFF);
display();
snelh();
randcontrole();
}
void snelh() {
x = x + xsnelh;
y = y + ysnelh;
}
void randcontrole() {
if (x > width || x < 0) xsnelh = xsnelh*-1;
if (y > height || y < 0) ysnelh = ysnelh*-1;
}
void display() {
fill(255, 0, 0);
ellipse(x, y, 20, 20);
}
void keyPressed() {
if (key == 's') {
noLoop();
}
if (key == 'r') {
loop();
}
}