From 465e09b9034498dd8e75165d0f4aad56cfd7289c Mon Sep 17 00:00:00 2001 From: maxluli Date: Tue, 17 May 2022 09:11:02 +0200 Subject: [PATCH] Changed from bloc shape to ball shape and modified colors --- ReadMe.md | 8 +++---- src/main.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 8c2104e..d043952 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -18,13 +18,13 @@ cargo run --release ## Controls -W,A,S,D and Arrow Keys are for moving around the cube. +W,A,S,D and Arrow Keys are for moving around the ball. -Left Click teleports the block the cursors location +Left Click teleports the ball to the cursors location. -Right Click cancels any velocity of the cube +Right Click cancels any velocity of the ball. -Tab toggles a debug mode where you can see the actual vector applied to the cube (maybe even more in the futur) +Tab toggles a debug mode where you can see the actual vector applied to the ball wich is render as a cube to see its hitbox (maybe even more in the futur) Spacebar doubles the acceleration (to use with moderation) diff --git a/src/main.rs b/src/main.rs index 54e191d..71b70ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ const BLOC_SPEED:f32 = 3.0; const MAX_SPEED:f32 = 60.0; const DEBUG_FX_SIZE_RATIO:i32 = 2; -const BOUNCE_RATIO:f32 = 80.0; +const BOUNCE_RATIO:f32 = 60.0; pub struct Game{ main_bloc:Entity, @@ -22,13 +22,19 @@ impl Game{ pub fn render(&mut self,ctx:&mut sdl2::render::Canvas){ ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64)); ctx.clear(); - ctx.set_draw_color(Color::RGB(0x33, 0x33, 0x33)); - ctx.fill_rect(self.main_bloc.shape).expect("Could not fill the rectangle"); + let main_bloc_color:Color = Color::RGB(0x33, 0x33, 0x33); + let main_bloc_outline:Color = Color::RGB(0xFC, 0xEC, 0xC9); + let center:Point = Point::new(self.main_bloc.shape.x + (self.main_bloc.shape.width() / 2) as i32,self.main_bloc.shape.y + (self.main_bloc.shape.width() / 2) as i32); if self.draw_debug { - ctx.set_draw_color(Color::RGB(0x8b,0x00,0x00)); - let center:Point = Point::new(self.main_bloc.shape.x + (self.main_bloc.shape.width() / 2) as i32,self.main_bloc.shape.y + (self.main_bloc.shape.width() / 2) as i32); + ctx.set_draw_color(main_bloc_color); + ctx.fill_rect(self.main_bloc.shape).expect("Could not fill the rectangle"); + ctx.set_draw_color(main_bloc_outline); + ctx.draw_rect(self.main_bloc.shape).expect("Could not ouline the rectangle"); let apex:Point = Point::new(center.x + (self.main_bloc.velocity.x * DEBUG_FX_SIZE_RATIO) as i32,center.y + (self.main_bloc.velocity.y * DEBUG_FX_SIZE_RATIO) as i32); ctx.draw_line(center,apex).expect("Could not render the Debug FX"); + }else{ + fill_circle(ctx, center.x, center.y,self.main_bloc.shape.width() as i32 / 2, main_bloc_color); + draw_circle(ctx,center.x,center.y,self.main_bloc.shape.width() as i32 / 2, main_bloc_outline); } } pub fn up(&mut self){ @@ -67,7 +73,7 @@ impl Game{ self.main_bloc.shape.set_x(mouse_position.x - (self.main_bloc.shape.width() / 2) as i32); self.main_bloc.shape.set_y(mouse_position.y - (self.main_bloc.shape.height() / 2) as i32); } - pub fn right_click(&mut self,mouse_position:Point){ + pub fn right_click(&mut self,_mouse_position:Point){ // Removes any vectors self.main_bloc.velocity = Point::new(0,0); } @@ -118,7 +124,57 @@ impl Game{ bloc.shape.set_y(posy); } } +//https://stackoverflow.com/questions/65723827/sdl2-function-to-draw-a-filled-circle ## Translated to Rust +pub fn fill_circle(ctx:&mut sdl2::render::Canvas, x:i32, y:i32, radius:i32, color:Color) +{ + ctx.set_draw_color(color); + let diameter = radius * 2; + for w in 0..diameter{ + for h in 0..diameter{ + let dx = radius - w; // horizontal offset + let dy = radius - h; // vertical offset + if (dx*dx + dy*dy) <= (radius * radius) + { + ctx.draw_point(Point::new(x + dx, y + dy)).expect("Trouble rendering the circle"); + } + } + } +} +// https://stackoverflow.com/questions/38334081/howto-draw-circles-arcs-and-vector-graphics-in-sdl ## Translated to Rust +pub fn draw_circle(ctx:&mut sdl2::render::Canvas, centre_x:i32, centre_y:i32, radius:i32, color:Color) +{ + ctx.set_draw_color(color); + let diameter = radius * 2; + let mut x:i32 = radius - 1; + let mut y:i32 = 0; + let mut tx:i32 = 1; + let mut ty:i32 = 1; + let mut error:i32 = tx - diameter; + while x >= y + { + // Each of the following renders an octant of the circle + ctx.draw_point(Point::new(centre_x + x, centre_y - y)).expect("Trouble rendering the circle"); + ctx.draw_point(Point::new(centre_x + x, centre_y + y)).expect("Trouble rendering the circle"); + ctx.draw_point(Point::new(centre_x - x, centre_y - y)).expect("Trouble rendering the circle"); + ctx.draw_point(Point::new(centre_x - x, centre_y + y)).expect("Trouble rendering the circle"); + ctx.draw_point(Point::new(centre_x + y, centre_y - x)).expect("Trouble rendering the circle"); + ctx.draw_point(Point::new(centre_x + y, centre_y + x)).expect("Trouble rendering the circle"); + ctx.draw_point(Point::new(centre_x - y, centre_y - x)).expect("Trouble rendering the circle"); + ctx.draw_point(Point::new(centre_x - y, centre_y + x)).expect("Trouble rendering the circle"); + + if error <= 0{ + y+= 1; + error += ty; + ty += 2; + } + if error > 0{ + x -= 1; + tx += 2; + error += tx - diameter; + } + } +} pub struct Entity{ shape:Rect, velocity:Point,