use flo_draw::*;
use flo_canvas::*;
use rand::*;
use std::thread;
use std::time::{Duration};
struct Ball {
col: Color,
radius: f64,
x: f64,
y: f64,
dx: f64,
dy: f64
}
impl Ball {
pub fn random() -> Ball {
Ball {
col: Color::Hsluv(random::<f32>()*360.0, random::<f32>()*100.0, random::<f32>()*75.0 + 25.0, 1.0),
radius: random::<f64>() * 16.0 + 16.0,
x: random::<f64>() * 1000.0,
y: random::<f64>() * 1000.0 + 64.0,
dx: random::<f64>() * 8.0 - 4.0,
dy: random::<f64>() * 8.0 - 4.0
}
}
pub fn update(&mut self) {
if self.x+self.dx+self.radius > 1000.0 && self.dx > 0.0 { self.dx = -self.dx; }
if self.y+self.dy+self.radius > 1000.0 && self.dy > 0.0 { self.dy = -self.dy; }
if self.x+self.dx-self.radius < 0.0 && self.dx < 0.0 { self.dx = -self.dx; }
if self.y+self.dy-self.radius < 0.0 && self.dy < 0.0 { self.dy = -self.dy; }
if self.y >= self.radius {
self.dy -= 0.2;
}
self.x += self.dx;
self.y += self.dy;
}
}
pub fn main() {
with_2d_graphics(|| {
let canvas = create_drawing_window("Bouncing balls");
let mut balls = (0..256).into_iter().map(|_| Ball::random()).collect::<Vec<_>>();
loop {
for ball in balls.iter_mut() {
ball.update();
}
canvas.draw(|gc| {
gc.clear_canvas(Color::Rgba(0.6, 0.7, 0.8, 1.0));
gc.canvas_height(1000.0);
gc.center_region(0.0, 0.0, 1000.0, 1000.0);
for ball in balls.iter() {
gc.circle(ball.x as f32, ball.y as f32, ball.radius as f32);
gc.fill_color(ball.col);
gc.fill();
}
});
thread::sleep(Duration::from_nanos(1_000_000_000 / 60));
}
});
}