[feat] moving sprite

This commit is contained in:
SouthFox 2024-05-20 23:05:28 +08:00
parent 9c2f4c1c5f
commit 18e1e4f9a2

View file

@ -18,12 +18,15 @@
(define canvas (get-element-by-id "canvas")) (define canvas (get-element-by-id "canvas"))
(define context (get-context canvas "2d")) (define context (get-context canvas "2d"))
(define *element-x* (/ game-width 2.0))
;; Draw ;; Draw
(define (draw prev-time) (define (draw prev-time)
(clear-rect context 0.0 0.0 game-width game-height)
(set-fill-color! context "#140c1c") (set-fill-color! context "#140c1c")
(set-text-align! context "center") (set-text-align! context "center")
(set-font! context "bold 24px monospace") (set-font! context "bold 24px monospace")
(fill-text context "Hello, World! :)" (/ game-width 2.0) (/ game-height 2.0)) (fill-text context "Hello, World! :)" *element-x* (/ game-height 2.0))
(request-animation-frame draw-callback)) (request-animation-frame draw-callback))
(define draw-callback (procedure->external draw)) (define draw-callback (procedure->external draw))
@ -34,3 +37,20 @@
(dprint "game-height" game-height) (dprint "game-height" game-height)
(console-log canvas) (console-log canvas)
;; Input
(define key:left "ArrowLeft")
(define key:right "ArrowRight")
(define (on-key-down event)
(let ((key (keyboard-event-code event)))
(cond
((string=? key key:left)
(dprint "key:" key)
(set! *element-x* (- *element-x* 10)))
((string=? key key:right)
(dprint "key:" key)
(set! *element-x* (+ *element-x* 10))))))
(add-event-listener! (current-document) "keydown"
(procedure->external on-key-down))