use sdl2::pixels::Color; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::keyboard::Scancode; use std::collections::HashSet; use std::time::Duration; use sdl2::rect::Point; use sdl2::rect::Rect; const WINDOW_HEIGHT:i32 = 800; const WINDOW_WIDTH:i32 = 1000; const BLOC_SPEED:f32 = 3.0; const MAX_SPEED:f32 = 20.0; const GRAVITY:f32 = 10.0; const BOUNCE_RATIO:f32 = 110.0; pub struct Game{ main_bloc:Entity, } impl Game{ pub fn render(&mut self,ctx:&mut sdl2::render::Canvas){ ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64)); ctx.clear(); //Rendering main_bloc ctx.set_draw_color(Color::RGB(0x33, 0x33, 0x33)); ctx.fill_rect(self.main_bloc.shape).expect("Could not fill the rectangle"); //ctx.set_draw_color(Color::RGB(0xFF, 0xFF, 0xFF)); //ctx.draw_rect(self.main_bloc.shape).expect("Could not outline the rectangle"); } pub fn up(&mut self){ // 'w' or the up arrow has been pressed //println!("DEBUG: Up is pressed"); self.main_bloc.velocity.y -= BLOC_SPEED as i32; if self.main_bloc.velocity.y < -MAX_SPEED as i32{ self.main_bloc.velocity.y = -MAX_SPEED as i32; } } pub fn down(&mut self){ // 's' or the down arrow has been pressed //println!("DEBUG: Down is pressed"); self.main_bloc.velocity.y += BLOC_SPEED as i32; if self.main_bloc.velocity.y > MAX_SPEED as i32{ self.main_bloc.velocity.y = MAX_SPEED as i32; } } pub fn left(&mut self){ // 'a' or the left arrow has been pressed //println!("DEBUG: Left is pressed"); self.main_bloc.velocity.x -= BLOC_SPEED as i32; if self.main_bloc.velocity.x < -MAX_SPEED as i32{ self.main_bloc.velocity.x = -MAX_SPEED as i32; } } pub fn right(&mut self){ // 'd' or the right arrow has been pressed //println!("DEBUG: Right is pressed"); self.main_bloc.velocity.x += BLOC_SPEED as i32; if self.main_bloc.velocity.y > MAX_SPEED as i32{ self.main_bloc.velocity.y = MAX_SPEED as i32; } } pub fn update(&mut self){ let bloc = &mut self.main_bloc; let mut posx = bloc.shape.x; let mut posy = bloc.shape.y; let width = bloc.shape.width() as i32; let height = bloc.shape.height() as i32; if posy + height >= WINDOW_HEIGHT{ //we hit rock bottom posy = WINDOW_HEIGHT - height; bloc.velocity.y = -(bloc.velocity.y as f32 / 100.0 * BOUNCE_RATIO) as i32; } if posy <= 0{ //we hit top posy = 0; bloc.velocity.y = -(bloc.velocity.y as f32 / 100.0 * BOUNCE_RATIO) as i32; } if posx + width >= WINDOW_WIDTH{ //we hit right posx = WINDOW_WIDTH - width; bloc.velocity.x = -(bloc.velocity.x as f32 / 100.0 * BOUNCE_RATIO) as i32; } if posx <= 0{ //we hit left posx = 0; bloc.velocity.x = -(bloc.velocity.x as f32 / 100.0 * BOUNCE_RATIO) as i32; } if bloc.velocity.x > MAX_SPEED as i32{ bloc.velocity.x = MAX_SPEED as i32; } if bloc.velocity.x < -MAX_SPEED as i32{ bloc.velocity.x = -MAX_SPEED as i32; } if bloc.velocity.y > MAX_SPEED as i32{ bloc.velocity.y = MAX_SPEED as i32; } if bloc.velocity.y < -MAX_SPEED as i32{ bloc.velocity.y = -MAX_SPEED as i32; } posx += bloc.velocity.x; posy += bloc.velocity.y; bloc.shape.set_x(posx); bloc.shape.set_y(posy); } } pub struct Entity{ shape:Rect, velocity:Point, } pub fn main_loop(canvas: &mut sdl2::render::Canvas,event_pump:&mut sdl2::EventPump,game:&mut Game){ 'running: loop { for event in event_pump.poll_iter() { match event { Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { break 'running }, Event::KeyDown { keycode: Some(Keycode::W), .. } | Event::KeyDown {keycode: Some(Keycode::Up), ..} => { //pressed up game.up(); }, Event::KeyDown { keycode: Some(Keycode::S), .. } | Event::KeyDown { keycode: Some(Keycode::Down), .. } => { //pressed up game.down(); }, Event::KeyDown { keycode: Some(Keycode::A), .. } | Event::KeyDown { keycode: Some(Keycode::Left), .. } => { //pressed up game.left(); }, Event::KeyDown { keycode: Some(Keycode::D), .. } | Event::KeyDown { keycode: Some(Keycode::Right), .. } => { //pressed up game.right(); }, _ => {} } } // MAIN LOOP game.update(); game.render(canvas); // MAIN LOOP canvas.present(); ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); } } pub fn main() { let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); let window = video_subsystem.window("Test SDL Rust", WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32) .position_centered() .build() .unwrap(); let mut canvas = window.into_canvas().build().unwrap(); let mut event_pump = sdl_context.event_pump().unwrap(); canvas.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64)); let main_bloc_width:i32 = 50; let main_bloc_height:i32 = 50; let main_bloc_position:Point = Point::new(WINDOW_WIDTH / 2 - main_bloc_width / 2,WINDOW_HEIGHT / 2 - main_bloc_height / 2); let main_bloc_rectangle = Rect::new(main_bloc_position.x,main_bloc_position.y, main_bloc_width as u32, main_bloc_height as u32); let main_bloc_velocity = Point::new(0,0); let main_entity = Entity{ shape:main_bloc_rectangle, velocity:main_bloc_velocity }; let mut game = Game{main_bloc:main_entity}; main_loop(&mut canvas,&mut event_pump,&mut game); }