; 简单的赛车游戏
;
; 湖北民族学院 向远
;
; 这个程序是大二时的汇编课程设计,当时也是花了不少时间,因为是初学,很多方;面都遇到了麻烦。首先就是画小车的问题,如果采用子程序,参数的频繁变动将使程序;变得很难阅读,后来终于发现宏的应用将使程序变得更易于阅读,但因为宏调用在编译;时会宏展开,采用宏将使编译后的文件比较大。再就是小车的动画实现及其碰撞检测,;也是历经磨难好不容易才完成。尤其是碰撞检测,也没找到好的方法,以至三个车道绝;大部分代码是重复的,还盼高手指点迷津。原准备用中断做背景音乐的,但没做好,只;做了个显示系统时钟的,让大虾们见笑。没写好画圆的函数,红绿灯只好用几个方块表;示了,嘿嘿。
; 程序在masm5.0下编译通过,在此贴出来,希望与初学者及汇编爱好者共同学习讨;论。因为我也是个初学者,里面的内容也是一些适合初学的东西,相信大家都能看懂。
;希望大家有什么问题或看法能与我联系讨论,我的email:xiangyuan_122@163.com
buff_head=1ah
buff_tail=1ch
carpeed=15
;********************
data segment
start_col db ?
sgin db 1
flag db 0
gall_ms db 2
db 2
db 10000011b
db 79 dup(20h),0ffh
db 0ffh,0ffh,0ffh,0ffh,0ffh,0ffh,0ffh,0ffh,0ffh,0ffh
db 25 dup(20h),0dfh,0dbh,0dfh,0dbh,0dch,20h,20h,0dch,0dch,20h
db 20h,20h,0dch,0dch,20h,20h,0dbh,0dbh,0dbh,0ddh,20h,0ffh
db 25 dup(20h),20h,0dbh,20h,20h,0dbh,20h,20h,20h,0dbh,20h,20h
db 20h,0dbh,20h,20h,20h,0dbh,20h,20h,0ddh,20h,0ffh
db 25 dup(20h),0dch,0dbh,0dch,0dbh,0dfh,20h,20h,20h,0dbh,20h
db 20h,20h,0dbh,20h,20h,20h,0dbh,20h,20h,20h,20h,0ffh
db 25 dup(20h),0dfh,0dbh,0dfh,0dbh,0dch,20h,20h,20h,0dbh,20h
db 20h,20h,0dbh,20h,20h,20h,0dfh,0dfh,0dfh,0ddh,20h,0ffh
db 25 dup(20h),20h,0dbh,20h,20h,0dbh,20h,20h,20h,0dbh,20h,20h
db 20h,0dbh,20h,20h,20h,0dch,20h,20h,0ddh,20h,0ffh
db 25 dup(20h),0dch,0dbh,0dch,0dbh,0dfh,20h,20h,20h,0dbh,0dbh
db 0dbh,0dbh,0dbh,20h,20h,20h,0dbh,0dbh,0dbh,0ddh,20h,0ffh
db 0ffh,0ffh,0ffh,0ffh,65 dup(20h),'ENTER'
db 00
times db 5
mess0 db 30 dup(' '),' INSTRUCTIONS',3 dup(13,10)
db 30 dup(' '),' LEFT-----',1bh,3 dup(13,10)
db 30 dup(' '),' RIGHT----',1ah,3 dup(13,10)
db 30 dup(' '),' UP-------',18h,3 dup(13,10)
db 30 dup(' '),' DOWN-----',19h,3 dup(13,10)
db 30 dup(' '),' HP-------','B',3 dup(13,10)
db 30 dup(' '),' GAS------','G',3 dup(13,10)
db 30 dup(' '),' EXIT-----','ESC',3 dup(13,10)
mess1 db 'Are you sure to exit this game?(y/n)';退出提示消息
mess2 db 'HP'
mess3 db 'GAS'
mess4 db 3
mess5 db 0feh
mess6 db 'NEXT'
mess7 db 'GAME OVER'
mess8 db 'CONGRATULATIONS!YOUR PASSED THE GAME'
mess9 db 'GO ON PLAYING?(Y/N)'
pass db 31h
buspeed db 10
color db 0;颜色
x1 dw 0
y1 dw 0
x2 dw 0
y2 dw 0
xx1 dw 0
yy1 dw 80
xx2 dw 150
yy2 dw 500;左边绿化带坐标
xx11 dw 450
yy11 dw 80
xx22 dw 500
yy22 dw 450;右边绿化带坐标
cx1 dw 0
cy1 dw 300
cx2 dw 0
cy2 dw 400;小车坐标初始化
lx1 dw 5
ly1 dw 20
lx2 dw 150
ly2 dw 30;;生命值图坐标
gx1 dw 5
gy1 dw 50
gx2 dw 150
gy2 dw 60;油气值图坐标
rcur db 10
ccur db 22
vbufh dw ?,': '
vbufm dw ?,': '
vbufs dw ?
hhh dw ?
mmm dw ?
sss dw ?
countt dw 18
old_ip1c dw ?
old_cs1c dw ?
count dw 1
pib db 8
gasnum db 30h
bloodnum db 30h
data ends
;**************************************************************************
;**************************************************************************
;显示一个字符的宏定义
display macro char,atr,num
push ax
push bx
push cx
mov ah,9
mov bh,0
mov al,char
mov bl,atr
mov cx,num
int 10h
pop cx
pop bx
pop ax
endm
;设置光标位置的宏定义
set_cursor macro start_row,start_col
push ax
push bx
push dx
mov dh,start_row
mov dl,start_col
mov ah,2
mov bh,0
int 10h
pop dx
pop bx
pop ax
endm
;显示字符串的宏定义
gdisplay macro messn,length,sline,atr
mov ax,seg messn
mov es,ax
lea bp,messn
mov cx,length
mov dx,sline
mov bh,0
mov al,1
mov bl,atr
mov ah,13h
int 10h
endm
;清空键盘缓冲区
ckbuf macro
push ds
push ax
push bx
mov ax,40h
mov ds,ax
mov bx,ds:[buff_head]
mov ds:[buff_tail],bx
pop bx
pop ax
pop ds
endm
;保存中断的宏定义
savevecter macro n
mov al,n
mov ah,35h
int 21h
mov old_ip1c,es
mov old_cs1c,bx
push ds
endm
;设置中断的宏定义
setvecter macro n,addr
mov dx,offset addr
mov ax,seg addr
mov ds,ax
mov al,n
mov ah,25h
int 21h
endm
;设置开中断的掩码
openint macro mark
pop ds
in al,21h
and al,mark
out 21h,al
sti
endm
;发出指定频率的声音
sound macro freq
push ax
push dx
mov di,freq
mov al,0b6h
out 43h,al
mov dx,12h
mov ax,533h*896
div di
out 42h,al
mov al,ah
out 42h,al
in al,61h
mov ah,al
or al,3
out 61h,al
pop dx
pop ax
endm
;关闭扬声器
nosound macro
in al,61h
xor al,3
out 61h,al
endm
;延时的宏定义
daly macro low_us,high_us
push ax
push cx
push dx
mov dx,low_us
mov cx,high_us
mov ah,86h
int 15h
pop dx
pop cx
pop ax
endm
;产生0-n的随机数宏定义
;返回值存ah寄存器
rand macro n
push cx
push dx
xor ah,ah
int 1ah
mov ax,dx
and ax,07fh
mov dl,n
div dl
pop dx
pop cx
endm
;清屏宏定义
cscreen macro coloratr
push ax
push bx
push cx
push dx
mov ah,6
mov al,0
mov bh,coloratr
mov ch,0
mov cl,0
mov dh,24h
mov dl,79
int 10h
mov dx,0
mov ah,2
int 10h
pop dx
pop cx
pop bx
pop ax
endm
;画横线的宏定义
hline macro dy0,dx1,dx2,color
local hdot
mov dx,dy0 ;像素行
mov cx,dx1 ;像素列
hdot:
mov al,color;颜色值
or al,10000000b
mov ah,0ch
int 10h ;写像素
inc cx
cmp cx,dx2
jl hdot
endm
;画竖线的宏定义
vline macro dx0,dy1,dy2,color
local vdot
mov cx,dx0 ;像素列
mov dx,dy1 ;像素行
vdot:
mov al,color;颜色值
or al,10000000b
mov ah,0ch
int 10h ;写像素
inc dx
cmp dx,dy2
jl vdot
endm
;画矩形的宏定义
rectangle macro x1,y1,x2,y2,color
hline y1,x1,x2,color
hline y2,x1,x2,color
vline x1,y1,y2,color
vline x2,y1,y2,color
endm
;画填充矩形的宏定义
fillrectangle macro x1,y1,x2,y2,color
local dline
dline:
hline y1,x1,x2,color
inc y1
mov ax,y2
cmp ax,y1
ja dline
endm
;*********************************************************
;主过程
;*********************************************************
pro_nam segment
assume cs:pro_nam,ds:data
main proc far
jmp start
;画路的子程序
road proc near
vline 450,0,472,15
vline 350,0,472,15
vline 250,0,472,15
vline 150,0,472,15
vline 400,0,472,3
vline 300,0,472,3
vline 200,0,472,3
ret
road endp
;左边绿化带
ltree proc near
fillrectangle xx1,yy1,xx2,yy2,5
fillrectangle xx1,yy1,xx2,yy2,5
fillrectangle xx1,yy1,xx2,yy2,5
fillrectangle xx1,yy1,xx2,yy2,5
fillrectangle xx1,yy1,xx2,yy2,5
ret
ltree endp
;右边绿化带
rtree proc near
fillrectangle xx11,yy11,xx22,yy22,5
fillrectangle xx11,yy11,xx22,yy22,5
fillrectangle xx11,yy11,xx22,yy22,5
fillrectangle xx11,yy11,xx22,yy22,5
fillrectangle xx11,yy11,xx22,yy22,5
ret
rtree endp
;生命值
blood proc near
sub lx1,20
mov ly1,20
sub lx2,20
mov ly2,30
fillrectangle lx1,ly1,lx2,ly2,3
ret
blood endp
;恢复生命值
rehp proc near
push ax
mov ax,lx1
mov lx2,ax
pop ax
mov ly1,20
add lx2,20
mov ly2,30
fillrectangle lx1,ly1,lx2,ly2,3
add lx1,20
add lx2,20
ret
rehp endp
;显示一个加命的字符
playhp proc near
set_cursor 0ch,1ch
display 3,0f3h,1
playhp endp
;油气值
gas proc near
sub gx1,1
mov gy1,50
sub gx2,1
mov gy2,60
fillrectangle gx1,gy1,gx2,gy2,0f6h
ret
gas endp
;恢复油气值
regas proc near
push ax
mov ax,gx1
mov gx2,ax
pop ax
mov gy1,50
add gx2,10
mov gy2,60
fillrectangle gx1,gy1,gx2,gy2,0f6h
add gx1,10
add gx2,1
ret
regas endp
;显�