spook

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 9ca998b07d265576eea8402f279fd1a6ec80ae3f
parent ccc242eb528252a8f4ef612b5e7e0c373ed77ff8
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Sun, 22 Oct 2023 19:49:52 +0300

Drawing paddle and a bit of movement.

Diffstat:
Mmain.scm | 119++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 94 insertions(+), 25 deletions(-)

diff --git a/main.scm b/main.scm @@ -3,38 +3,107 @@ (define (a) (alert "aaaaa")) -(define (set-fill-style! ctx style) - (set! (.fillStyle ctx) style)) +(define canvas + (%inline "document.getElementById" "canvas")) -(define (fill-rect! ctx a b c d) - (%inline ".fillRect" ctx a b c d)) +(define ctx + (%inline ".getContext" canvas "2d")) -(define canvas (%inline "document.getElementById" "canvas")) -(define ctx (%inline ".getContext" canvas "2d")) +(set! (.fillStyle ctx) "rgb(200, 0, 0)") -(define canvas-width (.width canvas)) -(define canvas-height (.height canvas)) +(define brick-half-width 10) +(define brick-half-height 5) -(set-fill-style! ctx "rgb(200, 0, 0)") -(fill-rect! ctx 10 10 50 50) +(define (make-brick x y) + (cons x y)) -(define (make-world x y) - (list x y)) +(define (brick-x brick) + (car brick)) -(define (world-x world) - (car world)) +(define (brick-y brick) + (car (cdr brick))) -(define (world-y world) - (car (cdr world))) +(define paddle-half-width 20) +(define paddle-half-height 3) -(define (draw ctx world) - (fill-rect! ctx - (world-x world) - (world-y world) - 50 - 50)) +(define (make-paddle x y) + (cons x y)) -(define world (make-world (/ canvas-width 2) - (/ canvas-height 2))) +(define (paddle-x paddle) + (car paddle)) -(draw ctx world) +(define (paddle-y paddle) + (cdr paddle)) + +(define (set-paddle-x! paddle value) + (set! (car paddle) value)) + +(define paddle + (make-paddle (/ (.width canvas) + 2) + (- (.height canvas) + 50))) + +(define (draw-rect x y half-width half-height) + (%inline ".fillRect" + ctx + (- x half-width) + (- y half-height) + (* 2 half-width) + (* 2 half-height))) + +(define (draw-bricks bricks) + (for-each + (lambda (brick) + (draw-rect (brick-x brick) + (brick-y brick) + brick-half-width + brick-half-height)) + bricks)) + +(define bricks + '() + #; + (make-world (/ (.width (get-canvas)) 2) ; + (/ (.height (get-canvas)) 2))) + +(define (clear-rect) + (%inline ".clearRect" + ctx + 0 + 0 + (.width canvas) + (.height canvas))) + +(define last-time 0) + +(define cursor (cons (/ (.width canvas) 2) + (/ (.height canvas) 2))) + +(define (cursor-x cursor) + (car cursor)) + +(define (loop time) + (define dt (- time last-time)) + (set! last-time time) + (clear-rect) + + #;(alert world) + (draw-rect (paddle-x paddle) + (paddle-y paddle) + paddle-half-width + paddle-half-height) + + (set-paddle-x! paddle (cursor-x cursor)) + + (%inline "window.requestAnimationFrame" (callback loop))) + +(%inline "window.requestAnimationFrame" (callback loop)) + +(%inline ".addEventListener" + canvas + "mousemove" + (callback (lambda (e) + (set! cursor + (cons (.offsetX e) + (.offsetY e))))))