Skip to content

03 Input

Dave Kimber edited this page May 27, 2023 · 4 revisions

For a quick demo, check out the squidgy-box example game.

Keyboard Input

There are two methods for handling keyboard input.

Held Keys

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))))

Key Events

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 (quip/game {:init-scenes-fn scenes
                 :current-scene :level-01}))

Mouse Input

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}