2014-01-12 02:57:03 -05:00
2014-01-12 02:57:03 -05:00
2013-12-29 02:37:31 -05:00
2013-12-31 22:44:34 -05:00
2014-01-11 14:15:58 -05:00
2013-12-28 20:22:37 -05:00

Introduction

A Clojure library that provides a wrapper for LibGDX, allowing you to write Clojure games that run on desktop OSes (Windows, OS X, and Linux) and mobile OSes (Android and iOS) with the same codebase.

Justification

The best thing about making a game in Clojure is that you can modify it in a REPL while it's running. By simply reloading a namespace, your code will be injected into the game, uninhibited by the restrictions posed by tools like HotSwap. Additionally, a REPL lets you read and modify the state of your game at runtime, so you can quickly experiment and diagnose problems.

Clojure also brings the benefits of functional programming. This is becoming a big topic of discussion in gamedev circles, including by John Carmack. Part of this is due to the prevalence of multi-core hardware, making concurrency more important. Additionally, there is a general difficulty of maintaining object-oriented game codebases as they grow, due to complicated class hierarchies and state mutations.

Installation

The recommended way to start using play-clj is to create a Clojure game project with Nightcode. It will automatically generate three separate Leiningen projects for desktop, Android, and iOS, all pointing to the same source code and resources directories. You can build the projects with Nightcode itself or with Leiningen on the command line (if you choose the latter, you will need to set up lein-droid and lein-fruit to build the Android and iOS projects respectively).

Documentation

There are currently no tutorials or generated docs, because play-clj is changing rapidly. This will be resolved in the near future. For now, consider this commented example:

(ns game-test.core
  (:require [play-clj.core :refer :all]))

; define a screen, where all the action takes place
(defscreen main-screen
  ; all the screen functions get a map called "screen" containing various
  ; important values, and a list called "entities" for storing game objects
  
  ; the entities list is immutable, so in order to update it you must simply
  ; return a new list at the end of each screen function
  
  ; this function runs only once, when the screen is first shown
  :on-show
  (fn [screen entities]
    ; update the screen map to hold a tiled map renderer and a camera
    (update! screen
             :renderer (orthogonal-tiled-map "level1.tmx" (/ 1 8))
             :camera (orthographic-camera))
    (let [; load a sprite sheet from your resources dir
          sheet (texture "tiles.png")
          ; split the sheet into 16x16 tiles
          ; (the "texture!" function lets you call TextureRegion methods directly)
          tiles (texture! sheet :split 16 16)
          ; get the tile at row 6, col 0
          player-image (texture (aget tiles 6 0))
          ; add position and size to the player-image map so it can be drawn
          player-image (assoc player-image :x 0 :y 0 :width 2 :height 2)]
      ; return a new entities list with player-image inside of it
      (conj entities player-image)))
  
  ; this function runs every time a frame must be drawn (about 60 times per sec)
  :on-render
  (fn [screen entities]
    ; make the screen completely black
    (clear!)
    ; render the tiled map
    (render! screen)
    ; draw the entities and return them
    (draw! screen entities))
  
  ; this function runs when the screen dimensions change
  :on-resize
  (fn [screen entities]
    ; make the camera 20 tiles high, and adjust the width appropriately
    (let [height 20
          width (* height (/ (game :width) (game :height)))]
      (resize-camera! screen width height))
    ; return the entities list unmodified
    entities))

; define the game itself, and immediately hand off to the screen
(defgame game-test
  :on-create
  (fn [this]
    (set-screen! this main-screen)))

Licensing

All source files that originate from this project are dedicated to the public domain. I would love pull requests, and will assume that they are also dedicated to the public domain.

Description
No description provided
Readme 33 MiB
Languages
Clojure 95.8%
CSS 2.2%
Java 1.8%
JavaScript 0.2%