Playing with Keys ⌨️¶
Steer, jump and play two players with the keyboard
Click, Then Press 👆¶
Your code and your drawing share one keyboard. Click the drawing first: the ⌨️ jumps beside it, and now your keys move things instead of typing. Click your code to type again.
Holding a Key Down ⬅️➡️¶
if key rightarrow is down is true for as long as you hold the right arrow. Check it again and again in a forever, and the car drives while you hold the key:
Canvas road having width 400, length 300, fill "lightgray"
draw rectangle car at 180, 130 having width 40, length 25, fill "tomato"
forever {
if key leftarrow is down { move car by -4, 0 }
if key rightarrow is down { move car by 4, 0 }
wait 0.02 seconds
}
Pressing a Key Once 🦘¶
when key space is pressed runs each time you press space, just like when ... is clicked runs each time you click:
Canvas park having width 400, length 300, fill "skyblue"
draw circle frog at 200, 250 having radius 20, fill "green"
when key space is pressed {
move frog by 0, -100 in 0.3 seconds
move frog by 0, 100 in 0.3 seconds
}
Holding space down doesn't make the frog jump again. Let go and press it once more.
Two Players 🏓¶
Two people can hold keys at the same time. Give each paddle its own keys with a recipe's settings:
Canvas table having width 400, length 300, fill "darkgreen"
shape paddle having up key w, down key s {
rectangle bat having width 10, length 60, fill "white"
forever {
if up is down { move bat by 0, -4 }
if down is down { move bat by 0, 4 }
wait 0.02 seconds
}
}
draw paddle left at 20, 120
draw paddle right at 370, 120 having up key uparrow, down key downarrow
key w is the W key. up is the paddle's setting that holds a key, so it's used without key.
Watch Out 👀¶
- Click the drawing before pressing keys, or they'll type into your code.
ifgoes withis down, andwhengoes withis pressed.- The arrows are
leftarrow,rightarrow,uparrowanddownarrow, and the space bar isspace.
Challenge Time! 🏆¶
- 🌟 Challenge 1: Make the car drive up and down too.
- 🌟 Challenge 2: Count the frog's jumps and show them:
"Jumps: " + jumps. - 🌟 Challenge 3: Give the paddles a ball to hit.
What's Next? 🚀¶
Your drawings can move, listen for clicks and read the keyboard. That's everything you need to make a game! 🎮