diff --git a/addfood.asm b/addfood.asm index 6ef1d3a..d361361 100644 --- a/addfood.asm +++ b/addfood.asm @@ -3,5 +3,87 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;; addfood: .scope +.data +.space seed 6 +.text + lda #field + sta _ptr + 1 + + `propagate seed, $c3 + `propagate seed+1, $9e + `propagate seed+2, $26 + `propagate seed, seed+1 + `propagate seed+1, seed+2 + `propagate seed+2, seed+3 + + lda seed + 5 + ldx seed + 4 + sta seed + 4 + lda seed + 3 + stx seed + 3 + ldx seed + 2 + sta seed + 2 + lda seed + 1 + stx seed + 1 + ldx seed + sta seed + stx seed + 5 + + lda seed + tay + lda seed + 1 + and #$03 + beq + + clc + adc _ptr + 1 + sta _ptr + 1 +* cmp #$03 + bne + + tya + cmp #$c0 + bmi + + sbc #$c0 + tay +* lda (_ptr), y + iny + cmp #csnk + beq - + cmp #cblk + beq - + lda #csnk + dey + sta (_ptr), y rts -.scend \ No newline at end of file +.scend + +srand: ; 做随机种子的初始化 +.scope + jsr rdtim + sta seed + 5 + stx seed + 3 + sty seed + 1 + rts +.scend + +;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; propagate 进位 +;;;;;;;;;;;;;;;;;;;;;;;;;;;; +.macro propagate + lda _1 + clc + adc _2 + bcc _end + inc _1 + 1 + bne _end + inc _1 + 2 + bne _end + inc _1 + 3 + bne _end + inc _1 + 4 + bne _end + inc _1 + 5 +_end: + sta _1 +.macend \ No newline at end of file diff --git a/delay.asm b/delay.asm index c51ee64..a573d9a 100644 --- a/delay.asm +++ b/delay.asm @@ -1,7 +1,7 @@ ; DELAY routine. Takes values from the Accumulator and pauses ; for that many jiffies (1/60th of a second). .scope -.data zp +.data .space _tmp 1 .space _target 1 .text diff --git a/head.asm b/head.asm index 19bca85..2b5f825 100644 --- a/head.asm +++ b/head.asm @@ -17,10 +17,11 @@ .space d 1 ; 方向 值定义如上 .space c 1 ; 🐍长度 最大255 最小0 .space s 2 ; 得分 小端序 +.space _ptr 2 ; 通用指针 + +.alias title $0400 +.alias field $0428 ; 蛇所在屏幕内存区 .data -.org $0400 -.space title 40 -.space field 960 ; 蛇所在屏幕内存区 - +.org $c000 .text \ No newline at end of file diff --git a/main.asm b/main.asm index 7c5eafa..26a9e29 100644 --- a/main.asm +++ b/main.asm @@ -5,6 +5,9 @@ main: .scope +.data +.space lopcnt 1 ; 循环次数计数 +.text `init jsr getchar ; 等待输入任意字符开始游戏 jsr erasehint ; 游戏开始,清空提示 @@ -21,7 +24,7 @@ main: jsr append * jsr calcscore ; 计算得分 jsr printscore ; 打印分数 - lda #60 + lda #32 jsr delay ; 延时期间最后一个按键位于d lda d beq ++ @@ -29,7 +32,13 @@ main: bne + rts * sta d -* jsr addfood +* inc lopcnt + lda lopcnt + cmp #16 + bmi ----- + lda #0 + sta lopcnt + jsr addfood jmp ----- .scend @@ -51,6 +60,7 @@ main: jsr printhint ; 打印开始提示 lda #go_d ; 初始化方向为下 sta d + jsr srand ; 初始化随机数种子 .macend .require "printscore.asm" @@ -66,8 +76,8 @@ main: .require "calcscore.asm" .require "print.asm" -.checkpc $A000 ; text段边界 +.checkpc $a000 ; text段边界 .data zp ; 零页段边界 .checkpc $80 .data -.checkpc $0800 ; data段边界 \ No newline at end of file +.checkpc $d000 ; data段边界 \ No newline at end of file diff --git a/print.asm b/print.asm index 93db005..a18fdd3 100644 --- a/print.asm +++ b/print.asm @@ -13,9 +13,6 @@ ; PRINTSTR routine. Accumulator stores the low byte of the address, ; X register stores the high byte. .scope -.data zp -.space _ptr 2 -.text printstr: sta _ptr sty _ptr+1 diff --git a/print16.asm b/print16.asm index 9da3aeb..af2cd39 100644 --- a/print16.asm +++ b/print16.asm @@ -4,7 +4,7 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; print16: .scope -.data zp +.data .space _num 4 .space _num_dec 6 .text diff --git a/printfield.asm b/printfield.asm index 92b4e7b..5e2b8a8 100644 --- a/printfield.asm +++ b/printfield.asm @@ -3,9 +3,6 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; printfield: .scope -.data zp -.space _ptr 2 -.text ldx #23 ; i代表行数,不含边框 lda #