Use timer instead of sleeping
This commit is contained in:
parent
c2cb3b4915
commit
5aa2a67c17
12
src/main.rs
12
src/main.rs
|
@ -2,7 +2,7 @@ use cat_box::{draw_text, get_keyboard_state, Game, Sprite, SpriteCollection};
|
|||
use rand::thread_rng;
|
||||
use rand::Rng;
|
||||
use sdl2::keyboard::Scancode;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
enum Direction {
|
||||
|
@ -41,6 +41,8 @@ fn main() {
|
|||
|
||||
let mut score = 0u64;
|
||||
|
||||
let mut time = Instant::now();
|
||||
|
||||
game.run(|ctx| {
|
||||
draw_text(
|
||||
ctx,
|
||||
|
@ -72,6 +74,7 @@ fn main() {
|
|||
};
|
||||
}
|
||||
|
||||
if time.elapsed().as_millis() >= 125 {
|
||||
{
|
||||
let mut last_part = snake[0].position();
|
||||
|
||||
|
@ -101,7 +104,8 @@ fn main() {
|
|||
};
|
||||
|
||||
{
|
||||
let hitted = cat_box::physics::check_for_collision_with_collection(&snake[0], &snake);
|
||||
let hitted =
|
||||
cat_box::physics::check_for_collision_with_collection(&snake[0], &snake);
|
||||
if hitted.len() > 1 {
|
||||
println!("Game over!");
|
||||
println!("Your score was: {}", score);
|
||||
|
@ -159,8 +163,8 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
// So that the snake doesn't move at super speed
|
||||
std::thread::sleep(Duration::from_millis(125));
|
||||
time = Instant::now();
|
||||
}
|
||||
apple.draw(ctx).unwrap();
|
||||
snake.draw(ctx).unwrap();
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue