-
Notifications
You must be signed in to change notification settings - Fork 0
03 Input
For a quick demo, check out the squidgy-box example game.
There are two methods for handling keyboard input.
By default quip tracks the set of keys that are currently held and stores this information in the global state under :held-keys
.
;; An example scene update function
(defn update-level-01
[state]
(let [current (:held-keys state)]
(if (contains? current :up)
(accelerate-player state)
(decelerate-player state))))
When defining a scene configuration map you can include a vector of key-event handling functions under :key-pressed-fns
and/or :key-released-fns
.
A key-event handling function takes two arguments, the global state, and the triggered key event. It should return the new global state. It will be executed when any key is pressed, so it will likely want to check the :key
or :key-code
of the event to ensure it is relevant.
;; An example key-event (pressing control + s)
{:key :s, :key-code 83, :raw-key \s, :modifiers #{:ctrl}}
Whenever a key is pressed (or released) the global state will be threaded through the appropriate vector of handler functions ultimately returning a possibly updated global state.
;; An example key-event handler
(defn player-movement
[state e]
(if (= :space (:key e))
(player-jump state)
state))
(defn scenes
[]
{:level-01 {:key-pressed-fns [player-movement]}})
(def g (qp/game {:init-scenes-fn scenes
:current-scene :level-01}))
Mouse input works similarly to the key-event handling, you can specify :mouse-pressed-fns
and :mouse-released-fns
. Mouse-events specify the position of the mouse and which button was clicked.
;; An example mouse-event (left-clicking)
{:x 283, :y 266, :button :left}