This commit is contained in:
gallant 2023-01-25 07:41:12 -06:00
parent 2f67f84388
commit 176a48c909

View file

@ -4,7 +4,6 @@ use rand::Rng;
use sdl2::keyboard::Scancode;
use std::time::Instant;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Direction {
Up,
@ -16,7 +15,7 @@ enum Direction {
macro_rules! set_if_not_opp {
($i:ident, $e:expr, $opp:expr, $v:expr) => {
if $i != $opp {
$v.push(&$e);
$v.push($e);
$i = $e;
}
};
@ -82,7 +81,8 @@ fn main() {
//DEFAULT DIRECTION
let mut dir = Direction::Left;
let mut queue: Vec<&Direction> = Vec::with_capacity(3);
let mut dir2 = Direction::Left;
let mut queue: Vec<Direction> = Vec::new();
let mut score = 0u64;
let mut now = Instant::now();
@ -106,6 +106,7 @@ fn main() {
}
if now.elapsed().as_millis() >= 125 {
dir2 = dir.clone();
{
let mut last_part = snake[0].position();
@ -118,27 +119,22 @@ fn main() {
}
}
// The snake head needs to be moved after the body or else the body will just collapse into the head
match *queue[0] {
Direction::Up => {
snake[0].translate((0, 37));
}
Direction::Left => {
snake[0].translate((-37, 0));
}
Direction::Down => {
snake[0].translate((0, -37));
}
Direction::Right => {
snake[0].translate((37, 0));
}
};
let _ = &mut queue.remove(0);
match dir2 {
Direction::Up => {
snake[0].translate((0, 37));
}
Direction::Left => {
snake[0].translate((-37, 0));
}
Direction::Down => {
snake[0].translate((0, -37));
}
Direction::Right => {
snake[0].translate((37, 0));
}
};
//queue.remove(0);
{
let hitted_self =
@ -177,10 +173,9 @@ fn main() {
score += 1;
}
now = Instant::now();
}
queue.push(&dir);
// So that the snake doesn't move at super speed
//std::thread::sleep(Duration::from_millis(125));