Changed from bloc shape to ball shape and modified colors
This commit is contained in:
@@ -18,13 +18,13 @@ cargo run --release
|
|||||||
|
|
||||||
## Controls
|
## 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)
|
Spacebar doubles the acceleration (to use with moderation)
|
||||||
|
|
||||||
|
|||||||
68
src/main.rs
68
src/main.rs
@@ -12,7 +12,7 @@ const BLOC_SPEED:f32 = 3.0;
|
|||||||
const MAX_SPEED:f32 = 60.0;
|
const MAX_SPEED:f32 = 60.0;
|
||||||
const DEBUG_FX_SIZE_RATIO:i32 = 2;
|
const DEBUG_FX_SIZE_RATIO:i32 = 2;
|
||||||
|
|
||||||
const BOUNCE_RATIO:f32 = 80.0;
|
const BOUNCE_RATIO:f32 = 60.0;
|
||||||
|
|
||||||
pub struct Game{
|
pub struct Game{
|
||||||
main_bloc:Entity,
|
main_bloc:Entity,
|
||||||
@@ -22,13 +22,19 @@ impl Game{
|
|||||||
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){
|
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){
|
||||||
ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64));
|
ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64));
|
||||||
ctx.clear();
|
ctx.clear();
|
||||||
ctx.set_draw_color(Color::RGB(0x33, 0x33, 0x33));
|
let main_bloc_color:Color = Color::RGB(0x33, 0x33, 0x33);
|
||||||
ctx.fill_rect(self.main_bloc.shape).expect("Could not fill the rectangle");
|
let main_bloc_outline:Color = Color::RGB(0xFC, 0xEC, 0xC9);
|
||||||
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);
|
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(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);
|
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");
|
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){
|
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_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);
|
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
|
// Removes any vectors
|
||||||
self.main_bloc.velocity = Point::new(0,0);
|
self.main_bloc.velocity = Point::new(0,0);
|
||||||
}
|
}
|
||||||
@@ -118,7 +124,57 @@ impl Game{
|
|||||||
bloc.shape.set_y(posy);
|
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<sdl2::video::Window>, 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<sdl2::video::Window>, 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{
|
pub struct Entity{
|
||||||
shape:Rect,
|
shape:Rect,
|
||||||
velocity:Point,
|
velocity:Point,
|
||||||
|
|||||||
Reference in New Issue
Block a user