Handling User Input
enums and match
Yeah, it's kind of hard to close the window. This is because we have not told our program how to deal with events, so it continues to run and we have to force it to quit. That's uncomfortable, let's change that.
Go to the sdl2
documentation. Go to the event
module, find the Event
enum
. An enum
in Rust is a type that represents data that is one of several possible variants. Each variant in the enum
can optionally have data associated with it. Look at the possible event
variants in the declaration of Event
. We want to end the program by pushing esc
. What variant are we looking for?
In our game loop, we iterate over events
. Depending on what kind of event
is happening, we want the program to react in different ways. Pushing space
means something different than pushing esc
. We will match
the event
to the variants of the enum Event
. match
is a way to control the flow of the program when there are several possible options, similar to if
else
statements.
- To be able to use the enum and work with keyboard input, we need to add the following dependencies:
#![allow(unused_variables)] fn main() { use sdl2::event::Event; use sdl2::keyboard::Keycode; }
- Enter the following lines into the
for
loop.match
compares the value of theevent
-variable to the variants of the enumEvent
. If the value of theevent
-variable is a pressedesc
-key, the'game
-loop breaks. If the value is something else, the loop continues. The last part of amatch
always needs to be_ =>
, the case that covers all cases that are not explicitly defined.
#![allow(unused_variables)] fn main() { match event { Event::KeyDown { keycode: Some(Keycode::Escape), .. } => break 'game, _ => continue 'game, } }
- Run the program, try if the user input you implemented works!
- To be able to close the window with a mouse click, change the first line of the
match
to includeEvent::Quit { .. }
. The'game
-loop breaks now if either theesc
-key is pressed, or the quit-button is clicked with the cursor.
#![allow(unused_variables)] fn main() { match event { Event::Quit { .. } | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => break 'game, _ => continue 'game, } }