implement bounce.rs
This commit is contained in:
parent
372d3d1137
commit
e3c86e0e37
8
Makefile
8
Makefile
@ -1,2 +1,10 @@
|
||||
.PHONY: all
|
||||
|
||||
all: rect bounce
|
||||
|
||||
rect: rect.rs
|
||||
rustc -L./raylib/ -lraylib $< -o bin/$@
|
||||
|
||||
bounce: bounce.rs
|
||||
rustc -L./raylib/ -lraylib $< -o bin/$@
|
||||
|
||||
|
42
bounce.rs
Normal file
42
bounce.rs
Normal file
@ -0,0 +1,42 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
19
raylib.rs
19
raylib.rs
@ -11,8 +11,12 @@ extern {
|
||||
fn SetTargetFPS(fps: u32);
|
||||
fn BeginDrawing();
|
||||
fn EndDrawing();
|
||||
|
||||
fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color);
|
||||
fn DrawCircleV(center: Vector2<f32>, radius: f32, color: Color);
|
||||
|
||||
fn IsKeyDown(key: u32) -> bool;
|
||||
fn GetFrameTime() -> f32;
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
@ -29,6 +33,13 @@ pub mod colors {
|
||||
pub const BLACK: Color = Color(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Vector2<S> {
|
||||
pub x: S,
|
||||
pub y: S,
|
||||
}
|
||||
|
||||
pub fn init_window(width: u32, height: u32, title: &str) {
|
||||
unsafe {
|
||||
let title = CString::new(title).unwrap();
|
||||
@ -60,7 +71,15 @@ pub fn draw_rect(pos_x: u32, pos_y: u32, width:u32, height: u32, color: Color) {
|
||||
unsafe { DrawRectangle(pos_x, pos_y, width, height, color); }
|
||||
}
|
||||
|
||||
pub fn draw_circle_v(center: Vector2<f32>, radius: f32, color: Color) {
|
||||
unsafe { DrawCircleV(center, radius, color) }
|
||||
}
|
||||
|
||||
pub fn is_key_down(key: Key) -> bool {
|
||||
unsafe { IsKeyDown(key as u32) }
|
||||
}
|
||||
|
||||
pub fn get_frame_time() -> f32 {
|
||||
unsafe { GetFrameTime() }
|
||||
}
|
||||
|
||||
|
9
rect.rs
9
rect.rs
@ -1,13 +1,8 @@
|
||||
mod raylib;
|
||||
|
||||
pub struct Vector2<S> {
|
||||
pub x: S,
|
||||
pub y: S,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let screen = Vector2::<u32> { x: 800, y: 600 };
|
||||
let mut pos = Vector2::<u32> {
|
||||
let screen = raylib::Vector2::<u32> { x: 800, y: 600 };
|
||||
let mut pos = raylib::Vector2::<u32> {
|
||||
x: screen.x / 2,
|
||||
y: screen.y / 2
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user