artworld/examples/balls.js
2025-03-12 23:51:24 -05:00

56 lines
1.0 KiB
JavaScript

import {
World,
Color,
Pico8,
Group,
Circle,
Random,
Vector2,
degToRad,
} from "../artworld/index.js";
let world = new World("mainCanvas");
window.world = world;
class Ball extends Circle {
constructor() {
super(world);
this.speed = new Vector2(0, Random.between(9, 15));
}
update() {
this.move(this.speed);
if (this.worldPos.y > world.height + 20) {
this.y(-20);
}
}
}
class GravityBall extends Circle {
constructor() {
super(world);
this.accel = new Vector2(0, 0.5);
this.speed = Vector2.random(0, 5);
}
update() {
this.speed = this.speed.add(this.accel);
this.move(this.speed);
if (this.worldPos.y > world.height - 10) {
this.speed = this.speed.scale(-0.98); // bounce w/ dampening
this.y(world.height - 10.01);
}
}
}
for (let i = 0; i < 21; i++) {
new Ball()
.pos(new Vector2(40 * i, 0))
.radius(10)
.fill(Pico8.BLUE);
new GravityBall()
.pos(new Vector2(20 + 40 * i, 0))
.radius(10)
.fill(Pico8.PURPLE);
}
world.loopStep();