1
0
mirror of https://github.com/fumiama/c64-snake.git synced 2026-06-30 08:50:34 +08:00

框架构建完成

This commit is contained in:
fumiama
2021-04-03 19:34:32 +08:00
parent c21a2a1efb
commit f716081ad7
17 changed files with 241 additions and 109 deletions

7
addfood.asm Normal file
View File

@@ -0,0 +1,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; addfood 随机生成食物
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
addfood:
.scope
rts
.scend

9
append.asm Normal file
View File

@@ -0,0 +1,9 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; append 增加蛇长
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
append:
.scope
inc c ; 蛇长加一
; 其他代码
rts
.scend

8
calcscore.asm Normal file
View File

@@ -0,0 +1,8 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; calcscore 计算分数存储到s
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
calcscore:
.scope
inc s
rts
.scend

23
delay.asm Normal file
View File

@@ -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

4
getchar.asm Normal file
View File

@@ -0,0 +1,4 @@
getchar:
* jsr getin
beq -
rts

View File

@@ -2,12 +2,16 @@
.alias csnk $a0 ; 像素 .alias csnk $a0 ; 像素
.alias csps $20 ; 空白 .alias csps $20 ; 空白
.alias go_u $55 ; 上 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.alias go_d $5f ; 下 ; 按钮配置调试时使用WASD控制
.alias go_l $1d ; 左 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.alias go_r $32 ; 右
.alias st_g $a0 ; 开始/暂停 .alias go_u 'W ;$55 ; 上
.alias ed_g $20 ; 结束 .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 .data zp
.space d 1 ; 方向 值定义如上 .space d 1 ; 方向 值定义如上

24
hint.asm Normal file
View File

@@ -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

19
judge.asm Normal file
View File

@@ -0,0 +1,19 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; judgeout 判断是否出界/撞到自身
; 是->sec 即进位标志置1
; 否->clc 即进位标志置0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
judgeout:
.scope
rts
.scend
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; judgefood 判断是否吃到食物
; 是->sec 即进位标志置1
; 否->clc 即进位标志置0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
judgefood:
.scope
rts
.scend

View File

@@ -6,8 +6,31 @@
main: main:
.scope .scope
`init `init
jsr getchar ; 等待输入任意字符开始游戏
jsr erasehint ; 游戏开始,清空提示
* jsr move ; 蛇移动一格
clc
jsr judgeout ; 判断是否出界
bcc +
jsr printfail
jsr getchar
rts 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 .scend
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -21,15 +44,26 @@ main:
lda #$00 lda #$00
sta s + 1 ; 初始化分数为0 sta s + 1 ; 初始化分数为0
jsr printscore ; 打印分数 jsr printscore ; 打印分数
lda #1 lda #csnk
sta field + 11*38 + 19 ; 初始化蛇位置 sta field + 11*40 + 19 ; 初始化蛇位置
sta c ; 初始化蛇长为1 sta c ; 初始化蛇长为1
jsr printfield ; 打印蛇,包括边框 jsr printfield ; 打印蛇,包括边框
jsr printhint ; 打印开始提示
lda #go_d ; 初始化方向为下
sta d
.macend .macend
.require "printscore.asm" .require "printscore.asm"
.require "print16.asm" .require "print16.asm"
.require "printfield.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" .require "print.asm"
.checkpc $A000 ; text段边界 .checkpc $A000 ; text段边界

7
move.asm Normal file
View File

@@ -0,0 +1,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; move 根据d中的值实现蛇的移动
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
move:
.scope
rts
.scend

View File

@@ -2,9 +2,9 @@
pha pha
tya tya
pha pha
lda #<_1 ; 取参数的低八位 lda #<_1 ; 取参数的低八位
ldy #>_1 ; 取参数的高八位 ldy #>_1 ; 取参数的高八位
jsr printstr jsr printstr
pla pla
tay tay
pla pla
@@ -17,15 +17,15 @@
.space _ptr 2 .space _ptr 2
.text .text
printstr: printstr:
sta _ptr sta _ptr
sty _ptr+1 sty _ptr+1
ldy #$00 ldy #$00
_lp: _lp:
lda (_ptr),y lda (_ptr),y
beq _done beq _done
jsr chrout jsr chrout
iny iny
bne _lp bne _lp
_done: _done:
rts rts
.scend .scend

View File

@@ -14,7 +14,7 @@ print16:
lda #0 lda #0
ldx #6 ldx #6
* sta _num_dec-1, x * sta _num_dec-1, x
dex dex
bne - bne -
@@ -32,26 +32,23 @@ print16:
; mod函数处理的数据为打印方便均使用大端序存储 ; mod函数处理的数据为打印方便均使用大端序存储
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mod_4: ; 高字节低4位若大于10则向高位进1 mod_4: ; 高字节低4位若大于10则向高位进1
;jsr chrin
lda _num + 3 lda _num + 3
cmp #10 cmp #10
bcc + bcc +
sbc #10 ; 此时c为1 sbc #10 ; 此时c为1
tax tax
lda #1 lda #1
sta _num_dec + 3 ; 进位 sta _num_dec + 3 ; 进位
;jsr chrin
txa txa
* sta _num_dec + 4 * sta _num_dec + 4
;jsr chrin
rts rts
mod_3: ; 高字节高4位的值相当于个位加6十位加1 mod_3: ; 高字节高4位的值相当于个位加6十位加1
ldx _num + 2 ldx _num + 2
bne + ; 如果为0直接返回 bne + ; 如果为0直接返回
rts rts
* lda #6 * lda #6
`carry10 4 `carry10 4
lda #1 lda #1
`carry10 3 `carry10 3
@@ -59,11 +56,11 @@ mod_3: ; 高字节高4位的值相当于个位加6十位加1
bne - bne -
rts rts
mod_2: ; 低字节低4位的值相当于个位加6十位加5百位加2 mod_2: ; 低字节低4位的值相当于个位加6十位加5百位加2
ldx _num + 1 ldx _num + 1
bne + ; 如果为0直接返回 bne + ; 如果为0直接返回
rts rts
* lda #6 * lda #6
`carry10 4 `carry10 4
lda #5 lda #5
`carry10 3 `carry10 3
@@ -73,11 +70,11 @@ mod_2: ; 低字节低4位的值相当于个位加6十位加5百位加
bne - bne -
rts rts
mod_1: ; 低字节低4位的值相当于个位加6十位加9百位加0千位加4 mod_1: ; 低字节低4位的值相当于个位加6十位加9百位加0千位加4
ldx _num ldx _num
bne + ; 如果为0直接返回 bne + ; 如果为0直接返回
rts rts
* lda #6 * lda #6
`carry10 4 `carry10 4
lda #9 lda #9
`carry10 3 `carry10 3
@@ -94,48 +91,44 @@ mod_1: ; 低字节低4位的值相当于个位加6十位加9百位加
adc _num_dec + _1 adc _num_dec + _1
bcc _skip bcc _skip
pha pha
lda #$10 ; 进位相当于100 lda #$10 ; 进位相当于100
clc clc
adc _num_dec + _1 - 1 adc _num_dec + _1 - 1
sta _num_dec + _1 - 1 sta _num_dec + _1 - 1
pla pla
_skip: _skip:
sta _num_dec + _1 sta _num_dec + _1
;jsr chrin
.macend .macend
.macro splitbyte .macro splitbyte
lda _1 lda _1
sta _num + _2 sta _num + _2
;jsr printbyte
lsr lsr
lsr lsr
lsr lsr
lsr lsr
sta _num + _2 - 1 sta _num + _2 - 1
;jsr printbyte
lda #$0f lda #$0f
and _num + _2 and _num + _2
sta _num + _2 sta _num + _2
;jsr printbyte
.macend .macend
.macro carry .macro carry
tya tya
pha pha
sed ; 设置为bcd加减法 sed ; 设置为bcd加减法
jsr mod_4 jsr mod_4
jsr mod_3 jsr mod_3
jsr mod_2 jsr mod_2
jsr mod_1 ; 此时结果中有些位可能大于10需要进行进位处理 jsr mod_1 ; 此时结果中有些位可能大于10需要进行进位处理
ldx #5 ldx #5
_loop: _loop:
ldy #0 ; y记录进位数 ldy #0 ; y记录进位数
txa txa
lda _num_dec-1, x lda _num_dec-1, x
cmp #10 cmp #10
bcc _skip ; 小于10不进位 bcc _skip ; 小于10不进位
pha pha
and #$f0 and #$f0
lsr lsr
@@ -151,11 +144,11 @@ _skip:
dex dex
bne _loop bne _loop
cld ; 退出bcd模式 cld ; 退出bcd模式
ldx #5 ldx #5
_up: _up:
lda _num_dec - 1, x lda _num_dec - 1, x
ora #$30 ; 转化为可显示字符 ora #$30 ; 转化为可显示字符
sta _num_dec - 1, x sta _num_dec - 1, x
dex dex
bne _up bne _up
@@ -163,5 +156,3 @@ _up:
pla pla
tay tay
.macend .macend
; .require "printbyte.asm"

View File

@@ -2,22 +2,22 @@
.space _na 1 ; a的临时存放处 .space _na 1 ; a的临时存放处
.text .text
printbyte: printbyte:
php php
pha pha
sta _na sta _na
txa txa
pha pha
ldx #8 ; 打印8bit ldx #8 ; 打印8bit
* lda #$30 ; a = '0' * lda #$30 ; a = '0'
asl _na ; 左移一位溢出到c asl _na ; 左移一位溢出到c
adc #0 ; a = a + c + 0 adc #0 ; a = a + c + 0
jsr chrout ; putchar(a) jsr chrout ; putchar(a)
dex ; x-- dex ; x--
bne - ; if(x != 0) goto 上个星号 bne - ; if(x != 0) goto 上个星号
lda #$20 lda #$20
jsr chrout jsr chrout
pla pla
tax tax
pla pla
plp plp
rts rts

View File

@@ -6,37 +6,35 @@ printfield:
.data zp .data zp
.space _ptr 2 .space _ptr 2
.text .text
ldx #23 ; i代表行数不含边框 ldx #23 ; i代表行数不含边框
lda #<field ; 取地址低8位 lda #<field ; 取地址低8位
sta _ptr sta _ptr
lda #>field ; 取地址高8位 lda #>field ; 取地址高8位
sta _ptr + 1 sta _ptr + 1
ldy #0 ldy #0
* lda #cblk * lda #cblk
sta (_ptr), y ; 打印左边框 sta (_ptr), y ; 打印左边框
lda #39 lda #39
jsr _addptr jsr _addptr
lda #cblk lda #cblk
sta (_ptr), y ; 打印右边框 sta (_ptr), y ; 打印右边框
lda #1 lda #1
jsr _addptr jsr _addptr
dex dex
bne - bne -
lda #cblk lda #cblk
ldy #40 ldy #40
dec _ptr dec _ptr
* sta (_ptr), y ; 打印底边 * sta (_ptr), y ; 打印底边
dey dey
bne - bne -
ldx #0 rts
jsr plot
rts
_addptr: _addptr:
clc clc
adc _ptr adc _ptr
bcc + bcc +
inc _ptr + 1 inc _ptr + 1
* sta _ptr * sta _ptr
rts rts
.scend .scend

View File

@@ -3,16 +3,16 @@
.require "platform/c64kernal.oph" .require "platform/c64kernal.oph"
main: main:
* jsr getin * jsr getin
beq skip beq skip
cmp #'@ cmp #'@
beq end beq end
jsr printbyte jsr printbyte
lda #13 ; 换行 lda #13 ; 换行
jsr chrout jsr chrout
skip: skip:
jmp - jmp -
end: end:
rts rts
.require "printbyte.asm" .require "printbyte.asm"

View File

@@ -3,13 +3,17 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
printscore: printscore:
.scope .scope
clc
ldx #0 ; 光标回到原点
ldy #0
jsr plot
lda #cblk lda #cblk
ldx #15 ldx #15
* jsr chrout * jsr chrout
dex dex
bne - bne -
`print score_str `print score_str
jsr print16 jsr print16
ldx #15 ldx #15
* jsr chrout * jsr chrout
dex dex

BIN
snake.prg

Binary file not shown.