Created
August 8, 2019 15:40
-
-
Save latant/0f4e230e47b4065737e29ff1f05e8748 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clojure-play.core | |
(:require [quil.core :as q :include-macros true] | |
[quil.middleware :as m])) | |
(def world-size 16) | |
(def cell-padding-ratio 0.1) | |
(def win-size 500) | |
(def cell-radius-ratio 0.1) | |
(def update-millis 100) | |
(def cstep (/ win-size world-size)) | |
(def csize (* cstep (- 1 cell-padding-ratio))) | |
(def cpad (* cell-padding-ratio cstep)) | |
(def hcpad (/ cpad 2)) | |
(def cradius (* csize cell-radius-ratio)) | |
(defn setup [] | |
(q/frame-rate 100) | |
(q/background 100) | |
{:cells (vec (repeat world-size (vec (repeat world-size false)))) | |
:position [(quot world-size 2) (quot world-size 2)] | |
:direction [0 1] | |
:last-update 0}) | |
(defmacro assoc2 [coll i j v] | |
`(let [coll# ~coll i# ~i j# ~j] | |
(assoc coll# i# (assoc (coll# i#) j# ~v)))) | |
(defn draw-state [{cells :cells [x y] :position}] | |
(q/no-stroke) | |
(q/background 0) | |
(q/no-stroke) | |
(doseq [i (range 0 world-size) | |
j (range 0 world-size)] | |
(q/fill (if ((cells i) j) 200 100)) | |
(q/rect (+ hcpad (* i cstep)) | |
(+ hcpad (* j cstep)) | |
csize csize | |
cradius)) | |
(q/fill 150 50 50) | |
(q/rect (+ hcpad (* x cstep)) | |
(+ hcpad (* y cstep)) | |
csize csize | |
cradius)) | |
(defn update-state [state] | |
(if (< (- (q/millis) (:last-update state)) update-millis) | |
state | |
(let [cells (:cells state) | |
[x y] (:position state) | |
[i j] (:direction state) | |
cell-alive? ((cells x) y) | |
[ni nj] (if cell-alive? [(- j) i] [j (- i)])] | |
{:cells (assoc2 cells x y (not cell-alive?)) | |
:position [(mod (+ x ni) world-size) | |
(mod (+ y nj) world-size)] | |
:direction [ni nj] | |
:last-update (q/millis)}))) | |
(q/defsketch langtons-ant | |
:host "host" | |
:size [win-size win-size] | |
:setup setup | |
:update update-state | |
:draw draw-state | |
:middleware [m/fun-mode]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment