diff --git a/README.md b/README.md index bfc69d9..f29c8f8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # c64-snake -C64上的贪吃蛇 +在Commodore64上实现简单的贪吃蛇游戏。 + +# 说明 +1. 如果想要直接游玩,只需要下载`snake.prg`文件,在模拟器上运行即可。 +2. 如果想进行修改,你需要下载[ophis](https://github.com/fumiama/c64-ophis-lib),并了解它的独特语法,以理解程序代码。 diff --git a/addfood.asm b/addfood.asm index 6ef1d3a..89f26af 100644 --- a/addfood.asm +++ b/addfood.asm @@ -3,5 +3,89 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;; 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 - + cmp #crnd + beq - + lda #crnd + 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/append.asm b/append.asm deleted file mode 100644 index 8788428..0000000 --- a/append.asm +++ /dev/null @@ -1,9 +0,0 @@ -;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; append 增加蛇长 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -append: -.scope - inc c ; 蛇长加一 - ; 其他代码 - rts -.scend \ No newline at end of file diff --git a/head.asm b/head.asm index 19bca85..9c59492 100644 --- a/head.asm +++ b/head.asm @@ -1,6 +1,7 @@ .alias cblk $66 ; 边框 .alias csnk $a0 ; 像素 .alias csps $20 ; 空白 +.alias crnd $51 ; 食物 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 按钮配置,调试时使用WASD控制 @@ -17,10 +18,13 @@ .space d 1 ; 方向 值定义如上 .space c 1 ; 🐍长度 最大255 最小0 .space s 2 ; 得分 小端序 +.space _ptr 2 ; 通用指针 +.space eat 1 ; 吃到食物标记 +.space shead 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..c9a6ede 100644 --- a/main.asm +++ b/main.asm @@ -8,29 +8,31 @@ main: `init jsr getchar ; 等待输入任意字符开始游戏 jsr erasehint ; 游戏开始,清空提示 -* jsr move ; 蛇移动一格 - clc +* clc jsr judgeout ; 判断是否出界 bcc + jsr printfail jsr getchar rts - clc -* jsr judgefood ; 判断是否吃到食物 +* clc + lda #0 + sta eat + jsr judgefood ; 判断是否吃到食物 bcc + - jsr append -* jsr calcscore ; 计算得分 + inc c ; 蛇长加一 + lda #1 + sta eat + jsr addfood +* jsr move ; 蛇移动一格 + jsr calcscore ; 计算得分 jsr printscore ; 打印分数 - lda #60 + lda #32 jsr delay ; 延时期间最后一个按键位于d lda d - beq ++ cmp #ed_g bne + rts -* sta d -* jsr addfood - jmp ----- +* jmp ---- .scend ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -41,16 +43,19 @@ main: jsr chrout lda #$00 sta s - lda #$00 + sta eat sta s + 1 ; 初始化分数为0 jsr printscore ; 打印分数 lda #csnk sta field + 11*40 + 19 ; 初始化蛇位置 + lda #1 sta c ; 初始化蛇长为1 jsr printfield ; 打印蛇,包括边框 jsr printhint ; 打印开始提示 lda #go_d ; 初始化方向为下 sta d + jsr srand ; 初始化随机数种子 + jsr addfood ; 增加食物 .macend .require "printscore.asm" @@ -59,15 +64,14 @@ main: .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段边界 +.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/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 #