From f716081ad7c113baa6890f0b5b99240c08a9e733 Mon Sep 17 00:00:00 2001 From: fumiama Date: Sat, 3 Apr 2021 19:34:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A1=86=E6=9E=B6=E6=9E=84=E5=BB=BA=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addfood.asm | 7 ++++++ append.asm | 9 ++++++++ calcscore.asm | 8 +++++++ delay.asm | 23 +++++++++++++++++++ getchar.asm | 4 ++++ head.asm | 16 ++++++++----- hint.asm | 24 ++++++++++++++++++++ judge.asm | 19 ++++++++++++++++ main.asm | 40 ++++++++++++++++++++++++++++++--- move.asm | 7 ++++++ print.asm | 20 ++++++++--------- print16.asm | 51 +++++++++++++++++------------------------ printbyte.asm | 38 +++++++++++++++---------------- printfield.asm | 60 ++++++++++++++++++++++++------------------------- printinput.asm | 18 +++++++-------- printscore.asm | 6 ++++- snake.prg | Bin 527 -> 739 bytes 17 files changed, 241 insertions(+), 109 deletions(-) create mode 100644 addfood.asm create mode 100644 append.asm create mode 100644 calcscore.asm create mode 100644 delay.asm create mode 100644 getchar.asm create mode 100644 hint.asm create mode 100644 judge.asm create mode 100644 move.asm diff --git a/addfood.asm b/addfood.asm new file mode 100644 index 0000000..6ef1d3a --- /dev/null +++ b/addfood.asm @@ -0,0 +1,7 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; addfood 随机生成食物 +;;;;;;;;;;;;;;;;;;;;;;;;;;;; +addfood: +.scope + rts +.scend \ No newline at end of file diff --git a/append.asm b/append.asm new file mode 100644 index 0000000..8788428 --- /dev/null +++ b/append.asm @@ -0,0 +1,9 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; append 增加蛇长 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +append: +.scope + inc c ; 蛇长加一 + ; 其他代码 + rts +.scend \ No newline at end of file diff --git a/calcscore.asm b/calcscore.asm new file mode 100644 index 0000000..ee8733e --- /dev/null +++ b/calcscore.asm @@ -0,0 +1,8 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; calcscore 计算分数,存储到s +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +calcscore: +.scope + inc s + rts +.scend \ No newline at end of file diff --git a/delay.asm b/delay.asm new file mode 100644 index 0000000..c51ee64 --- /dev/null +++ b/delay.asm @@ -0,0 +1,23 @@ +; DELAY routine. Takes values from the Accumulator and pauses +; for that many jiffies (1/60th of a second). +.scope +.data zp +.space _tmp 1 +.space _target 1 +.text + +delay: sta _tmp ; save argument (rdtim destroys it) + jsr rdtim + clc + adc _tmp ; add current time to get target + sta _target +* jsr rdtim + cmp _target + php + jsr getin + beq + + sta d +* plp + bmi -- ; Buzz until target reached + rts +.scend diff --git a/getchar.asm b/getchar.asm new file mode 100644 index 0000000..ffaf989 --- /dev/null +++ b/getchar.asm @@ -0,0 +1,4 @@ +getchar: +* jsr getin + beq - + rts \ No newline at end of file diff --git a/head.asm b/head.asm index f21dc37..19bca85 100644 --- a/head.asm +++ b/head.asm @@ -2,12 +2,16 @@ .alias csnk $a0 ; 像素 .alias csps $20 ; 空白 -.alias go_u $55 ; 上 -.alias go_d $5f ; 下 -.alias go_l $1d ; 左 -.alias go_r $32 ; 右 -.alias st_g $a0 ; 开始/暂停 -.alias ed_g $20 ; 结束 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 按钮配置,调试时使用WASD控制 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +.alias go_u 'W ;$55 ; 上 +.alias go_d 'S ;$5f ; 下 +.alias go_l 'A ;$1d ; 左 +.alias go_r 'D ;$32 ; 右 +.alias st_g 'P ;$a0 ; 开始/暂停 +.alias ed_g 'Q ;$20 ; 结束 .data zp .space d 1 ; 方向 值定义如上 diff --git a/hint.asm b/hint.asm new file mode 100644 index 0000000..24e270a --- /dev/null +++ b/hint.asm @@ -0,0 +1,24 @@ +printhint: + clc + ldx #20 ; 光标来到正确位置 + ldy #8 + jsr plot + `print starthint + rts +printfail: + clc + ldx #20 ; 光标来到正确位置 + ldy #8 + jsr plot + `print failhint + rts +erasehint: + ldy #25 + lda #csps +* sta [title+20*40+8-1], y + dey + bne - + rts + +starthint: .byte "PRESS ANY KEY TO START", 0 +failhint: .byte "FAILED:PRESS TO RETURN", 0 \ No newline at end of file diff --git a/judge.asm b/judge.asm new file mode 100644 index 0000000..1e27090 --- /dev/null +++ b/judge.asm @@ -0,0 +1,19 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; judgeout 判断是否出界/撞到自身 +; 是->sec 即进位标志置1 +; 否->clc 即进位标志置0 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +judgeout: +.scope + rts +.scend + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; judgefood 判断是否吃到食物 +; 是->sec 即进位标志置1 +; 否->clc 即进位标志置0 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +judgefood: +.scope + rts +.scend \ No newline at end of file diff --git a/main.asm b/main.asm index 259af73..7e009bd 100644 --- a/main.asm +++ b/main.asm @@ -6,8 +6,31 @@ main: .scope `init - + jsr getchar ; 等待输入任意字符开始游戏 + jsr erasehint ; 游戏开始,清空提示 +* jsr move ; 蛇移动一格 + clc + jsr judgeout ; 判断是否出界 + bcc + + jsr printfail + jsr getchar rts + clc +* jsr judgefood ; 判断是否吃到食物 + bcc + + jsr append +* jsr calcscore ; 计算得分 + jsr printscore ; 打印分数 + lda #60 + jsr delay ; 延时期间最后一个按键位于d + lda d + beq ++ + cmp #ed_g + bne + + rts +* sta d +* jsr addfood + jmp ----- .scend ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -21,15 +44,26 @@ main: lda #$00 sta s + 1 ; 初始化分数为0 jsr printscore ; 打印分数 - lda #1 - sta field + 11*38 + 19 ; 初始化蛇位置 + lda #csnk + sta field + 11*40 + 19 ; 初始化蛇位置 sta c ; 初始化蛇长为1 jsr printfield ; 打印蛇,包括边框 + jsr printhint ; 打印开始提示 + lda #go_d ; 初始化方向为下 + sta d .macend .require "printscore.asm" .require "print16.asm" .require "printfield.asm" +.require "hint.asm" +.require "getchar.asm" +.require "addfood.asm" +.require "append.asm" +.require "delay.asm" +.require "judge.asm" +.require "move.asm" +.require "calcscore.asm" .require "print.asm" .checkpc $A000 ; text段边界 diff --git a/move.asm b/move.asm new file mode 100644 index 0000000..32b33ab --- /dev/null +++ b/move.asm @@ -0,0 +1,7 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; move 根据d中的值实现蛇的移动 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +move: +.scope + rts +.scend \ No newline at end of file diff --git a/print.asm b/print.asm index e8df889..93db005 100644 --- a/print.asm +++ b/print.asm @@ -2,9 +2,9 @@ pha tya pha - lda #<_1 ; 取参数的低八位 - ldy #>_1 ; 取参数的高八位 - jsr printstr + lda #<_1 ; 取参数的低八位 + ldy #>_1 ; 取参数的高八位 + jsr printstr pla tay pla @@ -17,15 +17,15 @@ .space _ptr 2 .text printstr: - sta _ptr - sty _ptr+1 - ldy #$00 + sta _ptr + sty _ptr+1 + ldy #$00 _lp: lda (_ptr),y - beq _done - jsr chrout - iny - bne _lp + beq _done + jsr chrout + iny + bne _lp _done: rts .scend \ No newline at end of file diff --git a/print16.asm b/print16.asm index 66a67b3..1b83c80 100644 --- a/print16.asm +++ b/print16.asm @@ -14,7 +14,7 @@ print16: lda #0 ldx #6 -* sta _num_dec-1, x +* sta _num_dec-1, x dex bne - @@ -32,26 +32,23 @@ print16: ; mod函数处理的数据为打印方便均使用大端序存储 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -mod_4: ; 高字节低4位若大于10,则向高位进1 - ;jsr chrin +mod_4: ; 高字节低4位若大于10,则向高位进1 lda _num + 3 cmp #10 bcc + - sbc #10 ; 此时c为1 + sbc #10 ; 此时c为1 tax lda #1 - sta _num_dec + 3 ; 进位 - ;jsr chrin + sta _num_dec + 3 ; 进位 txa -* sta _num_dec + 4 - ;jsr chrin +* sta _num_dec + 4 rts -mod_3: ; 高字节高4位的值相当于个位加6,十位加1 +mod_3: ; 高字节高4位的值相当于个位加6,十位加1 ldx _num + 2 - bne + ; 如果为0直接返回 + bne + ; 如果为0直接返回 rts -* lda #6 +* lda #6 `carry10 4 lda #1 `carry10 3 @@ -59,11 +56,11 @@ mod_3: ; 高字节高4位的值相当于个位加6,十位加1 bne - rts -mod_2: ; 低字节低4位的值相当于个位加6,十位加5,百位加2 +mod_2: ; 低字节低4位的值相当于个位加6,十位加5,百位加2 ldx _num + 1 - bne + ; 如果为0直接返回 + bne + ; 如果为0直接返回 rts -* lda #6 +* lda #6 `carry10 4 lda #5 `carry10 3 @@ -73,11 +70,11 @@ mod_2: ; 低字节低4位的值相当于个位加6,十位加5,百位加 bne - rts -mod_1: ; 低字节低4位的值相当于个位加6,十位加9,百位加0,千位加4 +mod_1: ; 低字节低4位的值相当于个位加6,十位加9,百位加0,千位加4 ldx _num - bne + ; 如果为0直接返回 + bne + ; 如果为0直接返回 rts -* lda #6 +* lda #6 `carry10 4 lda #9 `carry10 3 @@ -94,48 +91,44 @@ mod_1: ; 低字节低4位的值相当于个位加6,十位加9,百位加 adc _num_dec + _1 bcc _skip pha - lda #$10 ; 进位相当于100 + lda #$10 ; 进位相当于100 clc adc _num_dec + _1 - 1 sta _num_dec + _1 - 1 pla _skip: sta _num_dec + _1 - ;jsr chrin .macend .macro splitbyte lda _1 sta _num + _2 - ;jsr printbyte lsr lsr lsr lsr sta _num + _2 - 1 - ;jsr printbyte lda #$0f and _num + _2 sta _num + _2 - ;jsr printbyte .macend .macro carry tya pha - sed ; 设置为bcd加减法 + sed ; 设置为bcd加减法 jsr mod_4 jsr mod_3 jsr mod_2 - jsr mod_1 ; 此时结果中有些位可能大于10,需要进行进位处理 + jsr mod_1 ; 此时结果中有些位可能大于10,需要进行进位处理 ldx #5 _loop: - ldy #0 ; y记录进位数 + ldy #0 ; y记录进位数 txa lda _num_dec-1, x cmp #10 - bcc _skip ; 小于10不进位 + bcc _skip ; 小于10不进位 pha and #$f0 lsr @@ -151,11 +144,11 @@ _skip: dex bne _loop - cld ; 退出bcd模式 + cld ; 退出bcd模式 ldx #5 _up: lda _num_dec - 1, x - ora #$30 ; 转化为可显示字符 + ora #$30 ; 转化为可显示字符 sta _num_dec - 1, x dex bne _up @@ -163,5 +156,3 @@ _up: pla tay .macend - -; .require "printbyte.asm" \ No newline at end of file diff --git a/printbyte.asm b/printbyte.asm index 7da63c8..5677371 100644 --- a/printbyte.asm +++ b/printbyte.asm @@ -2,22 +2,22 @@ .space _na 1 ; a的临时存放处 .text printbyte: - php - pha - sta _na - txa - pha - ldx #8 ; 打印8bit -* lda #$30 ; a = '0' - asl _na ; 左移一位,溢出到c - adc #0 ; a = a + c + 0 - jsr chrout ; putchar(a) - dex ; x-- - bne - ; if(x != 0) goto 上个星号 - lda #$20 - jsr chrout - pla - tax - pla - plp - rts \ No newline at end of file + php + pha + sta _na + txa + pha + ldx #8 ; 打印8bit +* lda #$30 ; a = '0' + asl _na ; 左移一位,溢出到c + adc #0 ; a = a + c + 0 + jsr chrout ; putchar(a) + dex ; x-- + bne - ; if(x != 0) goto 上个星号 + lda #$20 + jsr chrout + pla + tax + pla + plp + rts \ No newline at end of file diff --git a/printfield.asm b/printfield.asm index a8e2715..92b4e7b 100644 --- a/printfield.asm +++ b/printfield.asm @@ -6,37 +6,35 @@ printfield: .data zp .space _ptr 2 .text - ldx #23 ; i代表行数,不含边框 - lda #field ; 取地址高8位 - sta _ptr + 1 - ldy #0 -* lda #cblk - sta (_ptr), y ; 打印左边框 - lda #39 - jsr _addptr - lda #cblk - sta (_ptr), y ; 打印右边框 - lda #1 - jsr _addptr - dex - bne - - lda #cblk - ldy #40 - dec _ptr -* sta (_ptr), y ; 打印底边 - dey - bne - - ldx #0 - jsr plot - rts + ldx #23 ; i代表行数,不含边框 + lda #field ; 取地址高8位 + sta _ptr + 1 + ldy #0 +* lda #cblk + sta (_ptr), y ; 打印左边框 + lda #39 + jsr _addptr + lda #cblk + sta (_ptr), y ; 打印右边框 + lda #1 + jsr _addptr + dex + bne - + lda #cblk + ldy #40 + dec _ptr +* sta (_ptr), y ; 打印底边 + dey + bne - + rts _addptr: - clc - adc _ptr - bcc + - inc _ptr + 1 -* sta _ptr - rts + clc + adc _ptr + bcc + + inc _ptr + 1 +* sta _ptr + rts .scend diff --git a/printinput.asm b/printinput.asm index 30266ac..f9a9e0d 100644 --- a/printinput.asm +++ b/printinput.asm @@ -3,16 +3,16 @@ .require "platform/c64kernal.oph" main: -* jsr getin - beq skip - cmp #'@ - beq end - jsr printbyte - lda #13 ; 换行 - jsr chrout +* jsr getin + beq skip + cmp #'@ + beq end + jsr printbyte + lda #13 ; 换行 + jsr chrout skip: - jmp - + jmp - end: - rts + rts .require "printbyte.asm" \ No newline at end of file diff --git a/printscore.asm b/printscore.asm index f418c3a..732ca8b 100644 --- a/printscore.asm +++ b/printscore.asm @@ -3,13 +3,17 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; printscore: .scope + clc + ldx #0 ; 光标回到原点 + ldy #0 + jsr plot lda #cblk ldx #15 * jsr chrout dex bne - `print score_str - jsr print16 + jsr print16 ldx #15 * jsr chrout dex diff --git a/snake.prg b/snake.prg index d9b986cfe5b6b6564bdea34bf3e4aa432a592415..1cfbcec14d0101ee696e79132fcc56beec318485 100644 GIT binary patch delta 356 zcmY+9u}cDB7{sHMq8S%h4r-XgTV{NVM)Q%BgQrT!O|nmp*W)yxVO15R5)3 zs6V1GtvDMTTMCz!gP=i&>k}#HSzh=(JkP`1jJ!vk{dz~??CdK_!w7&naEr=#33v$j z3>x;qQE(3gAsRk`6UH0B7eJ9(!&~Uce(Z6_3lR~zhQg7ewIwE@AMk2>9$$l=H_o^6 z++zw3pmtTmq4LX#wO%p}$vw|NF^&guJdI=D?XJiJaD!nz3%u&8JcL>3d7r$>(hsVu zHoOVZDyk!o7N|c~`1XM7?wiV&JQS!B{( M%AXUzcfD`&15SpDrT_o{ delta 129 zcmV-{0Dk}D1&;)fSSDl$sR51M1%(43!3kigW}*)u(*MfP`bd~asfVBlApHqwsAwRK zk;gy^1qmP)2_QoWAcB(-0W|{s36n?x);jYEsb-N7sR1DK3Chsxsb-)k#t@MZh|v3@ j03h)HU>Ic(kOJlrg%Dtc5`+_=0I?GA1|ZV^$k6s+0Q@yp