Added a moving cube with drifty controls and borders

This commit is contained in:
2022-05-10 11:19:41 +02:00
commit 27a01e4891
4 changed files with 277 additions and 0 deletions
+204
View File
@@ -0,0 +1,204 @@
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;
use sdl2::rect::Point;
use sdl2::rect::Rect;
const WINDOW_HEIGHT:i32 = 600;
const WINDOW_WIDTH:i32 = 800;
const BLOC_SPEED:f32 = 3.0;
const MAX_SPEED:f32 = 20.0;
pub struct Game{
main_bloc:Entity,
}
impl Game{
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){
ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64));
ctx.clear();
//Rendering main_bloc
ctx.set_draw_color(Color::RGB(0x00, 0x00, 0x00));
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 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;
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 + 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 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 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);
}
}
pub struct Entity{
shape:Rect,
velocity:Point,
}
pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pump:&mut sdl2::EventPump,game:&mut Game){
'running: loop {
canvas.clear();
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), .. } => {
//pressed up
game.up();
},
Event::KeyDown { keycode: Some(Keycode::Up), .. } => {
//pressed up
game.up();
},
Event::KeyDown { keycode: Some(Keycode::S), .. } => {
//pressed up
game.down();
},
Event::KeyDown { keycode: Some(Keycode::Down), .. } => {
//pressed up
game.down();
},
Event::KeyDown { keycode: Some(Keycode::A), .. } => {
//pressed up
game.left();
},
Event::KeyDown { keycode: Some(Keycode::Left), .. } => {
//pressed up
game.left();
},
Event::KeyDown { keycode: Some(Keycode::D), .. } => {
//pressed up
game.right();
},
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);
}