Skip to content

Instantly share code, notes, and snippets.

@meganehouser
Created December 30, 2014 14:10
Show Gist options
  • Save meganehouser/0507cbb86075878a5ed4 to your computer and use it in GitHub Desktop.
Save meganehouser/0507cbb86075878a5ed4 to your computer and use it in GitHub Desktop.
Quil code for making a test movie
(ns twinkle.core
(:require [quil.core :as q])
(:require [quil.middleware :as m]))
(def sky-size {:w 640 :h 350})
(def star-size {:w 30 :h 30})
(def min-radius 10)
(def max-radius 20)
;-- point ----------------------------
(defrecord Point [x y radius radian direction])
(defn rand-point [start-x start-y]
(let [x (+ start-x (rand-int (:w star-size))), y (+ start-y (rand-int (:h star-size)))
radius (+ (rand-int (- max-radius min-radius)) min-radius)
radian (rand q/TWO-PI)
direction (if (= 1 (rand-int 2)) :posi :nega)]
(->Point x y radius radian direction)))
(defn get-xy [point]
(let [x (+ (* (q/cos (:radian point)) (:radius point)) (:x point))
y (+ (* (q/sin (:radian point)) (:radius point)) (:y point))]
[x y]))
(defn update-radian [point]
(let [radian (:radian point)
direction (:direction point)
radian-step (* 2 (/ q/TWO-PI 360))]
(assoc-in point [:radian]
(if (= :posi direction)
(- radian radian-step)
(+ radian radian-step)))))
;-- star ----------------------------
(defrecord Star [p1 p2 p3 color])
(defn new-star [p1 p2 p3 c] (->Star p1 p2 p3 c))
(defn rand-star-pair []
(let [start-x (rand-int (:w sky-size)), start-y (rand-int (:h sky-size))
p1 (rand-point start-x start-y)
p2 (rand-point start-x start-y)
p3 (rand-point start-x start-y)
p4 (rand-point start-x start-y)
p5 (rand-point start-x start-y)
p6 (rand-point start-x start-y)]
[(new-star p1 p2 p3 :c1) (new-star p4 p5 p6 :c2)]))
(defn update-star [star]
(-> star
(update-in [:p1] #(update-radian %))
(update-in [:p2] #(update-radian %))
(update-in [:p3] #(update-radian %))))
(defn draw-star [star]
(q/no-stroke)
(q/fill (if (= (:color star) :c1) (q/color 230 230 230 35) (q/color 70 70 70 35)))
(let [{p1 :p1 p2 :p2 p3 :p3} star
[x1 y1] (get-xy p1), [x2 y2] (get-xy p2), [x3 y3] (get-xy p3)]
(q/triangle x1 y1 x2 y2 x3 y3)))
(defn setup []
(q/smooth)
(let [img (q/load-image "bg.jpg")
stars (flatten (for [_ (range 3)] (rand-star-pair)))]
(q/image img 0 0)
{:img img :stars stars :life 0}))
(defn update [state]
(-> state
(update-in [:stars]
#(vec
(for [s %]
(update-star s))))
(update-in [:life] inc)
(update-in [:stars]
#(if (= 0 (rem (:life state) 10))
(vec (concat % (rand-star-pair)))
%))))
(defn draw [state]
(q/image (:img state) 0 0)
(doseq [s (:stars state)]
(draw-star s))
(if (and (<= (q/frame-count) 600) (= (rem (q/frame-count) 2) 0))
(q/save-frame "frames/####.jpg")))
(defn start-sketch []
(q/sketch
:title "twinkle"
:size [640 480]
:setup setup
:update update
:draw draw
:middleware [m/fun-mode]))
(defn -main [& args]
(start-sketch))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment