Skip to content

01 Creating A Game

Kimbsy edited this page May 20, 2023 · 3 revisions

The quip.core/game function takes an options map and returns a quip game configuration which can be transformed into a Quil sketch and executed using quip.core/run.

;; The simplest "game"
(def g (quip.core/game {}))

;; It's not exciting, but it does run
(quip.core/run g)

Config

There are some optional configuration fields you can specify in your game's options map:

(quip.core/game
 {:title       "title"   ;; the title of the game window
  :size        [600 400] ;; [x y] dimensions of the game in pixels
  :setup       #()       ;; function returning the initial state of the game
  :on-close    #()       ;; function called when the game closes
  :frame-rate  60        ;; target frame rate for the game
  })

Scenes

In order to do anything useful, your game's options map should also include at least an :init-scenes-fn and a :current-scene.

The :init-scenes-fn should be a function which returns a map of scene-key => scene-configuration where each scene-configuration should be a map representing the state of that scene. Primarily, a scene-configuration should contain a :update-fn, and a :draw-fn. The :current-scene should be a scene-key.

The :update-fn of the current scene is called every frame. It takes the game state from the previous frame and returns the new game state.

The :draw-fn of the current scene is called every frame. It takes the updated game state and must draw the current scene.

(ns quip.foo
  (:require [quip.core :as qp]
            [quip.utils :as qpu]))

(defn update-level-01
  [state]
  ;; always return the state, even if we're not doing anything
  state)

(defn draw-level-01
  [state]
  ;; fill the screen with an RGB colour
  (qpu/background [0 43 54]))

(defn scenes
  []
  {:level-01 {:update-fn update-level-01
              :draw-fn draw-level-01}})

(def g (qp/game {:init-scenes-fn scenes
                 :current-scene :level-01}))

(qp/run g)

State

The state that is passed into a scene's update and draw functions can contain anything you like. It's the primary place to store information about the game as it runs and is available in all scene update functions, input handling functions, collision handling functions, tween on-complete functions etc. By default it will contain a map of the scenes in the game, the set of currently held keyboard keys, and some utility fields.

You can enrich the initial state of the game by supplying a :setup function in your game's options map. This function should return a map which will be merged over the default game state map.

(ns quip.foo
  (:require [quip.core :as qp]
            [quip.utils :as qpu]))

(defn update-level-01 [state] state)

(defn draw-level-01 [state] (qpu/background [0 43 54]))

(defn scenes []
  {:level-01 {:update-fn update-level-01
              :draw-fn draw-level-01}})

(defn setup
  []
  ;; custom initial state
  {:highscore 0
   :player-health 100
   :unlocked-acheivements []
   :foo "bar"})

(def g (qp/game {:init-scenes-fn scenes
                 :current-scene :level-01
                 :setup setup}))

(qp/run g)