Skip to content
This repository was archived by the owner on May 20, 2021. It is now read-only.

Commit 03ea062

Browse files
committed
Very basic states
1 parent 0cf2fc5 commit 03ea062

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

snake.lisp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,21 @@
111111
(defmethod initialize-instance :after ((this game-state) &key)
112112
(with-slots (player food-pos) this
113113
(setf food-pos (new-food-pos))
114+
;; TODO(bsvercl): We should really have a CENTERED function or something.
114115
(setf player (make-snake (gamekit:vec2 (/ +segments-across-width+ 2)
115116
(/ +segments-across-height+ 2))))))
116117

117118
(defmethod update ((this game-state))
118-
(with-slots (player food-pos score) this
119+
(with-slots (player food-pos score end-callback) this
119120
(let* ((pos (snake-position player))
120121
(ate-food-p (bodge-math:vec= food-pos pos)))
121122
(advance player ate-food-p)
122123
(when ate-food-p
123124
(setf food-pos (new-food-pos))
124125
(incf score 50))
125126
(when (hit-itself player)
126-
(setf score -1)))))
127+
(setf score -1)
128+
(funcall end-callback)))))
127129

128130
(defmethod draw ((this game-state))
129131
;; TODO(bsvercl): Drop into CL-BODGE to speed this up.
@@ -147,17 +149,36 @@
147149
(gamekit:draw-text (format nil "Score: ~A" (score-of this))
148150
(gamekit:vec2 25 (- +screen-height+ 25))))
149151

150-
(defmethod handle-key ((state game-state) key)
151-
(with-slots (player) state
152+
(defmethod handle-key ((this game-state) key)
153+
(with-slots (player food-pos) this
152154
(case key
153155
(:w (change-direction player :up))
154156
(:s (change-direction player :down))
155157
(:a (change-direction player :left))
156158
(:d (change-direction player :right))
157-
(:space (change-direction player :nothing)))))
159+
(:space (change-direction player :nothing))
160+
(:q (setf food-pos (new-food-pos))))))
161+
162+
(defclass game-over-state (state)
163+
((restart-callback :initarg :restart)))
164+
165+
(defmethod draw ((this game-over-state))
166+
(declare (ignore this))
167+
(gamekit:draw-text "Good job, dummy! You ate yourself."
168+
(gamekit:vec2 (/ +screen-width+ 2)
169+
(/ +screen-height+ 2)))
170+
(gamekit:draw-text "Press SPACE to restart."
171+
(gamekit:subt (gamekit:vec2 (/ +screen-width+ 2)
172+
(/ +screen-height+ 2))
173+
(gamekit:vec2 0 20))))
174+
175+
(defmethod handle-key ((this game-over-state) key)
176+
(with-slots (restart-callback) this
177+
(when (eq key :space)
178+
(funcall restart-callback))))
158179

159180
(gamekit:defgame snake-game ()
160-
((current-state :accessor state-of))
181+
((current-state))
161182
(:viewport-title "Snake")
162183
(:viewport-width +screen-width+)
163184
(:viewport-height +screen-height+)
@@ -173,8 +194,7 @@
173194
(labels ((start ()
174195
(setf current-state (make-instance 'game-state :end #'end)))
175196
(end ()
176-
;; TODO(bsvercl): Game over state.
177-
(print "uh oh, we haven't implemented this yet.")))
197+
(setf current-state (make-instance 'game-over-state :restart #'start))))
178198
(setf current-state (make-instance 'main-menu-state :start #'start)))
179199
(macrolet ((%binder (key &body body)
180200
`(gamekit:bind-button ,key :pressed #'(lambda () ,@body))))

0 commit comments

Comments
 (0)