1 use sdl2::pixels::Color;
4 use sdl2::render::Canvas;
5 use sdl2::video::Window;
7 use {SCREEN_HEIGHT, SCREEN_WIDTH};
9 use sdl2::gfx::primitives::DrawRenderer;
13 fn draw(&mut self, canvas: &mut Canvas<Window>, size: u32);
16 pub struct SquareBoll {
17 pub pos: Point2D<f64>,
18 pub vel: Point2D<f64>,
21 impl Boll for SquareBoll {
22 fn update(&mut self) {
27 self.pos.x = -self.pos.x;
28 self.vel.x = -self.vel.x;
30 if self.pos.x > SCREEN_WIDTH as f64 {
31 self.pos.x = SCREEN_WIDTH as f64 - (self.pos.x - SCREEN_WIDTH as f64);
32 self.vel.x = -self.vel.x;
35 self.pos.y = -self.pos.y;
36 self.vel.y = -self.vel.y;
38 if self.pos.y > SCREEN_HEIGHT as f64 {
39 self.pos.y = SCREEN_HEIGHT as f64 - (self.pos.y - SCREEN_HEIGHT as f64);
40 self.vel.y = -self.vel.y;
44 fn draw(&mut self, canvas: &mut Canvas<Window>, size: u32) {
45 canvas.set_draw_color(Color::RGBA(
46 255 - std::cmp::min(255, (self.vel.length() * 25.0) as u8),
47 (255.0 * (self.pos.x / SCREEN_WIDTH as f64)) as u8,
48 (255.0 * (self.pos.y / SCREEN_HEIGHT as f64)) as u8, 128
50 let mut r = Rect::new(0, 0, size, size);
51 r.center_on(Point::new(self.pos.x as i32, self.pos.y as i32));
52 canvas.fill_rect(r).unwrap();
57 pub struct CircleBoll {
62 pub fn new(pos: Point2D<f64>, vel: Point2D<f64>) -> CircleBoll {
72 impl Boll for CircleBoll {
73 fn update(&mut self) {
77 fn draw(&mut self, canvas: &mut Canvas<Window>, size: u32) {
78 let val = 255 - std::cmp::min(255, (self.boll.vel.length() * 20.0) as u8);
79 canvas.filled_circle(self.boll.pos.x as i16, self.boll.pos.y as i16, size as i16, Color::RGBA(