📄 snake.asm
字号:
;绘制方格函数
draw_one_point proc near
push si
push di
push cx
push dx
;以当前坐标为中心,绘制一个半边长为HalfPoint的方块
mov si,cx
mov di,dx
add si,HalfPoint
add di,HalfPoint
sub cx,HalfPoint
sub dx,HalfPoint
mov TempPoint,dx
;一列一列的绘制
re_draw1:
mov dx,TempPoint
re_draw2:
int 10h
inc dx
cmp dx,di
jnz re_draw2
inc cx
cmp cx,si
jnz re_draw1
pop dx
pop cx
pop di
pop si
ret
draw_one_point endp
;************************************
;取得系统时间,单位为:时,分,秒,百分之一秒
Get_time proc near
push ax
push cx
push dx
mov ah,2ch
int 21h
mov SysTime,ch
mov SysTime+1,cl
mov SysTime+2,dh
mov SysTime+3,dl
;dh和上一次检测系统时间的数据比较
;不相同的时候将数据传送给Time_temp(即表示时间己过了一秒)
;并且显示游戏进度时间
cmp dh,Time_temp
jz Exit_Get
mov Time_temp,dh
call Print_GameTime
Exit_Get:
pop dx
pop cx
pop ax
ret
Get_time endp
;************************************
;食物随机坐标生成函数
;当食物被蛇"吃"掉的时候生成一个新的食物坐标
;由于两次吃掉食物之间有一个以百分之一秒为单位
;的时间差,并且每次的时间差相同的几率很小
;故可以用上一次吃掉食物的dl值作为种子,生成新坐标的X值
;用吃掉食物时刻的dl值作为种子,生成新坐标的Y值
;由于种子的随机性,生成的坐标的随机性也很好
Creat_a_random_mark proc near
push ax
push bx
push dx
;取这次吃掉食物时刻的dl值,作为种子
;生成随机坐标的Y值
mov dh,SysTime+3
mov ax,8
mul dh
add ax,16
cmp ax,466
jg Adjust1
jmp Normal1
Adjust1:
sub ax,440
Normal1:
mov Food+2,ax
mov ax,8
;取上次吃掉食物的dl值,作为种子
;生成随机坐标的X值
mov bl,Foodt
mul bl
add ax,18
cmp ax,466
jg Adjust2
jmp Normal2
Adjust2:
sub ax,440
Normal2:
mov Food,ax
;取这次吃掉食物的dl值,传送给Foodt,为下次
;生成随机坐标准备
mov al,SysTime+3
mov Foodt,al
pop dx
pop bx
pop ax
ret
Creat_a_random_mark endp
;************************************
;判断蛇吃掉食物的函数
Judeg_meet proc near
push ax
;判断蛇头的横坐标是否和食物的横坐标相等
;若不相等则退出本函数
;若相等则继续判断纵坐标的关系
mov ax,Food
cmp ax,snake_H
jne exit_Judeg
;若纵坐标不相同,则退出本函数
;若纵坐标依然和相同,则生成一个新的随机坐标
;并且在新的坐标的位置绘制食物
mov ax,Food+2
cmp ax,snake_H+2
jne exit_Judeg
;程序能运行到此处,表示蛇吃掉了食物
call Soundf
call Creat_a_random_mark
call draw_the_food
push ax
;增加游戏的分数值,这里设定为15,并且以5位十进制显示
mov ax,Gscore
add ax,15
mov Gscore,ax
mov BTD,ax
mov H_value,3
mov L_value,66
mov Ctr_Jmp,0
call Bin_to_Dec
;蛇吃了食物后身体边长,即纪录蛇身体坐标的数据元素增加
mov ax,count
add ax,4
mov count,ax
;吃掉食物之后色的移动速度增加
mov ax,Speed
sub ax,137
mov Speed,ax
pop ax
exit_Judeg:
pop ax
ret
Judeg_meet endp
;************************************
;设置光标在屏幕上的位置
;并在该位置显示一个字符
;位置坐标由dx传递,字符由al传递
View_Video proc near
push ax
push bx
push dx
push cx
mov ah,2
mov bh,0
int 10h
mov ah,0eh
mov bh,0
mov bl,07dh
mov cx,1
int 10h
pop cx
pop dx
pop bx
pop ax
ret
View_Video endp
;************************************
;在屏幕上显示'Score'
Print_score proc near
push ax
push dx
mov dh,1
mov dl,60
mov al,'S'
call View_Video
inc dl
mov al,'c'
call View_Video
inc dl
mov al,'o'
call View_Video
inc dl
mov al,'r'
call View_Video
inc dl
mov al,'e'
call View_Video
pop dx
pop ax
ret
Print_score endp
;************************************
;在屏幕上显示'System Data'
;并且以YY-MM-DD形式显示系统日期
Print_data proc near
push ax
push dx
mov dh,13
mov dl,60
mov al,'S'
call View_Video
inc dl
mov al,'y'
call View_Video
inc dl
mov al,'s'
call View_Video
inc dl
mov al,'t'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'m'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'D'
call View_Video
inc dl
mov al,'a'
call View_Video
inc dl
mov al,'t'
call View_Video
inc dl
mov al,'a'
call View_Video
;显示日
mov al,SysData+1
cbw
mov BTD,ax
mov H_value,15
mov L_value,68
call Bin_to_Dec
;显示月
mov al,SysData
cbw
mov BTD,ax
mov H_value,15
mov L_value,65
call Bin_to_Dec
;显示年
mov ax,SysYear
mov BTD,ax
mov H_value,15
mov L_value,62
call Bin_to_Dec
mov dh,15
mov dl,62
mov al,' '
call View_Video
mov dh,15
mov dl,67
mov al,'-'
call View_Video
mov dh,15
mov dl,70
mov al,'-'
call View_Video
pop dx
pop ax
ret
Print_data endp
;************************************
;二进制向十进制转换并在屏幕上显示
;有显示5位十进制和2位十进制两种模式
;由变量Ctr_Jmp控制
Bin_to_Dec proc near
push dx
push ax
push bx
push cx
mov ch,H_value
mov cl,L_value
;****get the dec number
mov ax,BTD
cmp Ctr_Jmp,0
jnz Two_number
mov bx,10000
mov dx,0
div bx
add al,30h
push dx
mov dx,cx
call View_Video
inc cl
pop ax
mov bx,1000
mov dx,0
div bx
add al,30h
push dx
mov dx,cx
call View_Video
inc cl
pop ax
mov dx,0
mov bx,100
div bx
add al,30h
push dx
mov dx,cx
call View_Video
inc cl
pop ax
Two_number:
mov dx,0
mov bx,10
div bx
add al,30h
push dx
mov dx,cx
call View_Video
inc cl
pop ax
mov dx,0
mov bx,1
div bx
add al,30h
push dx
mov dx,cx
call View_Video
pop dx
pop cx
pop bx
pop ax
pop dx
ret
Bin_to_Dec endp
;************************************
;在屏幕上显示 'speed'
Print_speed proc near
push ax
push dx
mov dh,7
mov dl,60
mov al,'S'
call View_Video
inc dl
mov al,'p'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'d'
call View_Video
pop dx
pop ax
ret
Print_speed endp
;************************************
;在屏幕上显示'System Time'
;'Game Time'和'sec'
Print_time proc near
push ax
push dx
mov dh,19
mov dl,60
mov al,'S'
call View_Video
inc dl
mov al,'y'
call View_Video
inc dl
mov al,'s'
call View_Video
inc dl
mov al,'t'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,'m'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'T'
call View_Video
inc dl
mov al,'i'
call View_Video
inc dl
mov al,'m'
call View_Video
inc dl
mov al,'e'
call View_Video
mov dh,21
mov dl,60
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
inc dl
mov al,' '
call View_Video
inc dl
mov al,'T'
call View_Video
inc dl
mov al,'i'
call View_Video
inc dl
mov al,'m'
call View_Video
inc dl
mov al,'e'
call View_Video
mov dh,22
mov dl,70
mov al,'s'
call View_Video
mov dh,22
mov dl,71
mov al,'e'
call View_Video
mov dh,22
mov dl,72
mov al,'c'
call View_Video
pop dx
pop ax
ret
Print_time endp
;************************************
;在屏幕上显示系统时间
;以HH:MM:SS方式显示
Print_SysTime proc near
push ax
;显示时
mov al,SysTime
cbw
mov BTD,ax
mov H_value,20
mov L_value,64
mov Ctr_Jmp,1
call Bin_to_Dec
mov dh,20
mov dl,66
mov al,':'
call View_Video
;显示分
mov al,SysTime+1
cbw
mov BTD,ax
mov H_value,20
mov L_value,67
mov Ctr_Jmp,1
call Bin_to_Dec
mov dh,20
mov dl,69
mov al,':'
call View_Video
;显示秒
mov al,SysTime+2
cbw
mov BTD,ax
mov H_value,20
mov L_value,70
mov Ctr_Jmp,1
call Bin_to_Dec
pop ax
ret
Print_SysTime endp
;************************************
;在屏幕上显示游戏进度时间
Print_GameTime proc near
push ax
mov ax,GameTime
inc ax
mov GameTime,ax
mov BTD,ax
mov H_value,22
mov L_value,64
mov Ctr_Jmp,0
call Bin_to_Dec
pop ax
ret
Print_GameTime endp
;************************************
;显示游戏名字'Snake Game'
;制作地点'Made in 515'
;制作单位'03090402'
;制作人'28 04 12 32 09'
Print_Copyright proc near
push ax
push dx
mov dh,24
mov dl,60
mov al,'S'
call View_Video
inc dl
mov al,'n'
call View_Video
inc dl
mov al,'a'
call View_Video
inc dl
mov al,'k'
call View_Video
inc dl
mov al,'e'
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
mov dh,25
mov dl,62
mov al,'M'
call View_Video
inc dl
mov al,'a'
call View_Video
inc dl
mov al,'d'
call View_Video
inc dl
mov al,'e'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'i'
call View_Video
inc dl
mov al,'n'
call View_Video
inc dl
mov al,' '
call View_Video
inc dl
mov al,'5'
call View_Video
inc dl
mov al,'1'
call View_Video
inc dl
mov al,'5'
call View_Video
mov Ctr_Jmp,0
mov BTD,402
mov H_value,26
mov L_value,65
call Bin_to_Dec
mov BTD,3090
mov H_value,26
mov L_value,62
call Bin_to_Dec
mov BTD,28004
mov H_value,26
mov L_value,71
call Bin_to_Dec
mov BTD,12032
mov H_value,27
mov L_value,71
call Bin_to_Dec
mov Ctr_Jmp,1
mov BTD,06
mov H_value,28
mov L_value,71
call Bin_to_Dec
mov dh,26
mov dl,70
mov al,'-'
call View_Video
mov dh,27
mov dl,70
mov al,'-'
call View_Video
mov dh,28
mov dl,70
mov al,'-'
call View_Video
mov dh,26
mov dl,73
mov al,' '
call View_Video
mov dh,27
mov dl,73
mov al,' '
call View_Video
pop dx
pop ax
ret
Print_Copyright endp
;****************************************
;显示游戏菜单
Show_Menu proc near
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -