You are on page 1of 2

Latihan buat Animasi

1. Animasi Acak
void setup() {
size(500, 500);
}
void draw() {
float x = random(500);
float y = random(500);
float w = random(100);
float h = random(100);
fill(random(255), random(255), random(255));
rect(x, y, w, h);
ellipse(x, y, w, h);
triangle(random(500),random(500),random(500),random(500),random(500),random(500));
}

2. Animasi Bola 1
float y = 50.0;
float speed = 2.0;
float radius = 15.0;
void setup() {
size(100, 100);
noStroke();
ellipseMode(RADIUS);
}
void draw() {
background(0);
ellipse(33, y, radius, radius);
y = y + speed;
if (y > height+radius) {
y = -radius;
}
}

3. Animasi Bola 2 (memantul)


float y = 50.0;
float speed = 1.0;
float radius = 15.0;
int direction = 1;
void setup() {
size(100, 100);
noStroke();
ellipseMode(RADIUS);
}
void draw() {
fill(0, 12);
rect(0, 0, width, height);
fill(255);
ellipse(33, y, radius, radius);
y += speed * direction;
if ((y > height-radius) || (y < radius)) {
direction = -direction;
}
}
4. Animasi Bola 3 (memantul)

float x = 50.0; // X-coordinate


float y = 50.0; // Y-coordinate
float radius = 15.0; // Radius of the circle
float speedX = 1.0; // Speed of motion on the x-axis
float speedY = 0.4; // Speed of motion on the y-axis
int directionX = 1; // Direction of motion on the x-axis
int directionY = -1; // Direction of motion on the y-axis

void setup() {
size(100, 100);
noStroke();
ellipseMode(RADIUS);
}

void draw() {
fill(0, 12);
rect(0, 0, width, height);
fill(255);
ellipse(x, y, radius, radius);
x += speedX * directionX;
if ((x > width-radius) || (x < radius)) {
directionX = -directionX; // Change direction
}
y += speedY * directionY;
if ((y > height-radius) || (y < radius)) {
directionY = -directionY; // Change direction
}
}

You might also like