Track
#func Track(k: str)Adds a key to the list of tracked keys; the key name is the browser key value, such as a. Up to 32 keys; more are ignored without a word. Works before or after Init.
import dom
import web.canvas
import web.keys
mut x := 160.0
mut score := 0
func main:
el := dom.Create("canvas")
dom.SetAttr(el, "id", "game")
dom.Append(dom.Body(), el)
c := canvas.Get("game")
keys.Init()
keys.Track("a")
keys.Track("d")
dom.EveryFrame((_now: f64): if keys.Down("ArrowLeft") or keys.Down("a"): x -= 4 end if keys.Down("ArrowRight") or keys.Down("d"): x += 4 end if keys.Pressed(" "): score++ end canvas.ClearRect(c, 0, 0, canvas.Width(c), canvas.Height(c)) canvas.FillRect(c, x - 10, 100, 20, 20) canvas.FillText(c, "score: " + score, 10, 20) end)
end