Tutorial 7

This commit is contained in:
David Botton 2021-01-06 22:02:24 -05:00
parent 0a20473a72
commit 5c498e16cf
2 changed files with 69 additions and 0 deletions

68
tutorial/07-tutorial.lisp Normal file
View file

@ -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 "<h2>KILL Darth's Tie Fighter - Click on it!</h2>")
(sleep 2))) t)
(let* ((mover (create-child body "<div>(-o-)</div>"))
(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))

View file

@ -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