Modified colors, window and ball size and have keypress detection to prevent key spam for toggling on and off features

This commit is contained in:
2022-05-17 13:22:43 +02:00
parent 465e09b903
commit 33befbc561

View File

@@ -7,78 +7,87 @@ 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 WINDOW_WIDTH:i32 = 500;
const BALL_SPEED:f32 = 3.0;
const MAX_SPEED:f32 = 60.0;
const DEBUG_FX_SIZE_RATIO:i32 = 2;
const BOUNCE_RATIO:f32 = 60.0;
const DEFAULT_BALL_WIDTH:i32 = 20;
const DEFAULT_START_BALL_LOCATION_X:i32 = WINDOW_WIDTH / 2 - DEFAULT_BALL_WIDTH / 2;
const DEFAULT_START_BALL_LOCATION_Y:i32 = (WINDOW_HEIGHT / 5) * 4 - DEFAULT_BALL_WIDTH / 2;
pub struct Game{
main_bloc:Entity,
main_ball:Entity,
draw_debug:bool,
}
ball_released:bool,
}
impl Game{
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){
ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64));
let main_ball_color:Color = Color::RGB(0xFA, 0xFD, 0xF6);
let main_ball_outline:Color = Color::RGB(0x17, 0x2A, 0x33);
let background_color:Color = Color::RGB(0x6A,0x9A,0x1D);
ctx.set_draw_color(background_color);
ctx.clear();
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);
let center:Point = Point::new(self.main_ball.shape.x + (self.main_ball.shape.width() / 2) as i32,self.main_ball.shape.y + (self.main_ball.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);
ctx.set_draw_color(main_ball_color);
ctx.fill_rect(self.main_ball.shape).expect("Could not fill the rectangle");
ctx.set_draw_color(main_ball_outline);
ctx.draw_rect(self.main_ball.shape).expect("Could not ouline the rectangle");
let apex:Point = Point::new(center.x + (self.main_ball.velocity.x * DEBUG_FX_SIZE_RATIO) as i32,center.y + (self.main_ball.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);
fill_circle(ctx, center.x, center.y,self.main_ball.shape.width() as i32 / 2, main_ball_color);
draw_circle(ctx,center.x,center.y,self.main_ball.shape.width() as i32 / 2, main_ball_outline);
draw_circle(ctx,center.x,center.y,self.main_ball.shape.width() as i32 / 2 + 1, main_ball_outline);
}
}
pub fn up(&mut self){
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;
self.main_ball.velocity.y -= BALL_SPEED as i32;
if self.main_ball.velocity.y < -MAX_SPEED as i32{
self.main_ball.velocity.y = -MAX_SPEED as i32;
}
}
pub fn down(&mut self){
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;
self.main_ball.velocity.y += BALL_SPEED as i32;
if self.main_ball.velocity.y > MAX_SPEED as i32{
self.main_ball.velocity.y = MAX_SPEED as i32;
}
}
pub fn left(&mut self){
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;
self.main_ball.velocity.x -= BALL_SPEED as i32;
if self.main_ball.velocity.x < -MAX_SPEED as i32{
self.main_ball.velocity.x = -MAX_SPEED as i32;
}
}
pub fn right(&mut self){
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;
self.main_ball.velocity.x += BALL_SPEED as i32;
if self.main_ball.velocity.y > MAX_SPEED as i32{
self.main_ball.velocity.y = MAX_SPEED as i32;
}
}
pub fn toggle_debug(&mut self){
self.draw_debug = !self.draw_debug;
}
pub fn double_speed(&mut self){
self.main_bloc.velocity.x *= 2;
self.main_bloc.velocity.y *= 2;
pub fn release(&mut self){
if self.ball_released{
self.main_ball.shape.set_x(DEFAULT_START_BALL_LOCATION_X);
self.main_ball.shape.set_y(DEFAULT_START_BALL_LOCATION_Y);
}
self.ball_released = !self.ball_released;
}
pub fn left_click(&mut self,mouse_position:Point){
// moves the bloc at the cursor position
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_ball.shape.set_x(mouse_position.x - (self.main_ball.shape.width() / 2) as i32);
self.main_ball.shape.set_y(mouse_position.y - (self.main_ball.shape.height() / 2) as i32);
}
pub fn right_click(&mut self,_mouse_position:Point){
// Removes any vectors
self.main_bloc.velocity = Point::new(0,0);
self.main_ball.velocity = Point::new(0,0);
}
pub fn update(&mut self){
let bloc = &mut self.main_bloc;
let bloc = &mut self.main_ball;
let mut posx = bloc.shape.x;
let mut posy = bloc.shape.y;
let width = bloc.shape.width() as i32;
@@ -118,10 +127,13 @@ impl Game{
bloc.velocity.y = -MAX_SPEED as i32;
}
posx += bloc.velocity.x;
posy += bloc.velocity.y;
if self.ball_released {
posx += bloc.velocity.x;
posy += bloc.velocity.y;
}
bloc.shape.set_x(posx);
bloc.shape.set_y(posy);
}
}
//https://stackoverflow.com/questions/65723827/sdl2-function-to-draw-a-filled-circle ## Translated to Rust
@@ -211,9 +223,11 @@ fn check_mouse_right_pressed(e: &sdl2::EventPump) -> Point{
return Point::new(-1,-1);
}
}
pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pump:&mut sdl2::EventPump,game:&mut Game){
'running: loop {
//let (mut up_state,mut down_state,mut left_state,mut right_state) = (false,false,false,false);
let (mut tab_state,mut space_state) = (false,false);
let mut quit = false;
loop{
if is_up_pressed(event_pump){
game.up();
}
@@ -227,10 +241,28 @@ pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pu
game.right();
}
if is_tab_pressed(event_pump){
game.toggle_debug();
if tab_state == false{
//Keydown
tab_state = true;
game.toggle_debug();
}
}else{
if tab_state == true{
//keyUp
tab_state = false
}
}
if is_space_pressed(event_pump){
game.double_speed();
if space_state == false{
//KeyDown
space_state = true;
game.release();
}
}else{
if space_state == true{
//KeyDown
space_state = false;
}
}
let mut mouse_position:Point = check_mouse_left_pressed(event_pump);
if mouse_position.x >=0 && mouse_position.y >= 0{
@@ -245,7 +277,8 @@ pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pu
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
quit = true;
break
},
_ => {}
}
@@ -258,6 +291,10 @@ pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pu
canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
if quit{
break;
}
}
}
@@ -265,7 +302,7 @@ 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)
let window = video_subsystem.window("Rusty Golfy", WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32)
.position_centered()
.build()
.unwrap();
@@ -275,15 +312,13 @@ pub fn main() {
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_ball_rectangle = Rect::new(
DEFAULT_START_BALL_LOCATION_X,DEFAULT_START_BALL_LOCATION_Y, DEFAULT_BALL_WIDTH as u32, DEFAULT_BALL_WIDTH as u32);
let main_ball_velocity = Point::new(0,0);
let main_entity = Entity{
shape:main_bloc_rectangle,
velocity:main_bloc_velocity
shape:main_ball_rectangle,
velocity:main_ball_velocity
};
let mut game = Game{main_bloc:main_entity,draw_debug:false};
let mut game = Game{main_ball:main_entity,draw_debug:false,ball_released:false};
main_loop(&mut canvas,&mut event_pump,&mut game);
}