Skip to main content

World

Struct World 

Source
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

Source

pub fn new() -> World

A world with 30 small balls and no gravity.

Examples found in repository?
examples/balls.rs (line 6)
3fn main() -> std::io::Result<()> {
4    // The mouse pulls the balls in, the right button pushes them away, a click
5    // adds a ball. Press q to quit.
6    World::new()
7        .balls(5)
8        .size(3.5)
9        .gravity(0.02)
10        .mouse_pulls()
11        .trails()
12        .run()
13}
More examples
Hide additional examples
examples/bubbles.rs (line 5)
3fn main() -> std::io::Result<()> {
4    // Bubbles float up (gravity is negative). The mouse pulls them. Click to add one.
5    World::new()
6        .balls(14)
7        .size(1.6)
8        .gravity(-0.004)
9        .mouse_pulls()
10        .trails()
11        .palette(Palette::ocean())
12        .run()
13}
Source

pub fn balls(self, count: usize) -> World

How many balls to start with.

Examples found in repository?
examples/balls.rs (line 7)
3fn main() -> std::io::Result<()> {
4    // The mouse pulls the balls in, the right button pushes them away, a click
5    // adds a ball. Press q to quit.
6    World::new()
7        .balls(5)
8        .size(3.5)
9        .gravity(0.02)
10        .mouse_pulls()
11        .trails()
12        .run()
13}
More examples
Hide additional examples
examples/bubbles.rs (line 6)
3fn main() -> std::io::Result<()> {
4    // Bubbles float up (gravity is negative). The mouse pulls them. Click to add one.
5    World::new()
6        .balls(14)
7        .size(1.6)
8        .gravity(-0.004)
9        .mouse_pulls()
10        .trails()
11        .palette(Palette::ocean())
12        .run()
13}
Source

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?
examples/balls.rs (line 8)
3fn main() -> std::io::Result<()> {
4    // The mouse pulls the balls in, the right button pushes them away, a click
5    // adds a ball. Press q to quit.
6    World::new()
7        .balls(5)
8        .size(3.5)
9        .gravity(0.02)
10        .mouse_pulls()
11        .trails()
12        .run()
13}
More examples
Hide additional examples
examples/bubbles.rs (line 7)
3fn main() -> std::io::Result<()> {
4    // Bubbles float up (gravity is negative). The mouse pulls them. Click to add one.
5    World::new()
6        .balls(14)
7        .size(1.6)
8        .gravity(-0.004)
9        .mouse_pulls()
10        .trails()
11        .palette(Palette::ocean())
12        .run()
13}
Source

pub fn gravity(self, gravity: f32) -> World

A downward pull each frame. Try 0.02.

Examples found in repository?
examples/balls.rs (line 9)
3fn main() -> std::io::Result<()> {
4    // The mouse pulls the balls in, the right button pushes them away, a click
5    // adds a ball. Press q to quit.
6    World::new()
7        .balls(5)
8        .size(3.5)
9        .gravity(0.02)
10        .mouse_pulls()
11        .trails()
12        .run()
13}
More examples
Hide additional examples
examples/bubbles.rs (line 8)
3fn main() -> std::io::Result<()> {
4    // Bubbles float up (gravity is negative). The mouse pulls them. Click to add one.
5    World::new()
6        .balls(14)
7        .size(1.6)
8        .gravity(-0.004)
9        .mouse_pulls()
10        .trails()
11        .palette(Palette::ocean())
12        .run()
13}
Source

pub fn mouse_pulls(self) -> World

The mouse attracts the balls; holding the right button pushes them away.

Examples found in repository?
examples/balls.rs (line 10)
3fn main() -> std::io::Result<()> {
4    // The mouse pulls the balls in, the right button pushes them away, a click
5    // adds a ball. Press q to quit.
6    World::new()
7        .balls(5)
8        .size(3.5)
9        .gravity(0.02)
10        .mouse_pulls()
11        .trails()
12        .run()
13}
More examples
Hide additional examples
examples/bubbles.rs (line 9)
3fn main() -> std::io::Result<()> {
4    // Bubbles float up (gravity is negative). The mouse pulls them. Click to add one.
5    World::new()
6        .balls(14)
7        .size(1.6)
8        .gravity(-0.004)
9        .mouse_pulls()
10        .trails()
11        .palette(Palette::ocean())
12        .run()
13}
Source

pub fn trails(self) -> World

Balls leave glowing tails.

Examples found in repository?
examples/balls.rs (line 11)
3fn main() -> std::io::Result<()> {
4    // The mouse pulls the balls in, the right button pushes them away, a click
5    // adds a ball. Press q to quit.
6    World::new()
7        .balls(5)
8        .size(3.5)
9        .gravity(0.02)
10        .mouse_pulls()
11        .trails()
12        .run()
13}
More examples
Hide additional examples
examples/bubbles.rs (line 10)
3fn main() -> std::io::Result<()> {
4    // Bubbles float up (gravity is negative). The mouse pulls them. Click to add one.
5    World::new()
6        .balls(14)
7        .size(1.6)
8        .gravity(-0.004)
9        .mouse_pulls()
10        .trails()
11        .palette(Palette::ocean())
12        .run()
13}
Source

pub fn palette(self, palette: Palette) -> World

The colours balls are drawn in, by speed.

Examples found in repository?
examples/bubbles.rs (line 11)
3fn main() -> std::io::Result<()> {
4    // Bubbles float up (gravity is negative). The mouse pulls them. Click to add one.
5    World::new()
6        .balls(14)
7        .size(1.6)
8        .gravity(-0.004)
9        .mouse_pulls()
10        .trails()
11        .palette(Palette::ocean())
12        .run()
13}
Source

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);
Source

pub fn run(self) -> Result<()>

Runs the world until the user presses q.

Examples found in repository?
examples/balls.rs (line 12)
3fn main() -> std::io::Result<()> {
4    // The mouse pulls the balls in, the right button pushes them away, a click
5    // adds a ball. Press q to quit.
6    World::new()
7        .balls(5)
8        .size(3.5)
9        .gravity(0.02)
10        .mouse_pulls()
11        .trails()
12        .run()
13}
More examples
Hide additional examples
examples/bubbles.rs (line 12)
3fn main() -> std::io::Result<()> {
4    // Bubbles float up (gravity is negative). The mouse pulls them. Click to add one.
5    World::new()
6        .balls(14)
7        .size(1.6)
8        .gravity(-0.004)
9        .mouse_pulls()
10        .trails()
11        .palette(Palette::ocean())
12        .run()
13}

Trait Implementations§

Source§

impl Clone for World

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for World

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for World

Source§

fn default() -> World

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for World

§

impl RefUnwindSafe for World

§

impl Send for World

§

impl Sync for World

§

impl Unpin for World

§

impl UnsafeUnpin for World

§

impl UnwindSafe for World

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.