Fork me on GitHub
;; title:   Starfield
;; author:  congusbongus, emma bukaceck and phil hagelberg
;; desc:    Draw a starfield with parallax stars
;; license: GPL3+
;; script:  fennel
;; strict:  true
;; tags:    draw parallax effect

;; Adapted from this is my mech by emma bukaceck and phil hagelberg
;; https://tic80.com/play?cart=817

(var t 0)
(var x 96)
(var y 24)
(var STARNEAR 13)
(var STARFAR 14)
(var WIDTH 240)
(var HEIGHT 136)

(local stars [])
(for [i 1 32] (table.insert stars [(math.random 480) (math.random 272)]))

(fn draw-stars [scroll-x scroll-y]
  (each [_ s (ipairs stars)]
    (let [[sx sy] s]
      (pix (% (* (- sx scroll-x) 0.5) WIDTH)
           (% (* (- sy scroll-y) 0.5) HEIGHT) STARFAR)
      (pix (% (- sx scroll-x) WIDTH)
           (% (- sy scroll-y) HEIGHT) STARNEAR))))

(fn _G.TIC []
  (when (btn 0) (set y (- y 1)))
  (when (btn 1) (set y (+ y 1)))
  (when (btn 2) (set x (- x 1)))
  (when (btn 3) (set x (+ x 1)))
  (cls 0)
  (draw-stars x y)
  (spr (+ 1 (* (// (% t 60) 30) 2))
       x y 0 3 0 0 2 2)
  (set t (+ t 1)))

;; <TILES>
;; 001:000000000008888800aaaaaa00a8888800accccc00accecc00accecc00accecc
;; 002:0000000088888000aaaaa8008888a800cccca800cecca800cecca800cecca800
;; 003:0008888800aaaaaa00a8888800accccc00accecc00accecc00accecc00accccc
;; 004:88888000aaaaa8008888a800cccca800cecca800cecca800cecca800cccca800
;; 017:00accccc00aaaaaa00aaacaa0ddeaaccd21deeeeeedddddd0eeeeedd0000ffff
;; 018:cccca800aaaaa800acaaa800caaaedd0eeeed76dddddddeeddeeeee0ffff0000
;; 019:00aaaaaa00aaacaa0ddeaaccd21deeeeeedddddd0eeeeedd0000ffff00000000
;; 020:aaaaa800acaaa800caaaedd0eeeed76dddddddeeddeeeee0ffff000000000000
;; </TILES>

;; <TRACKS>
;; 000:100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; </TRACKS>

;; <SCREEN>
;; 002:0000000000000000000000000000000000000000de000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 003:000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 005:00000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 007:00000000000000000000000000000000d000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 009:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 011:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 012:0e00000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 016:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 019:00000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 020:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 021:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e000000000000000000
;; 022:000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 023:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000
;; 024:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000
;; 025:000d0000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 026:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 032:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e000000000
;; 040:00000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 041:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 042:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000
;; 043:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000
;; 044:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 047:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 051:000000000000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 052:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e000000000088888888888888888888888888888800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 053:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000088888888888888888888888888888800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 054:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000088888888888888888888888888888800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 055:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 056:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa88800000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000
;; 057:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 058:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaa888888888888888888888888888aaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 059:000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000aaa888888888888888888888888888aaa88800000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000
;; 060:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaa888888888888888888888888888aaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 061:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccccccccccccccccccccccccaaa8880000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 062:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccccccccccccccccccccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 063:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccccccccccccccccccccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 064:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa888000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000
;; 065:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 066:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 067:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 068:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 069:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 070:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 071:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 072:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccceeeccccccccceeeccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 073:00000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000aaacccccccccccccccccccccccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 074:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccccccccccccccccccccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 075:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaacccccccccccccccccccccccccccaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 076:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa8880000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000
;; 077:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 078:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa8880000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000
;; 079:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaacccaaaaaaaaacccaaaaaaaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 080:0000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000e000000000000000000aaaaaaaaacccaaaaaaaaacccaaaaaaaaa88800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 081:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaacccaaaaaaaaacccaaaaaaaaa888000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000
;; 082:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ddddddeeeaaaaaacccccccccaaaaaaaaaeeedddddd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 083:000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000ddddddeeeaaaaaacccccccccaaaaaaaaaeeedddddd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 084:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ddddddeeeaaaaaacccccccccaaaaaaaaaeeedddddd0000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000
;; 085:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ddd222111dddeeeeeeeeeeeeeeeeeeeeeeeeddd777666ddd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 086:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ddd222111dddeeeeeeeeeeeeeeeeeeeeeeeeddd777666ddd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 087:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ddd222111dddeeeeeeeeeeeeeeeeeeeeeeeeddd777666ddd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 088:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000eeeeeeddddddddddddddddddddddddddddddddddddeeeeee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 089:000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000eeeeeeddddddddddddddddddddddddddddddddddddeeeeee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 090:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000eeeeeeddddddddddddddddddddddddddddddddddddeeeeee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 091:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000eeeeeeeeeeeeeeeddddddddddddeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 092:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000eeeeeeeeeeeeeeeddddddddddddeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 093:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000eeeeeeeeeeeeeeeddddddddddddeeeeeeeeeeeeeee00000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 094:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 095:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 096:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000
;; 097:000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 105:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000
;; 106:000000000000000000000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 110:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000
;; 112:000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 113:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 114:000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 116:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000
;; 118:000000000000000000000d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 119:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 121:0000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 123:00000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; 124:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000
;; 126:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e00
;; 127:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;; </SCREEN>

;; <PALETTE>
;; 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
;; </PALETTE>