diff --git a/main.asm b/main.asm index 8d42e17..ee018d1 100644 --- a/main.asm +++ b/main.asm @@ -46,13 +46,8 @@ main: sta eat sta s + 1 ; 初始化分数为0 jsr printscore ; 打印分数 - lda #csnk - sta field + 11*40 + 19 ; 初始化蛇位置 - lda #<[field + 11*40 + 19] - ldx #>[field + 11*40 + 19] - sta shead - stx shead+1 - lda #1 + jsr move_init + lda #2 sta c ; 初始化蛇长为1 jsr printfield ; 打印蛇,包括边框 jsr printhint ; 打印开始提示 diff --git a/move.asm b/move.asm index 5fba8da..b81155b 100644 --- a/move.asm +++ b/move.asm @@ -1,86 +1,143 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; move 根据d中的值实现蛇的移动 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -move: + .scope - ;lda #1 ;演示用 +.data zp +.space _stail 2 ; 蛇尾指针 +.space _tmp 1 +.text +move: + ;lda #0 ;演示用 ;sta eat - lda eat ;eat判断,进入两种移动模式 - ;bne +++++ ;进入模式一:四种移动模式(吃到)(头动,尾不动) - lda d + lda eat ;eat判断 + bne _head_move ;吃到食物直接进入模式一:head move,否则先执行tail move + lda #csps + ldy #0 + sta (_stail),y ;tail move + ldy #40 + lda (_stail),y + cmp #csnk ;down search + bne + + lda #40 + jsr _propergate_tail +* ldy #1 + lda (_stail),y + cmp #csnk ;right search + bne + + lda #1 + jsr _propergate_tail +* jsr _copy_to_ptr + lda #40 + sta _tmp + jsr _borrow_ptr + ldy #0 + lda (_ptr),y + cmp #csnk ;up search + bne + + jsr _copy_to_tail +* jsr _copy_to_ptr + lda #1 + sta _tmp + jsr _borrow_ptr + lda (_ptr),y + cmp #csnk ;left search + bne _head_move + jsr _copy_to_tail +;;;;;;;;;;;;;;;;;;;;;;;;; +; head move +;;;;;;;;;;;;;;;;;;;;;;;;; +_head_move: ;lda #go_l ;测试用 - cmp #go_d - bne + - clc - lda shead ;new head - adc #40 - sta shead - lda shead+1 - adc #0 - sta shead+1 - ldy #0 ;head move down - lda #csnk - sta (shead),y + `_m_judge_dir_head go_d, 40, _propergate_head + `_m_judge_dir_head go_r, 1, _propergate_head + `_m_judge_dir_head go_l, 1, _borrow_head + `_m_judge_dir_head go_u, 40, _borrow_head rts -* cmp #go_r - bne + - clc - lda shead ;new head - adc #1 - sta shead - lda shead+1 - adc #0 - sta shead+1 - ldy #0 ;head move right + +move_init: lda #csnk - sta (shead),y + sta field + 10*40 + 19 ; 初始化蛇位置 + sta field + 11*40 + 19 ; 初始化蛇位置 + lda #<[field + 11*40 + 19] ;初始化蛇头 + ldx #>[field + 11*40 + 19] + sta shead + stx shead+1 + lda #<[field + 10*40 + 19] ;初始化蛇尾 + ldx #>[field + 10*40 + 19] + sta _stail + stx _stail+1 rts -* cmp #go_l - bne + + +_propergate_tail: + `_m_propergate _stail + +_propergate_head: + `_m_propergate shead + +;;;;;;;;;;;;;;;;;;;;;;;; +; 减法借位,入参为_tmp +;;;;;;;;;;;;;;;;;;;;;;;; +_borrow_ptr: + `_m_borrow _ptr + +_borrow_head: + `_m_borrow shead + +_copy_to_ptr: + lda _stail + sta _ptr + lda _stail+1 + sta _ptr+1 + rts + +_copy_to_tail: + lda _ptr + sta _stail + lda _ptr+1 + sta _stail+1 + rts +.scend + +;;;;;;;;;;;;;;;;;;;;;;;; +; 加法进位,入参为a +;;;;;;;;;;;;;;;;;;;;;;;; +.macro _m_propergate clc - lda shead ;new head + adc _1 + sta _1 + bcc _end + inc _1+1 +_end: + rts +.macend + +;;;;;;;;;;;;;;;;;;;;;;;; +; _tmp:减数 _1:被减数 +;;;;;;;;;;;;;;;;;;;;;;;; +.macro _m_borrow + lda _1 sec - sbc #1 - sta shead - lda shead+1 - sbc #0 - sta shead+1 - ldy #0 ;head move left - lda #csnk - sta (shead),y + sbc _tmp + sta _1 + bcs _end + dec _1+1 +_end: rts -* cmp #go_u - bne + - clc - lda shead ;new head - sec - sbc #40 - sta shead - lda shead+1 - sbc #0 - sta shead+1 - ldy #0 ;head move up +.macend + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; _1:方向 _2:进位/借位数 _3:进位/借位函数 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +.macro _m_judge_dir_head + lda d + cmp #_1 + bne _next + lda #_2 + sta _tmp + jsr _3 lda #csnk - sta (shead),y -* rts -;模式二:四种移动模式 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; lda d -; lda #go_d ;实验用 -; cmp go_d -; beq + -; lda #csps -; ldy #0 -; sta (shead),y -; lda #csnk -; ldy #40 -; sta (shead),y -; clc -; lda shead -; adc #40 -; sta shead -; lda shead+1 -; adc #0 -; sta shead+1 -;* rts -.scend \ No newline at end of file + sta (shead),y + rts +_next: +.macend \ No newline at end of file diff --git a/snake.prg b/snake.prg index 28bdec9..96e6db2 100644 Binary files a/snake.prg and b/snake.prg differ