Skip to content

Full game

yesnogame.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
197
198
199
200
201
202
203
204
(def total-rounds 10)
(def max-timer 5)

(defshards load-resources []
  (LoadImage "data/cats/cat01.png") (Push :Name .images)
  (LoadImage "data/cats/cat02.png") (Push :Name .images)
  (LoadImage "data/cats/cat03.png") (Push :Name .images))

(defshards initialize-variables []
  ;; Variables to reset each round 
  true >= .new-round
  max-timer >= .time-remaining
  false >= .input-received

  ;; Variables to reset each game 
  0 >= .total-score
  1 >= .current-round
  false >= .game-over

  ;; Other Shared Variables
  0 >= .left-image-index
  0 >= .right-image-index
  (Count .images) >= .total-images
  true >= .same-image)

(defshards reset-round-variables []
  false > .new-round
  max-timer > .time-remaining
  false > .input-received)

(defshards reset-game-variables []
  (reset-round-variables)
  0 > .total-score
  1 > .current-round
  false > .game-over)

(defshards initialize-round []
  (RandomInt :Max .total-images) > .left-image-index
  (RandomInt :Max .total-images) > .right-image-index

  (If
   :Predicate (-> .left-image-index (Is .right-image-index))
   :Then (-> true > .same-image)
   :Else (-> false > .same-image))

  (reset-round-variables))

(defwire end-round
  (Setup 0 >= .new-round-number)

  .current-round (Math.Add 1)
  > .new-round-number

  (If
   :Predicate
   (-> .new-round-number (IsMore total-rounds))
   :Then
   (-> true > .game-over)
   :Else
   (->
    .new-round-number > .current-round
    true > .new-round)))

(defshards check-answer [yes-input]
  (When
   :Predicate (-> .input-received (Is false))
   :Action
   (->
    (When
     :Predicate (-> .same-image (Is yes-input))
     :Action (-> (Math.Inc .total-score)))

    true > .input-received
    (When
     :Predicate (-> .game-over (IsNot true))
     :Action (-> nil (Step end-round))))))

(defshards timer-tick []
  (When
   :Predicate (-> .game-over (Is false))
   :Action
   (->
    (Math.Dec .time-remaining)
    (When
     :Predicate (-> .time-remaining (IsLess 0))
     :Action (-> nil (Step end-round))))))

(defshards main-game-ui []
  (UI.BottomPanel
   :Contents (-> "Are they the same image? Press the UP arrow if YES, and the DOWN arrow if NO." (UI.Label)))

  (UI.TopPanel
   :Contents
   (->
    (UI.Horizontal
     :Contents
     (->
      "Score: " (UI.Label)
      .total-score (ToString) (UI.Label)
      (UI.Separator)
      "Round: " (UI.Label)
      .current-round (ToString) (UI.Label)
      (UI.Separator)
      "Time Left: " (UI.Label)
      .time-remaining (ToString) (UI.Label)))))

  (UI.CentralPanel
   :Contents
   (->
    (UI.Horizontal
     :Contents
     (->
      (UI.Area
       :Position (float2 -250.0, 0.0)
       :Anchor Anchor.Center
       :Contents
       (-> .images (Take .left-image-index) (UI.Image)))
      (UI.Area
       :Position (float2 250.0, 0.0)
       :Anchor Anchor.Center
       :Contents
       (-> .images (Take .right-image-index) (UI.Image))))))))

(defshards game-over-ui []
  (UI.CentralPanel
   :Contents
   (->
    (UI.Area
     :Position (float2 0.0, 0.0)
     :Anchor Anchor.Center
     :Contents
     (->
      "GAME OVER" (UI.Label)
      (UI.Horizontal
       :Contents
       (->
        "Final Score: " (UI.Label)
        .total-score (ToString) (UI.Label)
        "/"  (UI.Label)
        total-rounds (ToString) (UI.Label)))

      (UI.Button
       :Label "Play Again!"
       :Action (-> (reset-game-variables))))))))

(defloop ui-loop
  (GFX.MainWindow
   :Title "Yes-No Game"
   :Width 1280 :Height 768
   :Contents
   (->
    (Setup
     (GFX.DrawQueue) >= .ui-draw-queue
     (GFX.UIPass .ui-draw-queue) >> .render-steps)
    (| .ui-draw-queue (GFX.ClearQueue))

    (If
     :Predicate (-> .game-over)
     :Then
     (-> (UI .ui-draw-queue (game-over-ui)))
     :Else
     (-> (UI .ui-draw-queue (main-game-ui))))

    (GFX.Render :Steps .render-steps)

    (Inputs.KeyDown
     :Key "up"
     :Action (-> (check-answer true)))

    (Inputs.KeyDown
     :Key "down"
     :Action (-> (check-answer false))))))

(defloop logic-loop
  (Once
   :Action (-> (timer-tick))
   :Every 1.0)

  (WhenNot
   :Predicate (-> .game-over)
   :Action
   (->
    (When
     :Predicate (-> .new-round)
     :Action (-> (initialize-round))))))

(defshards initialize-round []
  (RandomInt :Max .total-images) > .left-image-index
  (RandomInt :Max .total-images) > .right-image-index

  (If
   :Predicate (-> .left-image-index (Is .right-image-index))
   :Then (-> true > .same-image)
   :Else (-> false > .same-image))

  (reset-round-variables))

(defloop game-loop
  (Setup (load-resources) (initialize-variables))
  (Branch [ui-loop, logic-loop]))

(defmesh main)
(schedule main game-loop)
(run main (/ 1.0 60.0))