Snake Game Demo improvements

This commit is contained in:
David Botton 2021-01-17 10:40:50 -05:00
parent cffea6af69
commit d06ce51f44

View file

@ -21,15 +21,19 @@
(defclass app-data () (defclass app-data ()
((snake-direction ((snake-direction
:initform :right :initform :right
:type snake-direction-type
:accessor snake-direction) :accessor snake-direction)
(score (score
:initform 0 :initform 0
:type number
:accessor score) :accessor score)
(food (food
:initform (new-food) :initform (new-food)
:type cons
:accessor food) :accessor food)
(snake (snake
:initform nil :initform nil
:type cons
:accessor snake))) :accessor snake)))
(defun display-splash (body) (defun display-splash (body)
@ -81,17 +85,17 @@
(1+ (cadr head-cell)))))) (1+ (cadr head-cell))))))
(if (or (< (car head-cell) 0) (cond ((or (< (car head-cell) 0)
(< (cadr head-cell) 0) (< (cadr head-cell) 0)
(>= (* (car head-cell) segment-size) display-width) (>= (* (car head-cell) segment-size) display-width)
(>= (* (cadr head-cell) segment-size) display-height) (>= (* (cadr head-cell) segment-size) display-height)
(self-collision)) (self-collision))
(progn
(fill-style cx :red) (fill-style cx :red)
(font-style cx "bold 20px sans-serif") (font-style cx "bold 20px sans-serif")
(fill-text cx "GAME OVER" 30 30) (fill-text cx "GAME OVER" 30 30)
(setf game-over t)) (setf game-over t))
(progn (t
(fill-style cx :purple) (fill-style cx :purple)
(push head-cell (snake app)) (push head-cell (snake app))
(dolist (cell (snake app)) (dolist (cell (snake app))
@ -99,8 +103,8 @@
(fill-style cx :white) (fill-style cx :white)
(if (equal head-cell (food app)) (cond ((equal head-cell (food app))
(progn
(fill-text cx (format nil "Score: ~A" (score app)) (fill-text cx (format nil "Score: ~A" (score app))
5 (- display-height 15)) 5 (- display-height 15))
(setf (score app) (+ (score app) 10)) (setf (score app) (+ (score app) 10))
@ -110,14 +114,14 @@
5 (- display-height 15)) 5 (- display-height 15))
(setf (food app) (new-food))) (setf (food app) (new-food)))
(progn (t
(draw-segment (car (last (snake app)))) (draw-segment (car (last (snake app))))
(setf (snake app) (butlast (snake app)))))) (setf (snake app) (butlast (snake app)))))
(fill-style cx :brown) (fill-style cx :brown)
(draw-segment (food app))) (draw-segment (food app)))
game-over))) game-over))))
(defun on-key-down (obj event) (defun on-key-down (obj event)
(let ((app (connection-data-item obj "app-data")) (let ((app (connection-data-item obj "app-data"))