Controlling the snake

This commit is contained in:
Timothy Warren 2020-12-24 13:49:23 -05:00
parent e860af1d7a
commit a62eb40527

View File

@ -22,11 +22,25 @@ fn spawn_snake(commands: &mut Commands, materials: Res<Materials>) {
.with(SnakeHead);
}
fn snake_movement(mut head_positions: Query<(&SnakeHead, &mut Transform)>) {
for (_head, mut transform) in head_positions.iter_mut() {
fn snake_movement(
keyboard_input: Res<Input<KeyCode>>,
mut head_positions: Query<&mut Transform, With<SnakeHead>>,
) {
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.;
}
}
}
fn main() {
App::build()