                                     `    `f  ÀÌ  ÌÌÀÌÀÌÌÌ ÌÌ UU PUUPUUUR%UUUUUUUUUPUUPUU UU PUUUUUUUUUUUUUUUUUUPUU UU	>                                                                À? 333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333633333c3333333333333333333333336333333333333333333333333333333333333333333333336333333333333333333333333c33333333333333333333333333333333333333333333c3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c3333333333333333333333333633c333333333333333333333333333333333333333333333333c33333333333333333333333333c333333333333333333333333333333336333333333333333c33333333c3333333333333333333333333333333333333333333333333333333333c333333333333c3333333333c3333333333333333333333333333333333333333333333333333333333333333633333333333333363333333333c3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c63333333333333333333333333333333333333333c333333333333333333333333333c33333333333336333333333333333333333c3333333333333363333333c33333333333333333333333333333333333333363333333333333333c33333333333333333333333333333333363333333333333333333333333333333333333333333333333333333333333333333336633333333333333333333333333633333333333333333333633333333333333333333333333336333333333333333333333333333333333333333633333333333333333333333333333333333333333333333333633333333333333333336333333363c333333333633333333333333333333333333333333333333333333333333363333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333336333333333333333333333333333333c3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333363333333333333333333333333333333333333333333333333333333333333333333333333333333336333333633333333333333333c3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333336333333333333333333333333c33333333333333333333333333333333333333333333333333333333333333333333c33333333333333333333333333333333333333333333333333333333333333333333c333333333333333333c33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c3333333c363333333333333333c3c33333333333333333333333333333333333333333333333333633333333333333333333c33333333333333333333333333333333333333333333333333333633333333333333363333c333333333333633333c3333333c3333333333333333333333333333333c333333c33333333333333333333333333333333333363333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c33333333333333333633333333333333333333333333333333333333333333333333333333333333333333333333333c333333333c333333333333333c36333333333c333333333333633333333333333333333333333333333333333c3333c3333333333333333333333333333c33333333333336333333333333333333333333333333333333333333333c33333333333333333333333333333333333333333333333333333333333333333333333c33363333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333633333333333333333333333333333333333333333333333333c333c3333c3333333333333333333333333333333333333333336333333c333333333333333333333363336333333333333333333333333c33333333333633333333333333333c333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c333333633333333333333333333333333333333333333333333333333333333333333333333333c33333333333333333333333333333333333c3333333333333333633333333333363333333333333333333333333333333333633333333c3c333336333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c33333333333333333633333333333333333333c333333333333333333333333333333333333333333333333333333333333333333333333c3333333333333333333333333333333333633333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c3333333333333333333333333333363333333333333333333333333333333633333333333333333333333333333333333333633333333333333333333333333333333333333c333333333333333333333333333333336333333333333333333333cc3333333333333363333333333333363333333333333333333333333333333333333333c333333333333333c3333c33333333333333333333333333333333333333333c33333333333633333333333333333333333333333333333333333c333333333333333333333333333333333333333333333333333333333333c3333333333333333333333333333333333333333333333336333333333333333333333333333333333333333333333333333333333633333333333333333333333333333333333363333333333333333333c3333333333333333333333333333333333c3333333333333333333333c3333333333333333333333633333333333333c3333333333333c33333333333333333333c33333333363333333c33333333333333333333333333333333333363333333333333333c3333333333333333333333333333333333333333333333333333c3333333633363333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c333333333333333333333333333333333333333333333333c3333333333333333333333333333333333333333333333333333333333333333333333333333336333333333333333333333333333cc3333333333333333333333333333333333333333c33333333333333333333333333333333333333333333333333c33333333333336333333333333c333333333333333333333333c33333333333333333333333333333333333333333333333333333333c33333333333333333336333333333333333c33333363333333333333333333333333333333333333333633333333333333333333333333333333333333333333333333333333333363333333333333333333333363333333333333333333333c333333633333333333333333333633333333333333333363333333333333333333c333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333633333333333333333333333333333333333333333333633333c33333333333333333333333333333333333333333333333333333333333333333333333c33c33333333333333333333333333333333333333333333363333333333333333333333333333333333333333333333333333333333336333333333333333333333333333333333333333333333333333333333333333333333333333333633333333333333333333333333333333333333333333333333333333333333333633333333333333333333333333333333333333333633333333633333333333333333333333333333333333333633333333333333333333333333333333333333333333333333333333333333333333333333333363333333333333333333333333c3333333333333333333333333633333333333333333333333333333333333333333333333333333333333333333633c3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333363333333333333333333333333333333333333333333333333333333333333333336333333333333333333333333333333333633333333333333333333333333333333c333333333333c333333333333c3333333333333c36333333333333333333333333333333333333333333333333333333333333333333c333333333c33333333333333633333333333333333333333333333333333c33333333333333333333333333333333333333333333c3333333333c33333333333c33333333333333333333336333333333333333333333333333333333333336333333333333333333333333633333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333633333333333333333c333333333333333333333333333333333333333333333333633336333333333333333333333333333333333333333336333333333333333333333333333333333333333333333333333333333c33c33333333333333333333333336333333333333333333333333333333333333333333333333363333333333336333633c333333c33333333333333333333c333333333333333333333333333333333333333333333333333333333333333c3333333333333c36333336333333333333333c333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c3333333333333333333333333333333333333333333333333333333333c3333333363333333333333333333333333333333333c333333333333333333333333333333333333333333333333333333333333333333333333UU33UU33UU33UU33UU3cUU33U533333333336333c3633333333333333333c3336333333c3333333333c3333333333333333333c3333333333333333SUU5SUU5SUU5SUU5SUU5SUU5UUU53333333333c3ccc33333333333333333333333333333333333333333333333333333333333333333333333333333UUUUUUUUUUUUUUUUUUUUUUUUUURU3333333333333f633333333333333633c3c333333333333333333333333363333333333333333333333333333333UUUUUUUUUUUUUUUUUUUUUUUUUUUU3333333333333Ì<33333333333333333333333333333333333363333333333333333336333333333333633333333UUUUUUUUUUUUUUUUUUUUUUUUUUUU333333333333ÃÌÌ333333333633333333333333333333333333333333333333333333c3333333333333333363c33UUUUUUUUUUUUUUUUUUUUUUUUUURU333363333333ÌÆÃ<c333c33333333c33333333333333333633333333333333333333333333333333333333333333SUU5SUU5SUU5SUU5SUU5SUU5UUU5333333333333ÌÌÌ<333c3333333333333333333333333333333333333333333333333633333333333333333333333UU33UU33UU3cUU33UU33UU33U53333333333333ÃÌÌ33333333333333333333333333333363333333333c33363333333333333333333333333333333cUU33333333333333333333333333333336333333333333333333c333333333333363333333333333333333333333336333333336333333333333333SUU533c333333333333333336c3c33333333333333333333333333333333333333333333333333333333333c33333333333333333333333333333333UUUU333333333333333333333333333333333333333363333333333333333333333333333333333333333333333c3c33333333333333333333333333UUUU333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c3UUUU333333333333333333333333333333333333333333333333333333333c33333333333333c3333333333333333333333633333333333333333333UUUU333333333333333333333333333c33333333333333333333333333333333333333333333c3333333333333333333333333333333333333333333SUU53333333333333333333333333333333333333333333333333333c3333333333333333333333333333333333633333333333333333333333333333UU333333333333333333333333333333333c333333333333333333333333333333333333c33333c33333333333333333333333333333333333333333UU33c333333333333363333333333333333333333333633c333333333333c333c333333333333333333333333333333333333333c33333333333333SUU5333333333336333333333c63333333333c33c3333333333333333333333333333333333633336333633333333333333333333333333333333333UUUU33333333333333333336333333333333333333333333333333333333333333333333333333333333633333333333333333333333333333333c33UUUU33333333333333333333363333333333333333333333333333333333333333333333333333336333333333333333333333333333333333333333UUUU333333333333333333333333336333333333333333333333333333333c3333333333333333333333333333333333333333333333333333333333UUUU33333333333333336333333333333333333336333333333333333333333333333333333336333333633363333333333333333333333333333333SUU533333333333333333333336333333333333333333333c333333336333333333333333333333333333363333333333333333333333333333333333UU3333333c33333333333333333333333363333333333333333333333333336333333c33333333333333333333333363333333336333333333333633UU33333333333333333333c33333333333333333333333c333333333333363333333333333333333333333333636333333333333333333333333333SUU56333333333333333333333333333333333333333333333c3333333333633333333333333333333333c3333333333333333333333333333333333UUUU33333333333333c333333333c33333333333333333333333333333333333333333333633363333333333333333333333333333336333333333c3UUUU33333333333333333333333333333333333c33333333333333333333c33333333333333333333333c33333333333333333c33333333333333333UUUU33333333333333333333333333333333333333333333333c33c33c33333333333c33333333333333333333333333333333333333333333333c33UUUU3c3333333333363333333333333333363333333333333333333333333333333333633333333333333333333333c6333333333333333333333333SUUe3333333333c3333333333333333333633333333333333333c3333333333333333c333333333333333333333333333333333333333333333c33333UU3333333333333333333333333333333c33333333333333333333333333333333333633333336333333333333333333333333333333333333333333UU3c33333333333333333333333333333333333333333333UU33UU33UU33UU33UU333333cc333c33333333333333333333333336333333333333333SUU53333333336333333333333333333c333333333333333SUU5SUU5SUU5SUU5SUU533333333333333333633333333333333333333633333333c3333UUUU33333333333333333333333333333333333333333333UUUUUUUUUUUUUUUUUUUU3333333333333333c63333333333333333333333333333333333UUUU3333333333333333333333333333333333333333333cUUUUUUUUUUUUUUUUUUUU3333333333333333333333333333333333333333333333333333UUUU33333333333333333c33336333333333333333333333UUUUUUUUUUUUUUUUUUUU3333333333333333c333333333633c3333333333333333333333UUUU33333333333333333333333333333333333333333333UUUUUUUUUUUUUUUUUUUU333333333333333333333c33333c333333333333333333333333SUU533333333333333333333333333336333333333333333SUU5SUU5SUU5SUU5SUU533c33333333333333333333333333333333333333333333333333UU3333333333333333333333333333633333333333333333UU33UU33UU33UU33UU333333333333333333333333333333333333333333333333333633UU333333333333333333333333333333UU33UU33UU33UU33UU33333333333333333333333333333333c333333333333333333333333333333333333SUU53333333333333333333333333333SUU5SUU5SUU5SUU5SUU533333333333333333333333333333333333c33333c33333333333333333333333333UUUU3333333333333333333333333333UUUUUUUUUUUUUUUUUUUU3333333333333333333333333333333333333333333333333333c333333333333333UUUU3333333333333333333333333333UUUUUUUUUUUUUUUUUUUU333333333333333c3333336333333333333333333333333333333333333333333333UUUU3633333333333333333333333333UUUUUUUUUUUUUUUUUUUU333f33c3333333333333333333333333333333333333333333333333333333333333UUUU3333633333333333333333333333UUUUUUUUUUUUUUUUUUUU333333333333363333336333333333333333333333333333333333333333333c3333SUU53333333333333333333333336333SUU5SUU5SUU5SUU5SUUe33333333333333333c333333c333c333333333333333333333333333333333333333cUU3333333333333333333c3333333333UU36UU33UU33UU33UU33333333333333333333333333333333333333333333333333333333c3333333333333UU33UU33UU33UU33UU33UU33UU33UU33UU3333333333333333333333333333333333333333333333633333333333333333363363333333333333333SUU5SUUeSUU5SUU5SUU5SUU5SUU5SUU5SUU53333333c33333333c3336333333333363333333333333333333333333333333333333333333333333333UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU3336333333333333333333333333333c33333333333333333333333333333333333333333333c3333c33UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU333333c33333333333333333333333333336333333333333333333333c33333333333333333333333333UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU3333c333333333333333333333333333333333333c3333333333333333333333333333c3333333333333UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU333333333333333333333333333333363333333363333333333333333c333333333333c3333333333333SUU5SUU5SUU5SUU5SUU5SUU5SUU5SUU5SUU533333333333333333333333333333333333333333333333333333333333333333333333333333c3333363UU33UU33UU33UU33UU33UU33UU33UU33UU33333333333333c33333333333333333333333333333333333333333333333333333333363333333333333333363333333333333333333336333333333333333333c33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333336363333333333333333333333c333333333333333333333333333333c33333333333333333333333333333333333333333333333333333333333333333333c333c333333333333363333333333333333333333333333333333333333333333333333333333333333633333333333333333333333333333333333333333333333333333333333333333333333633333333333633333333333333333333333333333333333333333363333333333c33333333c333333c3333333333333333333333333333333333333333333333333333c33333333333633333633333333333333c3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333633333363333333333cc333333333333333333333333333333333333333333333333333333333633333333333333333333333333333333333333333333333333c33333c33333333333333333333333333333333333333333333333333333333333363333333333333333c333333333333333333333333333333333333c33333333333333333333333333333333333333333c333333333333333333333333333333333333333333363333333333333333333333c33333333333333333333c3333333333333333333333333333333333333333333333333333333333333333333333333633333333333333333333333336333c333333333333333333333333333333333333333333333333333333333333333333333333c3333333333333633333333333336333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333336333333333333333333333333333333333333333333333333c333333333333333333333363333363333333c33333333333333333333333333333333333333333333333333333c6333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333633333333633333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c33366333333336333333333333333333333333333333333333333336Ç ;; title:   Fennel Snake
;; author:  Colin Woodbury, <colin@fosskers.ca>
;; desc:    Snake, the classic game.
;; site:    https://git.sr.ht/~fosskers/simple-tic80-games
;; license: GPLv3
;; version: 0.1.1
;; script:  fennel
;; strict:  true
;; input:   keyboard

;; Directions to move the head in.
(local up    {:x 0 :y -1})
(local down  {:x 0 :y 1})
(local left  {:x -1 :y 0})
(local right {:x 1 :y 0})
(local dirs  [up down left right])

(var t 0)
(var score 0)
(var snake [{:x 15 :y 8}    ;; tail
            {:x 14 :y 8}    ;; neck
            {:x 13 :y 8}])  ;; head
(var food {:x 1 :y 0})
(var dir (. dirs 1))

(fn init-grass []
  "Calculate random positions for grass pixels."
  (var grid [])
  (for [y 1 136]
    (table.insert grid y [])
    (let [row (. grid y)]
      (for [x 1 240]
        (let [draw? (= 67 (math.random 1 67))]
          (table.insert row x draw?)))))
  grid)

(local grass (init-grass))

(fn update? []
  "Only move the Snake every 6 frames."
  (= 0 (% t 10)))

(fn draw-food []
  "Draw the Fennel."
  (let [transparency 0
        scale 1
        flip (if (< (% t 40) 20) 0 1)]
    (spr 1 (* 8 food.x) (* 8 food.y) transparency scale flip)))

(fn draw-snake []
  (let [transparency 0]
    ;; Draw the body, leaving off the head.
    (each [i point (ipairs snake)]
      ;; This `when' is a hack to avoid using an `until' clause, which isn't
      ;; available until Fennel 1.2.0.
      (when (~= i (length snake))
        (spr 3 (* 8 point.x) (* 8 point.y) transparency)))
    ;; Draw the head.
    (let [head (. snake (length snake))
          scale 1
          flip 0
          rotate (if (= dir up) 0
                     (= dir right) 1
                     (= dir down) 2
                     3)]
      (spr 2 (* 8 head.x) (* 8 head.y) transparency scale flip rotate))))

(fn draw-grass []
  "Draw some randomly placed grass."
  (for [y 1 136]
    (let [row (. grass y)]
      (for [x 1 240]
        (when (. row x)
          (pix x y 6))))))

(fn draw []
  (cls 3)
  (draw-grass)
  (draw-food)
  (draw-snake))

(fn got-food? [head]
  "Did we find the food this turn?"
  (and (= head.x food.x)
       (= head.y food.y)))

(fn any? [p coll]
  "Does any element of the collection satisfy the predicate?"
  (accumulate [found false i x (ipairs coll)]
    ;; This `or' is a workaround to avoid using an `until' clause.
    (or found (p i x))))

(fn set-food []
  "Move the food to a new random square."
  (set food.x (math.random 0 29))
  (set food.y (math.random 0 16))
  (when (any? (fn [_ piece] (and (= piece.x food.x)
                                 (= piece.y food.y)))
              snake)
    (set-food)))

(fn colliding? [head body]
  "Is the head colliding with the body?"
  (any? (fn [i piece]
          (and (not (= i (length body)))  ;; The head itself.
               (= head.x piece.x)
               (= head.y piece.y)))
        body))

(fn _G.TIC []
  (set t (+ t 1))
  (var head (. snake (length snake)))
  (when (update?)
    ;; Move the snake.
    (table.insert snake {:x (% (+ head.x dir.x) 30)
                         :y (% (+ head.y dir.y) 17)})
    (set head (. snake (length snake)))
    (if (colliding? head snake) (do (trace "Game over!") (exit))
        (not (got-food? head)) (table.remove snake 1)
        (do (set score (+ 1 score))
            (sfx 0 (+ 20 score) 10)
            (set-food))))
  ;; Change directions. Can happen any time, even if the snake isn't growing in
  ;; that frame.
  (local neck (. snake (- (length snake) 1)))
  (local last-dir dir)
  (if (btn 0) (set dir (. dirs 1))
      (btn 1) (set dir (. dirs 2))
      (btn 2) (set dir (. dirs 3))
      (btn 3) (set dir (. dirs 4)))
  (when (and (= neck.x (+ head.x dir.x))
             (= neck.y (+ head.y dir.y)))
    (set dir last-dir))
  (draw))
