rust-raylib-playground/bounce.rs
2024-02-18 12:08:29 +01:00

43 lines
1.0 KiB
Rust

mod raylib;
use raylib::*;
fn main() {
let screen: Vector2<u32> = Vector2 { x: 800, y: 600 };
let ball_radius: f32 = 100.0;
let mut pos: Vector2<f32> = Vector2 {
x: screen.x as f32 / 2.0,
y: screen.y as f32 / 2.0,
};
let mut velocity: Vector2<f32> = Vector2 { x: 200.0, y: 200.0 };
init_window(screen.x, screen.y, "Bouncing ball");
set_target_fps(60);
while !window_should_close() {
begin_drawing();
clear_background(colors::BLACK);
let dt: f32 = get_frame_time();
velocity.y += dt * 1000.0;
let x: f32 = pos.x + velocity.x * dt;
if x - ball_radius < 0.0 || x + ball_radius >= screen.x as f32{
velocity.x *= -1.0;
} else {
pos.x = x;
}
let y: f32 = pos.y + velocity.y * dt;
if y - ball_radius < 0.0 || y + ball_radius >= screen.y as f32 {
velocity.y *= -1.0;
} else {
pos.y = y;
}
draw_circle_v(pos, ball_radius, colors::RED);
end_drawing();
}
}