Finish snake movement
This commit is contained in:
parent
4473b0330b
commit
ec73756b2c
34
src/main.rs
34
src/main.rs
|
@ -28,14 +28,27 @@ fn main() {
|
||||||
for key in keys {
|
for key in keys {
|
||||||
match key {
|
match key {
|
||||||
Scancode::Q => game.terminate(),
|
Scancode::Q => game.terminate(),
|
||||||
Scancode::W => dir = Direction::Up,
|
Scancode::W | Scancode::Up => dir = Direction::Up,
|
||||||
Scancode::A => dir = Direction::Left,
|
Scancode::A | Scancode::Left => dir = Direction::Left,
|
||||||
Scancode::S => dir = Direction::Down,
|
Scancode::S | Scancode::Down => dir = Direction::Down,
|
||||||
Scancode::D => dir = Direction::Right,
|
Scancode::D | Scancode::Right => dir = Direction::Right,
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut last_part = snake[0].position();
|
||||||
|
|
||||||
|
for s in snake.iter().skip(1) {
|
||||||
|
let (lastx, lasty) = last_part;
|
||||||
|
let (x, y) = s.position();
|
||||||
|
let (xdiff, ydiff) = (lastx - x, y - lasty);
|
||||||
|
s.translate((xdiff, ydiff));
|
||||||
|
last_part = s.position();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The snake head needs to be moved after the body or else the body will just collapse into the head
|
||||||
match dir {
|
match dir {
|
||||||
Direction::Up => {
|
Direction::Up => {
|
||||||
snake[0].translate((0, 37));
|
snake[0].translate((0, 37));
|
||||||
|
@ -51,18 +64,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
{
|
// So that the snake doesn't move at super speed
|
||||||
let mut last_part = snake[0].position();
|
|
||||||
|
|
||||||
for s in &mut *snake {
|
|
||||||
let (lastx, lasty) = last_part;
|
|
||||||
let (x, y) = s.position();
|
|
||||||
let (xdiff, ydiff) = (lastx - x, y - lasty);
|
|
||||||
s.translate((xdiff, ydiff));
|
|
||||||
last_part = s.position();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::thread::sleep(Duration::from_millis(125));
|
std::thread::sleep(Duration::from_millis(125));
|
||||||
snake.draw(ctx).unwrap();
|
snake.draw(ctx).unwrap();
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue