Keys, the mouse and a finger
Keys
keydown("Left") is 1 while the left arrow is held and 0 when it isn’t. That’s
what a game wants: not “a key was pressed” but “is it held right now?” Click
the screen so it hears the keys, then steer:
SCREEN 320, 240
x = 160
y = 120
DO
IF COOLDOWN(1)
IF keydown("Left") OR keydown("A") THEN x = x - 2
IF keydown("Right") OR keydown("D") THEN x = x + 2
IF keydown("Up") OR keydown("W") THEN y = y - 2
IF keydown("Down") OR keydown("S") THEN y = y + 2
MEMORYBUFFER "scene"
CIRCLE (x, y), 8, 14, 14
END MEMORYBUFFER
FRAME
CLS
PUTIMAGE (0, 0), "scene"
END FRAME
END IF
LOOP
A key is named as it’s written on it: "A", "5", "Space", "Escape",
and the arrows "Left", "Right", "Up" and "Down". A name that’s no key
is never down, so if a key seems dead, check how you spelled it.
Typing
inkey is the key that’s held, as the letter it types, so Shift + A is "A"
and A alone is "a". It’s "" when nothing is held. For a whole line, a name
or an answer, input asks, waits for Enter, and gives you what was typed.
SCREEN 320, 240
name = input("What's your name? ")
PRINT "Hi,", name
The mouse
mousex and mousey are where the pointer is, in the game’s own pixels,
however big the screen is shown. mousedown is 1 while the left button is
held.
SCREEN 320, 240
CLS
DO
IF COOLDOWN(1) AND mousedown
MEMORYBUFFER "dot", 9, 9
CIRCLE (4, 4), 4, 14, 14
END MEMORYBUFFER
FRAME
PUTIMAGE (mousex - 4, mousey - 4), "dot"
END FRAME
END IF
LOOP
There’s no CLS in that loop, so whatever you draw stays: hold the button and
scribble.
The wheel
mousewheel() is how many notches the wheel has turned since you last asked:
up is more than 0, down is less, and 0 means it hasn’t moved. Roll it to grow
the circle and shrink it again:
SCREEN 320, 240
size = 20
DO
IF COOLDOWN(1)
size = size + mousewheel() * 4
IF size < 4 THEN size = 4
IF size > 100 THEN size = 100
FRAME
CLS
CIRCLE (160, 120), size, 14, 14
LOCATE 2, 2
PRINT "size", size
END FRAME
END IF
LOOP
Asking takes the notches, so a second mousewheel() straight after the first
is 0. Ask once a frame, and keep the answer in a variable if more than one
thing wants it. mousewheelx() is the same sideways, for a wheel that tilts or
a trackpad, with right more than 0. A trackpad turns a little at a time, and
the little bits add up to whole notches.
Crash-tan’s Tip: Rolling the wheel away from you is always up, even on a Mac set to scroll the “natural” way. Up should zoom in, or move a list up, whichever way the player’s pages scroll.
A finger
On a phone or a tablet, a finger is the mouse: mousex, mousey and
mousedown follow it, so the scribbler above works there with no change.
tap is 1 while a finger (or the mouse button) is held still, and stops being
1 once it starts to drag, which tells a tap from a swipe.
Crash-tan’s Tip:
ismobileis 1 on a phone or a tablet. Use it to show buttons on the screen where a PC would use keys.
Gamepads
A gamepad works the way the keys do: joybutton(0) is 1 while button 0 is
held, and joyaxis(0) is where the left stick is pushed, from -1 to 1.
joyconnected says whether there’s one plugged in at all.
I don’t look at gamepads until a game asks me to, with GAMEPAD PHYSICAL ON.
Say it next to SCREEN, so a pad is ready by the time anyone presses a button.
SCREEN 320, 240
GAMEPAD PHYSICAL ON
DO
IF COOLDOWN(1)
FRAME
CLS
LOCATE 2, 2
IF joyconnected
PRINT "stick", fixed_str(joyaxis(0), 2)
ELSE
PRINT "No gamepad. Plug one in and press a button!"
END IF
END FRAME
END IF
LOOP