Skip to content

Instantly share code, notes, and snippets.

Created August 10, 2014 07:50
Show Gist options
  • Save anonymous/2b0443ea044c84af3881 to your computer and use it in GitHub Desktop.
Save anonymous/2b0443ea044c84af3881 to your computer and use it in GitHub Desktop.
Octo Chip8 Program
Play this game by pasting the program into http://johnearnest.github.io/Octo/
{"tickrate":"20","backgroundColor":"#640","fillColor":"#A5A","buzzColor":"#FFAA00","quietColor":"#000000","shiftQuirks":false,"loadStoreQuirks":false}
# Chip8 Mr Worm
# rmmh 2014
:alias wafflex v4
:alias waffley v5
:alias headx v6
:alias heady v7
:alias headdir v8
:alias tailx v9
:alias taily vA
:alias growth vB
:proto queue
:alias queuehead vC
:alias queuetail vD
:const STARTLENGTH 12
:const NDIRS 12
:const LASTDIR 11 # NDIRS - 1
:const TAILHEIGHT 5
: title-MrW 0xF8 0x04 0xA8 0x05 0xAB 0x85 0xAA 0x05 0xAA 0x05 0xAA 0x07 0x00 0x00 0x00 0x00 0x00 0x80 0x6D 0xCA 0xA6 0x8E 0xCA 0x8E 0x6E 0xCE 0x00 0x00 0x00 0x00 0x00 0x00
: title-orm 0x40 0x00 0x40 0x00 0x5D 0xDF 0x55 0x15 0x55 0x15 0xDD 0x15 0x00 0x00 0x00 0x00 0x05 0x40 0xCA 0x5B 0x7F 0x6A 0xAA 0x71 0xEA 0x5B 0x00 0x00 0x00 0x00 0x00 0x00
: dead-score 0xC0 0x00 0x80 0x00 0x80 0x00 0xDB 0xB7 0x52 0xA5 0x52 0xA7 0x52 0xA4 0xDB 0xA7 0x00 0x00 0x15 0x04 0x14 0x04 0x1D 0x77 0x15 0x55 0x15 0x75 0x00 0x10 0x00 0x70
:const WAFFLEHEIGHT 5
: waffle-sprite 0xF8 0xA8 0xF8 0xA8 0xF8
: worm-dirtab
# struct { char offx, offy, _padding, sprite[5]; };
: worm-ul -1 -2 0 0x70 0xF8 0xC0 0x80 0x00
: worm-u 0 -2 0 0x70 0xF8 0x88 0x00 0x00
: worm-ur +1 -2 0 0x70 0xF8 0x18 0x08 0x00
: worm-ru +2 -1 0 0x70 0x38 0x18 0x18 0x10
: worm-r +2 0 0 0x30 0x18 0x18 0x18 0x30
: worm-rd +2 +1 0 0x10 0x18 0x18 0x38 0x70
: worm-dr +1 +2 0 0x00 0x08 0x18 0xF8 0x70
: worm-d 0 +2 0 0x00 0x00 0x88 0xF8 0x70
: worm-dl -1 +2 0 0x00 0x80 0xC0 0xF8 0x70
: worm-ld -2 +1 0 0x40 0xC0 0xC0 0xE0 0x70
: worm-l -2 0 0 0x60 0xC0 0xC0 0xC0 0x60
: worm-lu -2 -1 0 0x70 0xE0 0xC0 0xC0 0x40
: queue-get # ( -- v0=item )
i := queue
i += queuetail
load v0
queuetail += 1
;
: queue-put # ( v0=item -- )
i := queue
i += queuehead
save v0
queuehead += 1
;
: queue-init # ( -- )
queuehead := 0
queuetail := 0
;
:proto move-worm
: place-waffle
i := waffle-sprite
loop
wafflex := random 0xFF
waffley := random 0xFF
sprite wafflex waffley WAFFLEHEIGHT
if vf == 0 then return
sprite wafflex waffley WAFFLEHEIGHT
again
;
:proto score
:proto hiscore
: init
clear
hires
# clear score
i := score
v0 := 0
save v0
# random ortho direction
v0 := random 0x3
headdir <<= v0
headdir += v0
# random pos (remember, it wraps)
headx := random 0xFF
heady := random 0xFF
queue-init
tailx := headx
taily := heady
growth := STARTLENGTH
move-worm
place-waffle
;
: hit-waffle?
i := waffle-sprite
sprite wafflex waffley WAFFLEHEIGHT
sprite wafflex waffley WAFFLEHEIGHT
v0 := vF
;
: eat-waffle
sprite wafflex waffley WAFFLEHEIGHT
place-waffle
growth += 3
i := score
load v0
v0 += 1
i := score
save v0
;
: bcd-temp 0 0 0
: draw-digits # ( v3=num, v4=x, v5=y )
i := bcd-temp
bcd v3
load v2
:proto nohundred
if v3 < 100 then jump nohundred
i := hex v0
sprite v4 v5 5
i := hex v1
: nohundred
:proto noten
v4 += 5
if v3 < 10 then jump noten
sprite v4 v5 5
: noten
v4 += 5
i := hex v2
sprite v4 v5 5
;
: set-hiscore
i := hiscore
v0 := v3
save v0
;
: show-score
lores
i := dead-score
v4 := 15
v5 := 8
sprite v4 v5 0
v4 += 20
v5 += 3
:next score v3 := 0
draw-digits
v4 += -10
v5 += 6
:next hiscore v0 := 0
if v3 > v0 then set-hiscore
v3 := v0
draw-digits
# busy loop so score stays up for a second
v0 := 100
loop
v0 += -1
if v0 != 0 then again
vF := key
;
: collision
hit-waffle?
if v0 == 1 then jump eat-waffle
show-score
init
;
: remove-segment
queue-get
# i = worm-dirtab + 8 * v0
i := worm-dirtab
v0 <<= v0
v0 <<= v0
v0 <<= v0
i += v0
# move worm
load v2 # i now points at the sprite
tailx += v0
taily += v1
# draw segment
sprite tailx taily TAILHEIGHT
;
: move-worm
# store current dir for tail
v0 := headdir
queue-put
# remove tail ?
if growth == 0 then remove-segment
if growth != 0 then growth += -1
# i = worm-dirtab + 8 * headdir
i := worm-dirtab
v0 <<= headdir
v0 <<= v0
v0 <<= v0
i += v0
# move worm
load v2 # i now points at the sprite
headx += v0
heady += v1
# draw segment
sprite headx heady TAILHEIGHT
if vf == 1 then collision
;
: title
i := title-MrW
v0 := 16
v1 := 9
sprite v0 v1 0
i := title-orm
v0 += 16
sprite v0 v1 0
vF := key
;
: main
title
init
loop
v0 := 7
if v0 key then headdir += -1
v0 := 9
if v0 key then headdir += 1
if headdir == NDIRS then headdir := 0
if headdir == -1 then headdir := LASTDIR
move-worm
again
: queue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment