forked from karx/snake
darn it
This commit is contained in:
parent
2f67f84388
commit
176a48c909
17
src/main.rs
17
src/main.rs
|
@ -4,7 +4,6 @@ use rand::Rng;
|
||||||
use sdl2::keyboard::Scancode;
|
use sdl2::keyboard::Scancode;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||||
enum Direction {
|
enum Direction {
|
||||||
Up,
|
Up,
|
||||||
|
@ -16,7 +15,7 @@ enum Direction {
|
||||||
macro_rules! set_if_not_opp {
|
macro_rules! set_if_not_opp {
|
||||||
($i:ident, $e:expr, $opp:expr, $v:expr) => {
|
($i:ident, $e:expr, $opp:expr, $v:expr) => {
|
||||||
if $i != $opp {
|
if $i != $opp {
|
||||||
$v.push(&$e);
|
$v.push($e);
|
||||||
$i = $e;
|
$i = $e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -82,7 +81,8 @@ fn main() {
|
||||||
|
|
||||||
//DEFAULT DIRECTION
|
//DEFAULT DIRECTION
|
||||||
let mut dir = Direction::Left;
|
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 score = 0u64;
|
||||||
let mut now = Instant::now();
|
let mut now = Instant::now();
|
||||||
|
|
||||||
|
@ -106,6 +106,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if now.elapsed().as_millis() >= 125 {
|
if now.elapsed().as_millis() >= 125 {
|
||||||
|
dir2 = dir.clone();
|
||||||
{
|
{
|
||||||
let mut last_part = snake[0].position();
|
let mut last_part = snake[0].position();
|
||||||
|
|
||||||
|
@ -119,26 +120,21 @@ fn main() {
|
||||||
}
|
}
|
||||||
// The snake head needs to be moved after the body or else the body will just collapse into the head
|
// The snake head needs to be moved after the body or else the body will just collapse into the head
|
||||||
|
|
||||||
match *queue[0] {
|
match dir2 {
|
||||||
Direction::Up => {
|
Direction::Up => {
|
||||||
snake[0].translate((0, 37));
|
snake[0].translate((0, 37));
|
||||||
|
|
||||||
}
|
}
|
||||||
Direction::Left => {
|
Direction::Left => {
|
||||||
snake[0].translate((-37, 0));
|
snake[0].translate((-37, 0));
|
||||||
|
|
||||||
}
|
}
|
||||||
Direction::Down => {
|
Direction::Down => {
|
||||||
snake[0].translate((0, -37));
|
snake[0].translate((0, -37));
|
||||||
|
|
||||||
}
|
}
|
||||||
Direction::Right => {
|
Direction::Right => {
|
||||||
snake[0].translate((37, 0));
|
snake[0].translate((37, 0));
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let _ = &mut queue.remove(0);
|
//queue.remove(0);
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
let hitted_self =
|
let hitted_self =
|
||||||
|
@ -180,7 +176,6 @@ fn main() {
|
||||||
|
|
||||||
now = Instant::now();
|
now = Instant::now();
|
||||||
}
|
}
|
||||||
queue.push(&dir);
|
|
||||||
|
|
||||||
// So that the snake doesn't move at super speed
|
// So that the snake doesn't move at super speed
|
||||||
//std::thread::sleep(Duration::from_millis(125));
|
//std::thread::sleep(Duration::from_millis(125));
|
||||||
|
|
Loading…
Reference in a new issue