1
0
mirror of https://github.com/fumiama/c64-snake.git synced 2026-06-05 00:32:39 +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 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 ; 方向 值定义如上

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:
.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段边界

7
move.asm Normal file
View File

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

View File

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

View File

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

View File

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

View File

@@ -6,37 +6,35 @@ printfield:
.data zp
.space _ptr 2
.text
ldx #23 ; i代表行数不含边框
lda #<field ; 取地址低8位
sta _ptr
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
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

View File

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

View File

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

BIN
snake.prg

Binary file not shown.