Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Snake ;;
- ;; a game by Siapran ;;
- ;; 16.10.2012 - 18.10.2012 ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- :start ;used to loop the game
- set [frontx], 16 ;initialise game
- set [fronty], 8
- set [frontdir], 2
- set [backx], 10
- set [backy], 8
- set [backdir], 2
- set [applex], 0x0
- set [appley], 0x0
- set [score], 3
- set [atelength], 0
- jsr clearscreen
- set a, 0 ;print greetings message
- set b, GreetingsMessage
- set c, 0xf000
- jsr printstr
- set a, 0
- :wait4keyloop ;wait for user to press a key
- add a, 1
- ife [0x9000], 0
- set pc, wait4keyloop
- set [seed], [0x9000] ;use key data to make a random seed
- add [seed], a
- set [0x9000], 0 ;reinit keyboard buffer
- jsr clearscreen ;clear screen
- set [0x8110], 0xFF02 ;initialise starting snake
- set [0x810F], 0xFF02
- set [0x810E], 0xFF02
- set [0x810D], 0xFF02
- set [0x810C], 0xFF02
- set [0x810B], 0xFF02
- :generatefirstapple ;generate the first apple
- set a, 0
- set b, 15
- jsr pseudorandom ;random x
- mul a, 2
- set [applex], a
- set a, 0
- jsr pseudorandom ;random y
- set [appley], a
- set b, [applex]
- mul b, 32
- add a, b
- ifn [0x8000+a], 0 ;check if apple appears in empty space
- set pc, generatefirstapple ; if not, try again
- :mainloop ;main game loop
- set a, [applex]
- set b, [appley]
- set c, 0xCC00
- jsr printpixel ;draw apple
- set a, [backx]
- set b, [backy]
- set c, 0
- jsr printpixel ;erase back of snake
- set a, [frontx]
- set b, [fronty]
- set c, 0xFF00
- jsr printpixel ;draw front of snake
- set a, 0
- set b, 0
- set [0x9000], 0 ;reinit key buffer
- :getkeyloop ;getkey loop (+speed regulation)
- add a, 1
- ifn [0x9000], 0
- set b, [0x9000] ;keycode stored in B
- ifn a, [gamespeed]
- set pc, getkeyloop
- ifn b, 1 ;if key=left
- set pc, leftcond
- ife [frontdir], 2 ;and if key!=right
- set pc, leftcond
- set [frontdir], b ;set direction to left
- :leftcond
- ifn b, 2 ;if key=right
- set pc, rightcond
- ife [frontdir], 1 ;same
- set pc, rightcond
- set [frontdir], b
- :rightcond
- ifn b, 3 ;if key=up
- set pc, upcond
- ife [frontdir], 4 ;same
- set pc, upcond
- set [frontdir], b
- :upcond
- ifn b, 4 ;if key=down
- set pc, downcond
- ife [frontdir], 3 ;same
- set pc, downcond
- set [frontdir], b
- :downcond
- set a, [fronty]
- mul a, 32
- add a, [frontx]
- bor [0x8000+a], [frontdir] ;record direction on matrix
- set b, [frontdir] ;move the front of the snake
- ife b, 1
- sub [frontx], 2
- ife b, 2
- add [frontx], 2
- ife b, 3
- sub [fronty], 1
- ife b, 4
- add [fronty], 1
- mod [frontx], 32 ;check collisions with borders of screen
- mod [fronty], 16
- ifn [atelength], 0 ;if nothing is eaten
- set pc, movebackelse ; then move the back
- set a, [backy]
- mul a, 32
- add a, [backx]
- and [0x8000+a], 0xFF00 ;erase on matrix at pos x,y
- set b, [backdir] ;same as front
- ife b, 1
- sub [backx], 2
- ife b, 2
- add [backx], 2
- ife b, 3
- sub [backy], 1
- ife b, 4
- add [backy], 1
- mod [backx], 32
- mod [backy], 16
- set a, [backy]
- mul a, 32
- add a, [backx]
- set [backdir], [0x8000+a] ;grab new diretion left by front on matrix
- and [backdir], 0xFF
- set pc, movebackend
- :movebackelse
- sub [atelength], 1 ; else digest
- :movebackend
- ifn [frontx], [applex] ;if front on apple
- set pc, eatapplecond
- ifn [fronty], [appley]
- set pc, eatapplecond
- add [atelength], 1 ;eat it
- add [score], 1 ;increment score
- :newapplegen ;spawn new apple
- set a, 0
- set b, 15
- jsr pseudorandom
- mul a, 2
- set [applex], a
- set a, 0
- jsr pseudorandom
- set [appley], a
- set b, [applex]
- mul b, 32
- add a, b
- ifn [0x8000+a], 0
- set pc, newapplegen
- :eatapplecond
- set a, [fronty]
- mul a, 32
- add a, [frontx]
- set b, [0x8000+a]
- and b, 0xFF
- ife b, 0 ;check for collision
- set pc, mainloop ;reiterate while snake didn't bite itself
- set a, 0 ;print score
- set b, gameover
- set c, 0xF000
- jsr printstr
- set a, [score] ;convert score to srt
- set b, scorestr
- jsr reg2str
- set a, 6
- set c, 0xF000
- jsr printstr
- :wait4keyenter ;wait for user to press ENTER
- set [0x9000], 0
- :wait4keyenterloop
- ife [0x9000], 0
- set pc, wait4keyenterloop
- ifn [0x9000], 0xa
- set pc, wait4keyenter
- set pc, start ;new game
- set pc, end ;in case of hypothetical crash
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; prints a massive 8*8 pixel
- ;; parameters
- ;; a - x
- ;; b - y
- ;; c - color (0xCC00 format)
- ;; returns
- ;; none
- :printpixel
- mul b, 32
- add a, b
- and [0x8000+a], 0xFF
- bor [0x8000+a], c
- add a, 1
- and [0x8000+a], 0xFF
- bor [0x8000+a], c
- set pc, pop
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Generate a random number
- ;; parameters
- ;; A - minimum
- ;; B - maximum
- ;; returns
- ;; A - The random number
- ;; notes
- ;; - Uses [seed] for the random seed and updates
- ;; it with the last un-modulized number generated.
- :pseudorandom
- set push, x
- set push, i
- set x, [seed]
- mul x, 0xe3d1
- add x, 0x2b69
- shr x, 3
- set [seed], x ; new seed
- set i, b
- sub i, a
- mod x, i
- add x, a
- set a, x ; random number
- set i, pop
- set x, pop
- set pc, pop
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Prints a string on the screen
- ;; parameters
- ;; A - relative position on screen (0x8000+A)
- ;; B - pointer to str
- ;; C - color (0xCC00)
- ;; returns
- ;; A - position of cursor on screen
- :printstr
- set [0x8000+a], [b] ;print char on screen
- bor [0x8000+a], c ;add color
- add a, 1 ;increment screen position
- add b, 1 ;increment pointer
- ifn [b], 0
- set pc, printstr ;loopwhile not at the end of the chain
- set pc, pop ;end of function
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Clears screen
- ;; parameters
- ;; none
- ;; returns
- ;; your porn (good guy)
- :clearscreen
- set push, a
- set a, 0x8000
- :clearscreenloop
- set [a], 0x0
- add a, 1
- ifn a, 0x8200
- set pc, clearscreenloop
- set a, pop
- set pc, pop
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Register to String
- ;; parameters
- ;; A - register
- ;; B - pointer to new str
- ;; returns
- ;; B - pointer to str
- :reg2str
- set push, c
- set push, b
- add b, 5
- :reg2strloop
- set c, a
- mod c, 10
- add c, 0x30
- sub b, 1
- set [b], c
- div a, 10
- ifn b, peek
- set pc, reg2strloop
- set b, pop
- set c, pop
- set pc, pop
- ;==========================================;
- ; Variables ;
- ;==========================================;
- :GreetingsMessage dat "SNAKE code by SiapranPRESS ANY KEY", 0x0
- :gameover dat "SCORE:", 0x0
- :seed dat 0x0
- :front
- :frontx dat 16
- :fronty dat 8
- :frontdir dat 2
- :back dat "ass"
- :backx dat 10
- :backy dat 8
- :backdir dat 2
- :applex dat 0x0
- :appley dat 0x0
- :score dat 3
- :atelength dat 0
- :gamespeed dat 0x500
- :scorestr dat "00000", 0x0
- :end set PC, end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement