From a62eb4052772270be29f379679522164ca331abc Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 24 Dec 2020 13:49:23 -0500 Subject: [PATCH] Controlling the snake --- src/main.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index d30ee8e..07ac9f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,9 +22,23 @@ fn spawn_snake(commands: &mut Commands, materials: Res) { .with(SnakeHead); } -fn snake_movement(mut head_positions: Query<(&SnakeHead, &mut Transform)>) { - for (_head, mut transform) in head_positions.iter_mut() { - transform.translation.y += 2.; +fn snake_movement( + keyboard_input: Res>, + mut head_positions: Query<&mut Transform, With>, +) { + for mut transform in head_positions.iter_mut() { + if keyboard_input.pressed(KeyCode::Left) { + transform.translation.x -= 2.; + } + if keyboard_input.pressed(KeyCode::Right) { + transform.translation.x += 2.; + } + if keyboard_input.pressed(KeyCode::Down) { + transform.translation.y -= 2.; + } + if keyboard_input.pressed(KeyCode::Up) { + transform.translation.y += 2.; + } } }