Created
September 25, 2018 10:16
-
-
Save eira-fransham/3958e738df4786b2c84a664f0605f178 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![feature(nll)] | |
| extern crate pollock; | |
| extern crate serde; | |
| #[macro_use] | |
| extern crate serde_derive; | |
| use pollock::*; | |
| #[derive(Serialize, Deserialize)] | |
| struct State { | |
| balls: Vec<(V2, V2)>, | |
| } | |
| fn main() { | |
| let radius = 10.; | |
| Pollock::setup(|p| { | |
| p.size = (600., 600.); | |
| p.background = Color::new(255, 255, 255, 255); | |
| p.stroke = Stroke::new(rgb(0, 0, 0), 4); | |
| p.fill = Fill::flat(rgba(255, 0, 0, 100)); | |
| State { | |
| balls: (0..10000) | |
| .map(|_| { | |
| ( | |
| v2( | |
| p.random_range(0., p.width() as f64), | |
| p.random_range(0., p.height() as f64), | |
| ), | |
| v2( | |
| if p.random() { -1 } else { 1 }, | |
| if p.random() { -1 } else { 1 }, | |
| ), | |
| ) | |
| }).collect(), | |
| } | |
| }).draw(|p| { | |
| use std::f32::consts::PI; | |
| let (width, height) = p.size; | |
| for (pos, vel) in &mut p.state.balls { | |
| if pos.x < radius { | |
| vel.x = 1. | |
| } else if pos.x > width as f64 - radius { | |
| vel.x = -1. | |
| } | |
| if pos.y < radius { | |
| vel.y = 1. | |
| } else if pos.y > height as f64 - radius { | |
| vel.y = -1. | |
| } | |
| *pos += *vel * 1.; | |
| } | |
| for (i, (pos, _)) in p.state.balls.iter().enumerate() { | |
| let (r, g, b) = ( | |
| (((p.frame_count as f32 + i as f32) * PI / 60.).sin() + 1.) * 255. / 2., | |
| (((p.frame_count as f32 + 20. + i as f32) * PI / 60.).sin() + 1.) * 255. / 2., | |
| (((p.frame_count as f32 + 40. + i as f32) * PI / 60.).sin() + 1.) * 255. / 2., | |
| ); | |
| p.with_fill(Fill::flat(rgb(r as _, g as _, b as _))) | |
| .circle(*pos, radius); | |
| } | |
| }).on_key_down(Key::O, |p| p.save_state("state.json").unwrap()) | |
| .on_key_down(Key::P, |p| p.load_state("state.json").unwrap()) | |
| .run(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment