Skip to content

Commit

Permalink
Make snake move
Browse files Browse the repository at this point in the history
So far it moves _only_ when keys are pressed
  • Loading branch information
Ablesius committed Apr 2, 2024
1 parent 2661ffc commit c058c83
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@ fn spawn_snake(mut commands: Commands) {
.insert(SnakeHead);
}

fn move_snake(mut head_positions: Query<(&SnakeHead, &mut Transform)>) {
for (_head, mut transform) in head_positions.iter_mut() {
// for now just move the head 2 upwards
// as we don't have user input yet
transform.translation.y += 2.0;
fn move_snake(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut head_positions: Query<&mut Transform, With<SnakeHead>>,
) {
for mut transform in head_positions.iter_mut() {
if keyboard_input.pressed(KeyCode::ArrowLeft) {
transform.translation.x -= 2.;
}
if keyboard_input.pressed(KeyCode::ArrowRight) {
transform.translation.x += 2.;
}
if keyboard_input.pressed(KeyCode::ArrowDown) {
transform.translation.y -= 2.;
}
if keyboard_input.pressed(KeyCode::ArrowUp) {
transform.translation.y += 2.;
}
}
}

0 comments on commit c058c83

Please sign in to comment.