📄 snake.asm
字号:
push ax
push dx
push cx
;主菜单第一项'1-New Game'
mov dh,10
mov dl,24
mov al,'1'
call View_Video
inc dl
mov al,'-'
call View_Video
inc dl
mov al,'N'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'w'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'G'
call View_Video
inc dl
mov al,'a'
call View_Video
inc dl
mov al,'m'
call View_Video
inc dl
mov al,'e'
call View_Video
;主菜单第二项'2-Load Game'
mov dh,11
mov dl,24
mov al,'2'
call View_Video
inc dl
mov al,'-'
call View_Video
inc dl
mov al,'L'
call View_Video
inc dl
mov al,'o'
call View_Video
inc dl
mov al,'a'
call View_Video
inc dl
mov al,'d'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'G'
call View_Video
inc dl
mov al,'a'
call View_Video
inc dl
mov al,'m'
call View_Video
inc dl
mov al,'e'
call View_Video
;主菜单第三项'3-View Rank'
mov dh,12
mov dl,24
mov al,'3'
call View_Video
inc dl
mov al,'-'
call View_Video
inc dl
mov al,'V'
call View_Video
inc dl
mov al,'i'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'w'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'R'
call View_Video
inc dl
mov al,'a'
call View_Video
inc dl
mov al,'n'
call View_Video
inc dl
mov al,'k'
call View_Video
;主菜单第四项'4-Other Set'
mov dh,13
mov dl,24
mov al,'4'
call View_Video
inc dl
mov al,'-'
call View_Video
inc dl
mov al,'O'
call View_Video
inc dl
mov al,'t'
call View_Video
inc dl
mov al,'h'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'r'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'S'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'t'
call View_Video
;主菜单第五项'5-Quit'
mov dh,14
mov dl,24
mov al,'5'
call View_Video
inc dl
mov al,'-'
call View_Video
inc dl
mov al,'Q'
call View_Video
inc dl
mov al,'u'
call View_Video
inc dl
mov al,'i'
call View_Video
inc dl
mov al,'t'
call View_Video
;绘制菜单框架,范围(170,150)-(300-246)
mov al,10
mov cx,170
mov dx,150
line5:
int 10h
inc cx
cmp cx,300
jnz line5
mov cx,170
mov dx,246
line6:
int 10h
inc cx
cmp cx,300
jnz line6
mov dx,150
mov cx,170
line7:
int 10h
inc dx
cmp dx,246
jnz line7
mov dx,150
mov cx,300
line8:
int 10h
inc dx
cmp dx,246
jnz line8
;判断是否显示第四项子菜单
cmp Direct_set1,1
jz Other_set
;判断是否显示第三项子菜单
cmp Direct_set3,1
jnz Go_on
jmp View_Rank_ABC
;判断用户选择的菜单项
Go_on:
R_in1: mov ah,08h
int 21h
cmp al,'1'
je the_1
cmp al,'2'
je the_2
cmp al,'3'
je the_3
cmp al,'4'
je the_4
cmp al,'5'
je the_5
;输入错误信息不作任何响应,等待用户继续输入
jmp R_in1
;第一项为开始新游戏
;游戏初始化
the_1: mov Speed,16572
mov GameTime,0
mov Gscore,0
jmp exit_Menu
;第二项为载入上一次保存的游戏
the_2: call Load_File
jmp exit_Menu
;第三项为察看游戏排名,打开进入第三项子菜单'开关'
the_3: mov Direct_set3,1
jmp far ptr start
;第五项为退出游戏
the_5: jmp far ptr exit
;第四项为显示其它设置,打开进入第四项子菜单'开关'
the_4: mov Direct_set1,1
jmp far ptr start
;绘制第四项子菜单的框架
;范围(200,200)-(330,265)
Other_set:
mov cx,200
mov al,0
INC_C:
inc cx
mov dx,200
INC_D: inc dx
int 10h
cmp dx,264
jnz INC_D
cmp cx,329
jnz INC_C
mov al,10
mov cx,200
mov dx,200
line15:
int 10h
inc cx
cmp cx,330
jnz line15
mov cx,200
mov dx,265
line16:
int 10h
inc cx
cmp cx,330
jnz line16
mov dx,200
mov cx,200
line17:
int 10h
inc dx
cmp dx,265
jnz line17
mov dx,200
mov cx,330
line18:
int 10h
inc dx
cmp dx,266
jnz line18
;第四项子菜单目录
;第一项'1-Set Count'
mov dh,13
mov dl,28
mov al,'1'
call View_Video
inc dl
mov al,'-'
call View_Video
inc dl
mov al,'S'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'t'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'C'
call View_Video
inc dl
mov al,'o'
call View_Video
inc dl
mov al,'u'
call View_Video
inc dl
mov al,'n'
call View_Video
inc dl
mov al,'t'
call View_Video
;第二项'2-Set Color'
mov dh,14
mov dl,28
mov al,'2'
call View_Video
inc dl
mov al,'-'
call View_Video
inc dl
mov al,'S'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'t'
call View_Video
inc dl
inc dl
mov al,'C'
call View_Video
inc dl
mov al,'o'
call View_Video
inc dl
mov al,'l'
call View_Video
inc dl
mov al,'o'
call View_Video
inc dl
mov al,'r'
call View_Video
;第三项'3-Cancel'
mov dh,15
mov dl,28
mov al,'3'
call View_Video
inc dl
mov al,'-'
call View_Video
inc dl
mov al,'C'
call View_Video
inc dl
mov al,'a'
call View_Video
inc dl
mov al,'n'
call View_Video
inc dl
mov al,'c'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'l'
call View_Video
;判断是否进入第四项子菜单的下一层菜单
cmp Direct_set2,1
jz Value_set0
;判断用户输入的菜单选项
R_in2: mov ah,7h
int 21h
cmp al,'1'
je the_6
cmp al,'2'
je the_7
cmp al,'3'
je the_8
jmp R_in2
;当输入错误信息时,不作任何响应,等待用户继续输入
;第一项为设置count值,打开进入第四项子菜单及下一层子菜单'开关'
;另 设置控制变量Ctrl_set的值为1,表示将要设置count的值
the_6: mov Direct_set1,1
mov Direct_set2,1
mov Ctrl_set,1
jmp far ptr start
;第二项为设置PointColor值
;同上
;另 设置控制变量Ctrl_set的值为2,表示将要设置PointColor的值
the_7: mov Direct_set1,1
mov Direct_set2,1
mov Ctrl_set,2
jmp far ptr start
;第三项为退出到上一层菜单
;关闭进入第四项子菜单及下一层子菜单'开关'
the_8: mov Direct_set1,0
mov Direct_set2,0
jmp far ptr start
;调用子函数,设置count和PointColor的值
Value_set0:
call Value_set
;调用子函数,显示游戏分数排名情况
View_Rank_ABC:
call View_Rank
exit_Menu:
;设置蛇的运动方向,默认值为03H,表示向上
mov Directive,03h
;清除菜单界面,绘制游戏界面
call draw_the_game_area
pop cx
pop dx
pop ax
ret
Show_Menu endp
;***********************************
;设置新游戏的count和PointColor的值
Value_set proc near
;关闭所有进入子菜单的开关
mov Direct_set1,0
mov Direct_set2,0
mov Direct_set3,0
;绘制数值输入窗口框架
;范围(200,270)-(330,290)
mov al,10
mov cx,200
mov dx,270
line15S:
int 10h
inc cx
cmp cx,330
jnz line15S
mov cx,200
mov dx,290
line16S:
int 10h
inc cx
cmp cx,330
jnz line16S
mov dx,270
mov cx,200
line17S:
int 10h
inc dx
cmp dx,290
jnz line17S
mov dx,270
mov cx,330
line18S:
int 10h
inc dx
cmp dx,291
jnz line18S
;设置光标在屏幕中的位置
mov dh,17
mov dl,32
mov al,' '
call View_Video
mov ah,01h
int 21h
;将用户输入的字符转化为数据
;根据Ctrl_set的值,判断用户需要的设置
sub al,2fh
cmp Ctrl_set,1
jz Set_count
cmp Ctrl_set,2
mov PointColor,al
jmp exit_SetV
Set_count:
;将转换来的数据乘以8,使差异明显化
push bx
mov bl,8
mul bl
mov count,al
exit_SetV:
mov ah,01h
int 21h
jmp far ptr start
ret
Value_set endp
;***********************************
;生成文件路径及名字'C:\515',保存在内存中
;无后缀
File_Name proc near
push bx
push ax
lea bx,File_Name_ASC
mov al,43h
mov [bx],al
inc bx
mov al,3ah
mov [bx],al
inc bx
mov al,5ch
mov [bx],al
inc bx
mov al,35h
mov [bx],al
inc bx
mov al,31h
mov [bx],al
inc bx
mov al,35h
mov [bx],al
pop ax
pop bx
ret
File_Name endp
;***********************************
;保存游戏环境设置及游戏进度
Save_File proc near
push ax
push bx
push cx
push dx
;生成文件名
call File_Name
;创建一个文件,获得该文件句柄
mov ah,3ch
mov cx,00
lea dx,File_Name_ASC
int 21h
mov File_Handle,ax
;收集要保存在文件中的信息
lea bx,File_Area
mov ax,count
mov [bx],ax
add bx,2
mov ax,Speed
mov [bx],ax
add bx,2
mov ax,GameTime
mov [bx],ax
add bx,2
mov ax,Gscore
mov [bx],ax
add bx,2
mov ax,TempHalf
mov [bx],ax
add bx,2
mov al,PointColor
cbw
mov [bx],ax
add bx,2
mov ax,Rank_ABC
mov [bx],ax
add bx,2
mov ax,Rank_ABC+2
mov [bx],ax
add bx,2
mov ax,Rank_ABC+4
mov [bx],ax
;将收集的信息保存在文件中
mov ah,40h
mov bx,File_Handle
mov cx,18
lea dx,File_Area
int 21h
;关闭文件
mov ah,3eh
mov bx,File_Handle
int 21h
pop dx
pop cx
pop bx
pop ax
ret
Save_File endp
;***********************************
;载入上一次保存的游戏进度及环境设置
Load_File proc near
push ax
push bx
push cx
push dx
;生成文件名
call File_Name
;打开文件,获得该文件句柄
mov ah,3dh
mov al,00
lea dx,File_Name_ASC
int 21h
mov File_Handle,ax
;读取文件中的内容到内存缓冲区
mov ah,3fh
mov bx,File_Handle
mov cx,18
lea dx,File_Area
int 21h
;关闭文件
mov ah,3eh
mov bx,File_Handle
int 21h
;根据从文件中读取的信息设置游戏环境及进度
mov bx,offset File_Area
mov ax,[bx]
mov count,ax
add bx,2
mov ax,[bx]
mov Speed,ax
add bx,2
mov ax,[bx]
mov GameTime,ax
add bx,2
mov ax,[bx]
mov Gscore,ax
add bx,2
mov ax,[bx]
mov TempHalf,ax
add bx,2
mov al,[bx]
mov PointColor,al
add bx,2
mov ax,[bx]
mov Rank_ABC,ax
add bx,2
mov ax,[bx]
mov Rank_ABC+2,ax
add bx,2
mov ax,[bx]
mov Rank_ABC+4,ax
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -