mirror of
https://gitlab.com/eql/lqml.git
synced 2025-12-06 02:30:38 -08:00
add examples 'tilt-sensor' (sensor reading on mobile devices)
This commit is contained in:
parent
834440953b
commit
15fc8c062a
9 changed files with 335 additions and 0 deletions
57
examples/tilt-sensor/lisp/main.lisp
Normal file
57
examples/tilt-sensor/lisp/main.lisp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
(in-package :maze)
|
||||
|
||||
(defconstant +w+ 13) ; ball width
|
||||
|
||||
(defvar *x*) ; ball x
|
||||
(defvar *y*) ; ball y
|
||||
|
||||
(defun move (x-rotation y-rotation) ; called from QML
|
||||
(labels ((add (x)
|
||||
(truncate (signum x)))
|
||||
(to-pos (x)
|
||||
(truncate (/ (+ x (/ +w+ 2))
|
||||
+w+)))
|
||||
(normalize (x)
|
||||
(* (to-pos x) +w+)))
|
||||
(let* ((dx (min (* 0.2 x-rotation) (1- +w+)))
|
||||
(dy (min (* 0.2 y-rotation) (1- +w+)))
|
||||
(add-x (add dx))
|
||||
(add-y (add dy)))
|
||||
(if (aref *maze*
|
||||
(+ add-x (to-pos *x*))
|
||||
(to-pos *y*))
|
||||
(setf *x* (normalize *x*))
|
||||
(incf *x* dx))
|
||||
(if (aref *maze*
|
||||
(to-pos *x*)
|
||||
(+ add-y (to-pos *y*)))
|
||||
(setf *y* (normalize *y*))
|
||||
(incf *y* dy))))
|
||||
(move-ball)
|
||||
(values))
|
||||
|
||||
(defun move-ball ()
|
||||
(if (and (= *x* +w+)
|
||||
(= *y* 0))
|
||||
(new-game)
|
||||
(let ((ball (find-quick-item ui:*ball*)))
|
||||
;; 'qset' is faster than 'q>', but can't trigger animations
|
||||
(qset ball |x| (- *x* 0.5))
|
||||
(qset ball |y| (- *y* 0.5)))))
|
||||
|
||||
(defun start ()
|
||||
(setf *x* (* +w+ (- *width* 1))
|
||||
*y* (* +w+ (- *height* 2)))
|
||||
(move-ball)
|
||||
(q> |running| ui:*timer* t))
|
||||
|
||||
(defun stop ()
|
||||
(q> |running| ui:*timer* nil))
|
||||
|
||||
(defun new-game ()
|
||||
(new-maze)
|
||||
(start))
|
||||
|
||||
(progn
|
||||
(start)
|
||||
(qlater 'ini))
|
||||
61
examples/tilt-sensor/lisp/maze.lisp
Normal file
61
examples/tilt-sensor/lisp/maze.lisp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
;;; original copyright: CL Maze 20030311 by Joe Wingbermuehle
|
||||
;;;
|
||||
;;; this is a reviewed version
|
||||
|
||||
(in-package :maze)
|
||||
|
||||
;; both must be odd
|
||||
(defconstant *width* 25)
|
||||
(defconstant *height* 39)
|
||||
|
||||
(defvar *maze*)
|
||||
|
||||
(defun carve-maze (x y)
|
||||
(let ((d (random 4)))
|
||||
(dotimes (c 4)
|
||||
(let* ((cd (mod (+ c d) 4))
|
||||
(dv (case cd
|
||||
(0 (list 1 0))
|
||||
(1 (list 0 1))
|
||||
(2 (list -1 0))
|
||||
(t (list 0 -1))))
|
||||
(x1 (+ x (car dv)))
|
||||
(y1 (+ y (cadr dv)))
|
||||
(x2 (+ x1 (car dv)))
|
||||
(y2 (+ y1 (cadr dv))))
|
||||
(when (and (< 0 x2 *width*)
|
||||
(< 0 y2 *height*)
|
||||
(aref *maze* x1 y1)
|
||||
(aref *maze* x2 y2))
|
||||
(set-visible x1 y1 nil)
|
||||
(set-visible x2 y2 nil)
|
||||
(qsleep 0.001) ; slow down to make visible
|
||||
(carve-maze x2 y2))))))
|
||||
|
||||
(defun generate-maze ()
|
||||
(set-visible 1 1 nil)
|
||||
(carve-maze 1 1)
|
||||
(set-visible 1 0 nil)
|
||||
(set-visible (- *width* 1) (- *height* 2) nil))
|
||||
|
||||
(defun set-visible (x y visible)
|
||||
(setf (aref *maze* x y) visible)
|
||||
;; |visible| doesn't work inside Repeater
|
||||
(q> |opacity| (q! |itemAt| ui:*maze*
|
||||
(+ x (* y *width*)))
|
||||
(if visible 1 0)))
|
||||
|
||||
(defun display-maze ()
|
||||
(dotimes (y *height*)
|
||||
(dotimes (x *width*)
|
||||
(set-visible x y (aref *maze* x y)))))
|
||||
|
||||
(defun new-maze ()
|
||||
(setf *maze* (make-array (list *width* *height*)
|
||||
:initial-element t))
|
||||
(display-maze)
|
||||
(qlater 'generate-maze))
|
||||
|
||||
(defun ini ()
|
||||
(setf *random-state* (make-random-state t))
|
||||
(new-maze))
|
||||
7
examples/tilt-sensor/lisp/package.lisp
Normal file
7
examples/tilt-sensor/lisp/package.lisp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defpackage :maze
|
||||
(:use :cl :qml)
|
||||
(:export
|
||||
#:*maze*
|
||||
#:ini
|
||||
#:new-maze))
|
||||
|
||||
13
examples/tilt-sensor/lisp/ui-vars.lisp
Normal file
13
examples/tilt-sensor/lisp/ui-vars.lisp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(defpackage ui
|
||||
(:use :cl :qml)
|
||||
(:export
|
||||
#:*ball*
|
||||
#:*maze*
|
||||
#:*timer*))
|
||||
|
||||
(in-package :ui)
|
||||
|
||||
(defparameter *ball* "ball")
|
||||
(defparameter *maze* "maze")
|
||||
(defparameter *timer* "timer")
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue