Skip to content

Full game

snake.edn
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
; SPDX-License-Identifier: BSD-3-Clause
; Copyright © 2021 Fragcolor Pte. Ltd.

(def grid-cols 12)
(def grid-rows 10)
(def empty-grid
  [0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0])

(defshards get-index []
  (| (Take 0) >= .x)
  (| (Take 1) >= .y)
  .y (Math.Multiply grid-cols) (Math.Add .x))

(defshards get-free-locations [fruit snake]
  [] >= .locations
  (ForRange
   :From 0 :To (- grid-cols 1) :Action
   (-> >= .a
       (ForRange
        :From 0 :To (- grid-rows 1) :Action
        (-> >= .b
            [.a .b] (ToInt2) >= .location
            (When (-> snake (IndexOf .location) (Is -1) (And) fruit (IsNot .location))
                  (-> .location (Push .locations)))))))
  .locations)

(defshards move-fruit [fruit snake]
  (get-free-locations fruit snake) >= .free-loc
  (Count .free-loc) >= .max
  (RandomInt .max) >= .next-fruit-loc
  .free-loc (Take .next-fruit-loc) (ToInt2))

(defshards move-snake [snake offset grow]
  snake (RTake 0) (Math.Add offset) (Push snake)
  (WhenNot (-> grow) (DropFront snake)))

(defshards populate-grid [fruit snake]
  >= .tmp-grid
  ; first the snake tail and body
  snake (Take 0) (get-index) >= .tail-index
  [.tail-index 4] (Assoc .tmp-grid)
  snake (Slice 1 -1)
  (ForEach
   (-> (get-index) >= .limb-index
       [.limb-index 3] (Assoc .tmp-grid)))
  ; then the fruit
  fruit (get-index) >= .fruit-index
  [.fruit-index 1] (Assoc .tmp-grid)
  ; finally the snake head
  snake (RTake 0) (get-index) >= .head-index
  [.head-index 2] (Assoc .tmp-grid)
  ; return the populated grid
  .tmp-grid)

(def cell-size 18)
(def x-offset 48)
(def y-offset 36)
(defn render-cells [n]
  (if
   (= n -1)
    nil
    (->
     (| (int2
         (% n grid-cols)
         (/ n grid-cols))
        (ToFloat2)
        (Math.Multiply (float2 cell-size))
        (Math.Add (float2 x-offset y-offset)) > .position)
     (| (Take n)
        (UI.Area
         :Position .position
         :Contents
         (-> (Match
              [0 (-> ".") ; empty
               1 (-> "F") ; fruit
               2 (-> "H") ; head
               3 (-> "B") ; body
               4 (-> "T") ; tail
               ]false)
             (UI.Label))))
     (render-cells (- n 1)))))

(defshards menus []
  (UI.MenuBar
   (->
    (UI.Menu
     "File"
     (->
      (UI.Button "New game" (-> (initialize) (UI.CloseMenu)))
      (UI.Separator)
      (UI.Button "Quit" (Stop)))))))

(defshards initialize []
  [(int2 1 2) (int2 2 2) (int2 3 2) (int2 3 3) (int2 4 3)] >= .snake
  false >= .grow
  (move-fruit (int2 0 0) .snake) >= .fruit
  "right" >= .direction >= .prev-direction
  (Time.Now) >= .last-tick
  ; do it once to not wait the next tick
  empty-grid (populate-grid .fruit .snake) >= .grid
  false >= .game-over)

(defloop main-wire
  (Once (initialize))
  ; logic
  (WhenNot (-> .game-over)
           ; normal game
           (When (-> (Time.Now) (Math.Subtract .last-tick) (IsMoreEqual 0.33))
                 (-> (Time.Now) > .last-tick
                     ; move the snake
                     .direction (Match ["up" (move-snake .snake (int2 0 -1) .grow)
                                        "right" (move-snake .snake (int2 1 0) .grow)
                                        "down" (move-snake .snake (int2 0 1) .grow)
                                        "left" (move-snake .snake (int2 -1 0) .grow)])

                     (Count .snake) (Math.Subtract 1) >= .head-idx
                     .snake (Take .head-idx) >= .head
                     ; snake eats its own body?
                     (When (-> .snake (IndexOf .head) (IsNot .head-idx))
                           (-> true > .game-over))
                     ; snake hits a wall?
                     (When (-> .head (Take 0) (IsLess 0) (Or) (Take 0) (IsMoreEqual grid-cols)
                               (Or)
                               .head (Take 1) (IsLess 0) (Or) (Take 1) (IsMoreEqual grid-rows))
                           (-> true > .game-over))
                     ; did the snake eat the fruit?
                     (If (-> .head (Is .fruit))
                         (-> true > .grow
                             (move-fruit .fruit .snake) > .fruit)
                         (-> false > .grow)
                         :Passthrough true)

                     (WhenNot (-> .game-over)
                              (-> empty-grid (populate-grid .fruit .snake) > .grid
                                  .direction > .prev-direction)))))
  ; window
  (GFX.MainWindow
   :Title "Snake game" :Width 480 :Height 360
   :Contents
   (->
    (Setup
     (GFX.DrawQueue) >= .ui-draw-queue
     (GFX.UIPass .ui-draw-queue) >> .render-steps)

    .ui-draw-queue (GFX.ClearQueue)

    (UI
     .ui-draw-queue
     (->
      (UI.TopPanel :Contents (menus))
      (UI.BottomPanel
       :Contents
       (->
        (When (-> .game-over)
              (UI.Horizontal
               (-> "GAME OVER!" (UI.Label :Style {:color (color 255 0 0)})
                   (UI.Space 10.0)
                   "Final score: " (UI.Label)
                   (Count .snake) (ToString) (UI.Label))))))
      .grid
      (Setup (float2 0) >= .position)
      (render-cells (- (* grid-cols grid-rows) 1))))

    (GFX.Render :Steps .render-steps)

    (Inputs.KeyDown
     "up"
     (When (-> .prev-direction (IsNot "down"))
           (-> "up" > .direction)))
    (Inputs.KeyDown
     "right"
     (When (-> .prev-direction (IsNot "left"))
           (-> "right" > .direction)))
    (Inputs.KeyDown
     "down"
     (When (-> .prev-direction (IsNot "up"))
           (-> "down" > .direction)))
    (Inputs.KeyDown
     "left"
     (When (-> .prev-direction (IsNot "right"))
           (-> "left" > .direction)))
    (Inputs.KeyDown
     "space" (initialize)))))

(defmesh root)
(schedule root main-wire)
(run root (/ 1.0 60))