Because coding is fun!

Example "drawing sprites & collision" in Rust

Create a folder called "resources" (in the same directory as your code) and place the two images (provided bellow) there.

hero_shipufo1


Here is the Rust code…


extern crate raylib;
use raylib::prelude::*;
use raylib::prelude::KeyboardKey::*;
use raylib::ffi::CheckCollisionRecs;

fn main() {
let screen_width = 800.0;
let screen_height = 600.0;

let (mut rl, thread) = raylib::init()
.size(screen_width as i32, screen_height as i32)
.title("Simple shooter")
.build();

//hero ship
let speed = 4.0;
let ship_size_width = 50;
let ship_size_height = 75;

let mut ship_position = Vector2 {
x: screen_width / 2.0,
y: screen_height - ship_size_height as f32,
};

let mut ship_rectangle = Rectangle {
width: ship_size_width as f32,
height: ship_size_height as f32,
x: ship_position.x,
y: ship_position.y,
};

// graphics load hero ship
let mut image_hero_ship = Image::load_image("resources/hero_ship.png");

let i=image_hero_ship.as_mut().unwrap();
Image::image_resize(i, 50, 75);

let mut hero_ship = rl.load_texture_from_image(&thread, i);
let hs=hero_ship.as_mut().unwrap();

//bad dude ship
let baddude_speed = 4.0;
let baddude_ship_size_width = 75;
let baddude_ship_size_height = 50;

let mut baddude_ship_position = Vector2 {
x: 0.0,
y: 20.0 + baddude_ship_size_height as f32,
};

let mut baddude_ship_rectangle = Rectangle {
width: baddude_ship_size_width as f32,
height: baddude_ship_size_height as f32,
x: baddude_ship_position.x,
y: baddude_ship_position.y,
};

// graphics load bad dude ship
let mut image_baddude_ship = Image::load_image("resources/ufo1.png");

let bi=image_hero_ship.as_mut().unwrap();
Image::image_resize(bi, baddude_ship_size_width, baddude_ship_size_height);

let mut baddude_ship = rl.load_texture_from_image(&thread, bi);
let bs=baddude_ship.as_mut().unwrap();


rl.set_target_fps(60); // Set our game to run at 60 frames-per-second

while !rl.window_should_close() {
// beging drawing operations
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::BLACK);

// control the hero ship
if d.is_key_down(KEY_RIGHT)
&& ship_position.x < screen_width as f32 - ship_size_width as f32
{
ship_position.x += speed;
}
if d.is_key_down(KEY_LEFT) && ship_position.x > 0.0 {
ship_position.x -= speed;
}

if d.is_key_down(KEY_UP) && ship_position.y > 30.0 {
ship_position.y -= speed;
}
if d.is_key_down(KEY_DOWN)
&& ship_position.y < screen_height as f32 - ship_size_height as f32
{
ship_position.y += speed;
}

// lets make the bad dude move
if baddude_speed as i32 > 0
&& baddude_ship_position.x as i32 + (baddude_speed as i32) < screen_width as i32
{
baddude_ship_position.x += baddude_speed
} else {
baddude_ship_position.x = 0.0 - baddude_ship_size_width as f32;
}

// draw the baddude ship
d.draw_texture(
&bs,
baddude_ship_position.x as i32,
baddude_ship_position.y as i32,
Color::LIGHTGRAY,
);

// keep the collision rectangle up to date on baddude ship
baddude_ship_rectangle.x = baddude_ship_position.x;
baddude_ship_rectangle.y = baddude_ship_position.y;

// draw the hero ship
d.draw_texture(
&hs,
ship_position.x as i32,
ship_position.y as i32,
Color::LIGHTGRAY,
);

// keep the collision rectangle up to date on hero ship
ship_rectangle.x = ship_position.x;
ship_rectangle.y = ship_position.y;

// any collisions????
if my_check_collision_recs(ship_rectangle, baddude_ship_rectangle) {
// BOOOOM
d.draw_text("BOOM", 500, 10, 20, Color::LIGHTGRAY);
}

//scores...
d.draw_text("0", 10, 10, 20, Color::LIGHTGRAY);

}
}



pub fn my_check_collision_recs(rec1: Rectangle, rec2: Rectangle) -> bool {
if rec1.x < rec2.x + rec2.width &&
rec1.x + rec1.width > rec2.x &&
rec1.y < rec2.y + rec2.height &&
rec1.y + rec1.height > rec2.y
{
return true;
}
return false;
}