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

Merge pull request #1 from fumiama/main

updata
This commit is contained in:
laffey98
2021-04-12 20:12:24 +08:00
committed by GitHub
8 changed files with 117 additions and 36 deletions

View File

@@ -1,2 +1,6 @@
# c64-snake # c64-snake
C64上的贪吃蛇 在Commodore64上实现简单的贪吃蛇游戏。
# 说明
1. 如果想要直接游玩,只需要下载`snake.prg`文件,在模拟器上运行即可。
2. 如果想进行修改,你需要下载[ophis](https://github.com/fumiama/c64-ophis-lib),并了解它的独特语法,以理解程序代码。

View File

@@ -3,5 +3,89 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
addfood: addfood:
.scope .scope
.data
.space seed 6
.text
lda #<field
sta _ptr
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 rts
.scend .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

View File

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

View File

@@ -1,6 +1,7 @@
.alias cblk $66 ; 边框 .alias cblk $66 ; 边框
.alias csnk $a0 ; 像素 .alias csnk $a0 ; 像素
.alias csps $20 ; 空白 .alias csps $20 ; 空白
.alias crnd $51 ; 食物
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 按钮配置调试时使用WASD控制 ; 按钮配置调试时使用WASD控制
@@ -17,10 +18,13 @@
.space d 1 ; 方向 值定义如上 .space d 1 ; 方向 值定义如上
.space c 1 ; 🐍长度 最大255 最小0 .space c 1 ; 🐍长度 最大255 最小0
.space s 2 ; 得分 小端序 .space s 2 ; 得分 小端序
.space _ptr 2 ; 通用指针
.space eat 1 ; 吃到食物标记
.space shead 2 ; 蛇头指针
.alias title $0400
.alias field $0428 ; 蛇所在屏幕内存区
.data .data
.org $0400 .org $c000
.space title 40
.space field 960 ; 蛇所在屏幕内存区
.text .text

View File

@@ -8,29 +8,31 @@ main:
`init `init
jsr getchar ; 等待输入任意字符开始游戏 jsr getchar ; 等待输入任意字符开始游戏
jsr erasehint ; 游戏开始,清空提示 jsr erasehint ; 游戏开始,清空提示
* jsr move ; 蛇移动一格 * clc
clc
jsr judgeout ; 判断是否出界 jsr judgeout ; 判断是否出界
bcc + bcc +
jsr printfail jsr printfail
jsr getchar jsr getchar
rts rts
clc * clc
* jsr judgefood ; 判断是否吃到食物 lda #0
sta eat
jsr judgefood ; 判断是否吃到食物
bcc + bcc +
jsr append inc c ; 蛇长加一
* jsr calcscore ; 计算得分 lda #1
sta eat
jsr addfood
* jsr move ; 蛇移动一格
jsr calcscore ; 计算得分
jsr printscore ; 打印分数 jsr printscore ; 打印分数
lda #60 lda #32
jsr delay ; 延时期间最后一个按键位于d jsr delay ; 延时期间最后一个按键位于d
lda d lda d
beq ++
cmp #ed_g cmp #ed_g
bne + bne +
rts rts
* sta d * jmp ----
* jsr addfood
jmp -----
.scend .scend
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -41,16 +43,19 @@ main:
jsr chrout jsr chrout
lda #$00 lda #$00
sta s sta s
lda #$00 sta eat
sta s + 1 ; 初始化分数为0 sta s + 1 ; 初始化分数为0
jsr printscore ; 打印分数 jsr printscore ; 打印分数
lda #csnk lda #csnk
sta field + 11*40 + 19 ; 初始化蛇位置 sta field + 11*40 + 19 ; 初始化蛇位置
lda #1
sta c ; 初始化蛇长为1 sta c ; 初始化蛇长为1
jsr printfield ; 打印蛇,包括边框 jsr printfield ; 打印蛇,包括边框
jsr printhint ; 打印开始提示 jsr printhint ; 打印开始提示
lda #go_d ; 初始化方向为下 lda #go_d ; 初始化方向为下
sta d sta d
jsr srand ; 初始化随机数种子
jsr addfood ; 增加食物
.macend .macend
.require "printscore.asm" .require "printscore.asm"
@@ -59,15 +64,14 @@ main:
.require "hint.asm" .require "hint.asm"
.require "getchar.asm" .require "getchar.asm"
.require "addfood.asm" .require "addfood.asm"
.require "append.asm"
.require "delay.asm" .require "delay.asm"
.require "judge.asm" .require "judge.asm"
.require "move.asm" .require "move.asm"
.require "calcscore.asm" .require "calcscore.asm"
.require "print.asm" .require "print.asm"
.checkpc $A000 ; text段边界 .checkpc $a000 ; text段边界
.data zp ; 零页段边界 .data zp ; 零页段边界
.checkpc $80 .checkpc $80
.data .data
.checkpc $0800 ; data段边界 .checkpc $d000 ; data段边界

View File

@@ -13,9 +13,6 @@
; PRINTSTR routine. Accumulator stores the low byte of the address, ; PRINTSTR routine. Accumulator stores the low byte of the address,
; X register stores the high byte. ; X register stores the high byte.
.scope .scope
.data zp
.space _ptr 2
.text
printstr: printstr:
sta _ptr sta _ptr
sty _ptr+1 sty _ptr+1

View File

@@ -3,9 +3,6 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
printfield: printfield:
.scope .scope
.data zp
.space _ptr 2
.text
ldx #23 ; i代表行数不含边框 ldx #23 ; i代表行数不含边框
lda #<field ; 取地址低8位 lda #<field ; 取地址低8位
sta _ptr sta _ptr

BIN
snake.prg

Binary file not shown.