use sdl2::pixels::Color; use sdl2::rect::Point; use sdl2::rect::Rect; use sdl2::render::Canvas; use sdl2::video::Window; use {SCREEN_HEIGHT, SCREEN_WIDTH}; use common::Point2D; pub struct Boll { pub pos: Point2D, pub vel: Point2D, } impl Boll { pub fn update(&mut self) { self.vel.y += 0.1; self.pos += self.vel; if self.pos.x < 0.0 { self.pos.x = -self.pos.x; self.vel.x = -self.vel.x; } if self.pos.x > SCREEN_WIDTH as f64 { self.pos.x = SCREEN_WIDTH as f64 - (self.pos.x - SCREEN_WIDTH as f64); self.vel.x = -self.vel.x; } if self.pos.y < 0.0 { self.pos.y = -self.pos.y; self.vel.y = -self.vel.y; } if self.pos.y > SCREEN_HEIGHT as f64 { self.pos.y = SCREEN_HEIGHT as f64 - (self.pos.y - SCREEN_HEIGHT as f64); self.vel.y = -self.vel.y; } } pub fn draw(&mut self,canvas: &mut Canvas, size: u32) { canvas.set_draw_color(Color::RGBA( 255 - std::cmp::min(255, (self.vel.length() * 25.0) as u8), (255.0 * (self.pos.x / SCREEN_WIDTH as f64)) as u8, (255.0 * (self.pos.y / SCREEN_HEIGHT as f64)) as u8, 128 )); let mut r = Rect::new(0, 0, size, size); r.center_on(Point::new(self.pos.x as i32, self.pos.y as i32)); canvas.fill_rect(r).unwrap(); } }