Skip to content

Instantly share code, notes, and snippets.

@LnL7
Created April 1, 2013 16:39
Show Gist options
  • Save LnL7/5286075 to your computer and use it in GitHub Desktop.
Save LnL7/5286075 to your computer and use it in GitHub Desktop.
(define white #b111111111111)
(define blue #b111100000000)
(define green #b000011110000)
(define red #b000000001111)
;Tile smaller than 10 might slow drawing, but will not cause flashes
(define tile 10) ;size of a `pixel'
(define size 13) ;number of pixels (/ 130 10)
(define index 169) ;total number of pixels
(define buffer (make-vector index white)) ;the pixel buffer
(define (get-ref x y) ;buffer index of point with x y
(+ (* y size) x))
(define (get-x ref) ;x position for buffer index
(modulo ref size))
(define (get-y ref) ;y position for buffer index
(floor (/ ref size)))
(define (set-px! x y color) ;set color of point in buffer
(let ((ref (get-ref (int x) (int y))))
(vector-set! buffer ref color)))
(define (int num) (inexact->exact (floor num))) ;convert float to valid vector index
(define (each proc!)
(let iter ((ctr 0)
(res #f))
(if (= ctr index)
res
(iter
(+ ctr 1)
(proc! ctr)))))
(define (draw!) ;draw the buffer to screen
(each
(lambda (ref)
(fill-rectangle!
(* (get-x ref) tile)
(* (get-y ref) tile)
tile tile
(vector-ref buffer ref)))))
(define (clear!) ;clear the buffer (white)
(each
(lambda (ref)
(vector-set! buffer ref white))))
;Test Code
(define pos (cons 3 3)) ;position of test object
(define spd 0.3) ;movement speed of test object
(define (start)
(let iter ()
(let ((x (car pos))
(y (cdr pos)))
(begin ;; Render
(clear!)
(set-px! x y blue)
(draw!))
(begin ;; Update
(set-car! pos (+ x spd))
(cond ;keeps test object on the screen
((< x 2) (set! spd (+ (abs spd)))) ;move right
((> x 11) (set! spd (- (abs spd)))))) ;move left
(iter))))
(start) ;start the `game loop'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment