From 430f3d6dbf8af468930e4dea4fe84ec5d9ce4136 Mon Sep 17 00:00:00 2001 From: maxluli Date: Wed, 11 May 2022 14:42:59 +0200 Subject: [PATCH] the bloc is now bouncing on the walls with a specified bouncing ratio --- src/main.rs | 98 ++++++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/src/main.rs b/src/main.rs index e277edf..3cbed16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,19 @@ 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 = 600; -const WINDOW_WIDTH:i32 = 800; +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, @@ -57,62 +62,50 @@ impl Game{ } } pub fn update(&mut self){ - let mut posx = self.main_bloc.shape.x; - let mut posy = self.main_bloc.shape.y; - posx += self.main_bloc.velocity.x; - posy += self.main_bloc.velocity.y; - let bloc_width:i32 = self.main_bloc.shape.width() as i32; - let bloc_height:i32 = self.main_bloc.shape.height() as i32; + 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 posx + bloc_width > WINDOW_WIDTH{ - posx = WINDOW_WIDTH - bloc_width as i32; - self.main_bloc.velocity.x = 0; - }else{ - if posx < 0{ - posx = 0; - self.main_bloc.velocity.x = 0; - } + 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 + bloc_height > WINDOW_HEIGHT{ - posy = WINDOW_HEIGHT - bloc_height as i32; - self.main_bloc.velocity.y = 0; - }else{ - if posy < 0{ - posy = 0; - self.main_bloc.velocity.y = 0; - } + 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 self.main_bloc.velocity.x > 0{ - if self.main_bloc.velocity.x < BLOC_SPEED as i32{ - self.main_bloc.velocity.x = 0; - }else{ - self.main_bloc.velocity.x -= (BLOC_SPEED / 2.0) as i32; - } - }else{ - if self.main_bloc.velocity.x > -BLOC_SPEED as i32{ - self.main_bloc.velocity.x = 0; - }else{ - self.main_bloc.velocity.x += (BLOC_SPEED / 2.0) 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; } - if self.main_bloc.velocity.y > 0{ - if self.main_bloc.velocity.y < BLOC_SPEED as i32{ - self.main_bloc.velocity.y = 0; - }else{ - self.main_bloc.velocity.y -= (BLOC_SPEED / 2.0) as i32; - } - }else{ - if self.main_bloc.velocity.y > -BLOC_SPEED as i32{ - self.main_bloc.velocity.y = 0; - }else{ - self.main_bloc.velocity.y += (BLOC_SPEED / 2.0) as i32; - } - } - - self.main_bloc.shape.set_x(posx); - self.main_bloc.shape.set_y(posy); + posx += bloc.velocity.x; + posy += bloc.velocity.y; + bloc.shape.set_x(posx); + bloc.shape.set_y(posy); } } @@ -123,7 +116,6 @@ pub struct Entity{ pub fn main_loop(canvas: &mut sdl2::render::Canvas,event_pump:&mut sdl2::EventPump,game:&mut Game){ 'running: loop { - canvas.clear(); for event in event_pump.poll_iter() { match event { Event::Quit {..} |