Bouncing ball example
()
| 56 | /// Bouncing ball example |
| 57 | /// |
| 58 | pub fn main() { |
| 59 | // 'with_2d_graphics' is used to support operating systems that can't run event loops anywhere other than the main thread |
| 60 | with_2d_graphics(|| { |
| 61 | // Create a window with a canvas to draw on |
| 62 | let canvas = create_drawing_window("Bouncing balls"); |
| 63 | |
| 64 | // Generate some random balls |
| 65 | let mut balls = (0..256).into_iter().map(|_| Ball::random()).collect::<Vec<_>>(); |
| 66 | |
| 67 | // Animate them |
| 68 | loop { |
| 69 | // Update the balls for this frame |
| 70 | for ball in balls.iter_mut() { |
| 71 | ball.update(); |
| 72 | } |
| 73 | |
| 74 | // Render the frame |
| 75 | canvas.draw(|gc| { |
| 76 | gc.clear_canvas(Color::Rgba(0.6, 0.7, 0.8, 1.0)); |
| 77 | gc.canvas_height(1000.0); |
| 78 | gc.center_region(0.0, 0.0, 1000.0, 1000.0); |
| 79 | |
| 80 | for ball in balls.iter() { |
| 81 | gc.circle(ball.x as f32, ball.y as f32, ball.radius as f32); |
| 82 | gc.fill_color(ball.col); |
| 83 | gc.fill(); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | // Wait for the next frame |
| 88 | thread::sleep(Duration::from_nanos(1_000_000_000 / 60)); |
| 89 | } |
| 90 | }); |
| 91 | } |
nothing calls this directly
no test coverage detected