This is an example program that uses the keyboard’s input to control a player on the screen. This works best when using a Bluetooth keyboard. There’s probably a better way of executing this, but for now, you have to hit the keys repetitively. Here’s the code:
function setup()
print("Use W, A, S, and D to move and X to open the chest.")
showKeyboard()
player=sprite("Planet Cute:Character Boy")
chest=sprite("Planet Cute:Chest Closed")
px=100
py=HEIGHT-100
points=0
parameter.watch("points")
chestClosed=true
end
function draw()
background(95, 70, 31, 255)
if chestClosed then chest=sprite("Planet Cute:Chest Closed", WIDTH/2, HEIGHT/2) end
player=sprite("Planet Cute:Character Boy", px, py)
showKeyboard()
end
function keyboard(key)
if (key == "w" or key == "W") then
py = py + 10
end
if (key == "a" or key == "A") then
px = px - 10
end
if (key == "s" or key == "S") then
py = py - 10
end
if (key == "d" or key == "D") then
px = px + 10
end
if (key == "x" or key == "X") then
if touchingChest() then
chestClosed=false
sound(SOUND_PICKUP, 24870)
points = points + 10
end
end
end
function touchingChest()
if (px < WIDTH/2+50 and
px > WIDTH/2-50 and
py < HEIGHT/2+85 and
py > HEIGHT/2-85) then return true
else
return false
end
end