Sprite/movesprite

Aus C64-Wiki
Zur Navigation springenZur Suche springen

<< zurück zu Sprite

Das Assembler-Programm movesprite demonstriert die einfache Erstellung und die Bewegung von Sprites.

;*************************************************
;* create and move a simple sprite               *
;* with coordinates displayed                    *
;* (2021) petrus, based on david roberts program *   
;* at https://gist.github.com/biomood/1001256    *
;*************************************************

	!to "movesprite.prg", cbm
	
;useful rom routines
bsout	=  $e10c
clear 	=  $e544
getin  	=  $ffe4
scnkey 	=  $ff9f

;sprite0 settings
sprite0 = $07f8
color0  = $d027
sp0x	= $d000
sp0y	= $d001
msbx	= $d010
control1= $d011
sp0val	= $0340
enable  = $d015
control2= $d016
yexpand	= $d017
xexpand	= $d01d
priority= $d01b

;colors
color	= $0286
border	= $d020
backgr	= $d021
;other
txbegin = 1108	;textstart 1024+2*40+4 at line 4, column 4

	* = $0801	;BASIC start: 10 SYS 2064
	!byte $0b,$08,$0a,$00,$9e,$32,$30,$36,$34,$00	
	
	* = $0810	;2064
	lda #6  	;blue
	sta backgr
	lda #14 	;lightblue
	sta border
	sta color
	jsr clear
	jsr infos
	
	lda #$0d	;using block 13 for sprite0
	sta sprite0
	
	lda #01 	;enable sprite0
	sta enable
	sta priority    ;sprite0 behind text
	
	lda #7  	;use yellow for sprite0
	sta color0
	
	ldx #0
	lda #0
	
	;reset the spriteval data
cleanup	sta sp0val,x
	inx
	cpx #63
	bne cleanup
	
	;build the sprite
	ldx #0
build	lda data,x
	sta sp0val,x
	inx
	cpx #63
	bne build
	
	jsr init	;sprite0 at 40,120
	
entry   lda #1
        sta $0289	;disable keyboard buffer
        lda #64
        sta $028a	;disable key repeat 

scan	jsr dspxy	
	jsr scnkey	;get key
	jsr getin	;put key in a

start	cmp #145	;crsr-up
	bne cdown
	jmp up
	
cdown	cmp #17 	;crsr-down
	bne cleft
	jmp down
	
cleft	cmp #157	;crsr-left
	bne cright
	jmp left
	
cright	cmp #29 	;crsr-right
	bne home
	jmp right
	
home	cmp #19
	bne plus
	jsr init
	jmp scan
	
plus	cmp #43 	;plus
	bne swscr1
	lda color0
	eor #5  	;toggle 7<->2 (yellow/red)
	sta color0
	lda $028a	;repeat-flag
	eor #64 	;toggle on/off
	sta $028a
	jmp scan
	
swscr1	cmp #87	;W (width)
	bne swscr2
	jsr scrcols     ;toggle 40/38 columns
	jmp scan
	
swscr2	cmp #72 	;H (height)
	bne xexp
	jsr scrlines 	;toggle 25/24 lines
	jmp scan	

xexp	cmp #88 	;X
	bne yexp
	lda xexpand
	eor #1
	sta xexpand
	jmp scan

yexp	cmp #89 	;Y
	bne entr
	lda yexpand
	eor #1
	sta yexpand
	jmp scan
	
entr	cmp #13 	;end if <return> 
	beq end
	jmp scan
	
up	ldy sp0y
	dey
	sty sp0y
	jmp scan

down    ldy sp0y
	iny
	sty sp0y
	jmp scan

left	dec sp0x
	ldx sp0x
	cpx #255
	bne le
	lda msbx
	eor #1
	sta msbx
le	jmp scan

right	inc sp0x
	ldx sp0x
	cpx #0
	bne re
	lda msbx
	eor #1
	sta msbx
re	jmp scan

	;clean up at the end
end	jsr clear
	lda #0
	sta enable
	rts
	
	;position
init	lda #0  	;stick with x0-255
	sta msbx
	;starting sprite location 40,140
	ldx #40
	ldy #140
	stx sp0x
	sty sp0y
	rts
	
dspxy	lda #1  	;white
	sta color
	ldx #12
        ldy #18
	clc
        jsr $fff0	;setcrsr	
	lda msbx
	ldx sp0x
	jsr $bdcd	;dsp 16-bit number
	jsr spcout
	
	ldx #13
        ldy #18
	clc
        jsr $fff0	;setcrsr
	lda #0
	ldx sp0y
	jsr $bdcd	;dsp 16-bit number
	jsr spcout
	lda #14
	sta color
	rts	
	
spcout	lda #$20	;2x space
	jsr bsout
	jsr bsout
	rts
	
scrcols	lda control2
	eor #$08
	sta control2
	rts
	
scrlines lda control1
	eor #$08
	sta control1
	rts	
	
infos	ldx #0
txloop	lda info1,x
	sta txbegin,x
	lda info2,x
	sta txbegin+40,x
	lda info3,x
	sta txbegin+80,x
	lda info4,x
	sta txbegin+120,x
	lda info5,x
	sta txbegin+160,x
	lda info6,x
	sta txbegin+200,x
	lda info7,x
	sta txbegin+240,x
	lda info8,x
	sta txbegin+280,x
	inx
	cpx #31
	bne txloop
	rts
	
info1	!scr "move sprite with cursor        "
info2	!scr "'home' start coordinates       "
info3   !scr "'+' toggle key repeat          "
info4	!scr "'w' toggle screen columns 40/38" 
info5	!scr "'h' toggle screen lines   25/24" 
info6	!scr "'x' x-expand sprite            " 
info7	!scr "'y' y-expand sprite            " 
info8	!scr "'return' end programm          "
	
	;define the sprite
data 	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %##......,%########,%########
	!byte %##......,%########,%########
	!byte %##......,%########,%########
	!byte %##......,%########,%########
	!byte %##......,%########,%########
	!byte %##......,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
	!byte %########,%########,%########
.end