-
-
Save laurentpetit/919494 to your computer and use it in GitHub Desktop.
This file contains 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
; Daniel Shiffman's initial example in his PVector + Processing Tutorial | |
; re-written in clojure | |
(ns example1 | |
(:use [rosado.processing] | |
[rosado.processing.applet])) | |
(set! *warn-on-reflection* true) | |
(def x (atom 100)) | |
(def y (atom 100)) | |
(def xspeed (atom 1)) | |
(def yspeed (atom 3.3)) | |
(defn draw-background | |
[] | |
(no-stroke) | |
(fill 255 10) | |
(rect 0 0 (width) (height))) | |
(defn draw-ball | |
[] | |
(stroke 0) | |
(fill 175) | |
(ellipse @x @y 16 16)) | |
(defn bounced? | |
"Checks to see if the ball has crossed any of our bounds" | |
[location, bound, current-speed] | |
(or (and (> location bound) (> current-speed 0)) | |
(and (< location 0) (< current-speed 0)))) | |
(defn increment-position-and-speed | |
[] | |
(swap! x + @xspeed) | |
(swap! y + @yspeed) | |
(if (bounced? @x (width) @xspeed) | |
(swap! xspeed * -1)) | |
(if (bounced? @y (height) @yspeed) | |
(swap! yspeed * -1))) | |
(defn bounce-ball | |
"The meat. Draws a ball, increments it's position and speed" | |
[] | |
(draw-background) | |
(draw-ball) | |
(increment-position-and-speed)) | |
(defn setup | |
"The setup. Gets run once." | |
[] | |
(smooth) | |
(background 255)) | |
(defn draw | |
"Draw loop. Gets called for every frame." | |
[] | |
(bounce-ball)) | |
;; Now we just need to define an applet: | |
(defapplet example1 :title "An example." | |
:setup setup :draw draw :size [200 200]) | |
(run example1) | |
;; (stop example1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment