diff --git a/tutorial/07-tutorial.lisp b/tutorial/07-tutorial.lisp new file mode 100644 index 0000000..c7ae556 --- /dev/null +++ b/tutorial/07-tutorial.lisp @@ -0,0 +1,68 @@ +(defpackage #:clog-user + (:use #:cl #:clog) + (:export start-tutorial)) + +(in-package :clog-user) + +(defun on-click (obj) + (setf (text obj) "DEAD") + (setf (connection-data-item obj "done") t) + (set-on-click obj nil) +) + +(defun on-new-window (body) + (handler-case + (progn + (setf (title (html-document body)) "Tutorial 7") + + (setf (hiddenp (prog1 + (create-child body "

KILL Darth's Tie Fighter - Click on it!

") + (sleep 2))) t) + + (let* ((mover (create-child body "
(-o-)
")) + (bounds-x (parse-integer (width (window body)) :junk-allowed t)) + (bounds-y (parse-integer (height (window body)) :junk-allowed t)) + (mover-x (/ bounds-x 2)) + (mover-y (/ bounds-y 2))) + + (setf (positioning mover) :fixed) + (set-on-click mover #'on-click) + + (set-on-resize (window body) + (lambda (obj) + (setf bounds-x (parse-integer (width (window body)) :junk-allowed t)) + (setf bounds-y (parse-integer (height (window body)) :junk-allowed t)))) + + (loop + (when (connection-data-item body "done") + (return)) + + (setf (top mover) (format nil "~Apx" mover-y)) + (setf (left mover) (format nil "~Apx" mover-x)) + + (if (= (random 2) 0) + (incf mover-y (random 10)) + (decf mover-y (random 10))) + (if (= (random 2) 0) + (incf mover-x (random 10)) + (decf mover-x (random 10))) + + (when (< mover-x 0) + (setf mover-x 0)) + (when (> mover-x bounds-x) + (setf mover-x bounds-x)) + + (when (< mover-y 0) + (setf mover-y 0)) + (when (> mover-y bounds-y) + (setf mover-y bounds-y)) + + (sleep .02))) + ) (error (c) + (format t "Lost connection.~&")))) + +(defun start-tutorial () + "Start turtorial." + + (initialize #'on-new-window) + (open-browser)) diff --git a/tutorial/README.md b/tutorial/README.md index 0502e46..f4daa60 100644 --- a/tutorial/README.md +++ b/tutorial/README.md @@ -39,3 +39,4 @@ Tutorial Summary 05-tutorial.lisp - The event target, reusing event handlers 05-tutorial.lisp - Using connection-data-item 06-tutorial.lisp - Tasking and events +07-tutorial.lisp - My first CLOG video game