pub struct World { /* private fields */ }Expand description
A world of bouncing balls, described with a few chained settings and
started with World::run.
Balls bounce off the walls and off each other. Move the mouse and they are
pulled towards it (with World::mouse_pulls); hold the right button and
they are pushed away. Click to add a ball. Press q to quit.
use terminal_graphics_interactive::World;
World::new()
.balls(5) // five balls
.size(3.5) // big ones
.gravity(0.02) // they fall and bounce
.mouse_pulls() // the pointer attracts them
.trails() // glowing tails
.run()?;Implementations§
Source§impl World
impl World
Sourcepub fn new() -> World
pub fn new() -> World
A world with 30 small balls and no gravity.
Examples found in repository?
More examples
Sourcepub fn balls(self, count: usize) -> World
pub fn balls(self, count: usize) -> World
How many balls to start with.
Examples found in repository?
More examples
Sourcepub fn size(self, radius: f32) -> World
pub fn size(self, radius: f32) -> World
How big each ball is, as a radius in cells. Try 3.5 for big ones. Never less than 0.5, and never bigger than the window.
Examples found in repository?
More examples
Sourcepub fn gravity(self, gravity: f32) -> World
pub fn gravity(self, gravity: f32) -> World
A downward pull each frame. Try 0.02.
Examples found in repository?
More examples
Sourcepub fn mouse_pulls(self) -> World
pub fn mouse_pulls(self) -> World
The mouse attracts the balls; holding the right button pushes them away.
Examples found in repository?
More examples
Sourcepub fn trails(self) -> World
pub fn trails(self) -> World
Balls leave glowing tails.
Examples found in repository?
More examples
Sourcepub fn simulation(&self, seed: u32) -> Simulation
pub fn simulation(&self, seed: u32) -> Simulation
The world as a Simulation you advance yourself, with no terminal:
for tests, for benchmarks, or to draw frames some other way. seed
decides where the balls start and which way clicks send them, so the
same seed always gives the same world.
use terminal_graphics_interactive::{Display, Pointer, World};
let mut simulation = World::new().balls(3).size(2.0).gravity(0.02).simulation(1);
simulation.resize(40, 12);
let mut display = Display::new(40, 12);
for _ in 0..30 {
simulation.step(&Pointer::default());
simulation.render(&mut display);
}
assert_eq!(simulation.positions().len(), 3);