In this challenge we’re going to use the keyboard as steering wheel for the turtle:
Pressing (and holding) a key will trigger execution of different commands. In other words: If a particular key is pressed, one or more commands are executed.
Follow the steps below to create the steering wheel app:
- Greet the user and tell her to press Enter to start.
- Create a loop that runs forever. Inside the loop move the turtle 10 steps forward and sleep for 25 milliseconds.
-
Before those two commands, but still inside the loop, comes the logic to steer the turtle. We want to execute commands based on whether a key is pressed or not. To check if a particular key is pressed, you can use
Game.IsKeyDown(KeyboardKey.A)
. This command - in this case it’s more like a question - returnsbool
, i.e.true
orfalse
which means we can use it directly as condition of anif
statement.if (Game.IsKeyDown(KeyboardKey.A)) { // Insert commands that only run when 'A' is currently pressed. }
- Rotate the turtle 5 degrees counter-clockwise if A is pressed.
-
Rotate the turtle 5 degrees clockwise if D is pressed.
Think about what happens when the user presses both A and D. Are both rotations executed or only one of them? What would you need to change to switch behavior?
-
You now have the most basic version of the program. Think of some extensions that you can add or use the following list as inspiration:
- Draw when Space is pressed, stop drawing when Space is released.
-
Change the pen color when W is pressed.
Start with red and execute
Turtle.ShiftPenColor(1);
every timeW is pressed. - Slow down the turtle when S is pressed. For example move 10 steps forward by default, but only 5 if S is pressed.
- Slow the turtle even more down when both S and J are pressed.