Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns snake-quil.core
(:require [quil.core :as q :include-macros true]
[quil.middleware :as m]))
(enable-console-print!)
(defn setup []
(q/frame-rate 60)
(q/color-mode :hsb)
{:color 0
:dir :east
:snake [[50 50] [60 50] [70 50]]})
(def speed 2)
(defn move-head [[x y] dir]
(case dir
:north [x (- y speed)]
:east [(+ speed x) y]
:south [x (+ y speed)]
:west [(- x speed) y]))
(defn update-state [state]
(let [new-head (move-head (first (:snake state))
(:dir state))
scooted (conj (drop-last 1 (:snake state))
new-head)]
{:color (mod (+ (:color state) 0.7) 255)
:dir (:dir state)
:snake scooted}))
(defn draw-state [state]
(q/background 240)
(q/fill (:color state) 255 255)
(doseq [[x y] (:snake state)]
(q/rect x y 10 10)))
(def key-map {:w :north
:d :east
:a :west
:s :south})
(defn key-pressed [state key-info]
(println "key presseeddd")
(println "key info: " key-info)
(println "started with state: ")
(println state)
(if-let [key-dir ((:key key-info) key-map)]
(let [new-state (assoc state :dir ((:key key-info) key-map))]
(println "new state: ")
(println new-state)
new-state)
state))
(q/defsketch snake-quil
:host "snake-quil"
:size [500 500]
; setup function called only once, during sketch initialization.
:setup setup
; update-state is called on each iteration before draw-state.
:update update-state
:draw draw-state
:key-pressed key-pressed
; This sketch uses functional-mode middleware.
; Check quil wiki for more info about middlewares and particularly
; fun-mode.
:middleware [m/fun-mode])
(defn inc-and-scoot [seq]
(let [new-head (inc (first seq))]
(conj (drop-last 1 seq) new-head)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.