⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 car.asm

📁 一个用x86汇编语言编写的模拟赛车游戏。实现了基本的键盘交互和菜单选择
💻 ASM
📖 第 1 页 / 共 2 页
字号:


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

;显示一个加气的字符
playgas proc near
set_cursor 0ch,2ch
display  4,0f6h,1
playgas endp

;红绿灯
light proc near
mov x1,535
mov y1,65
mov x2,565
mov y2,155
rectangle x1,y1,x2,y2,1
add x1,5
add y1,5
sub x2,5
sub y2,65
fillrectangle x1,y1,x2,y2,3
daly 4240h,000fh
add y1,10
add y2,30
fillrectangle x1,y1,x2,y2,9
daly 4240h,000fh
add y1,10
add y2,30
fillrectangle x1,y1,x2,y2,5
ret
light endp

;显示时间中断子程序
newint1c proc near
push ds
push es
push ax
push bx
push cx
push dx
push si
push di
mov bx,data
mov ds,bx
sti
dec countt
cmp countt,0
jnz exitint
mov countt,18
mov ah,2
int 1ah
mov al,ch
call ttasc
mov word ptr hhh,ax
mov al,cl
call ttasc
mov word ptr mmm,ax
mov al,dh
call ttasc
mov word ptr sss,ax
exitint:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
pop es
pop ds
iret
newint1c endp

ttasc proc near
push cx
mov ah,al
and al,0fh
mov cl,4
shr ah,cl
add ax,3030h
xchg ah,al
pop cx
ret
ttasc endp

;图形模式下显示文本
graphic_text proc near
mov dh,[di]
inc di
mov dl,[di]
mov start_col,dl
mov ah,2
mov bh,0
int 10h
inc di
mov bl,[di]
char_write:
inc di
mov al,[di]
cmp al,0ffh
je bump_row
cmp al,0
jz end_text
call show_char
jmp char_write
end_text:
ret
bump_row:
inc dh
mov dl,start_col
mov ah,2
mov bh,0
int 10h
jmp char_write
graphic_text endp

;显示字符
show_char proc near
mov ah,9
mov bh,0
mov cx,1
int 10h
inc dl
mov ah,2
mov bh,0
int 10h
ret
show_char endp

;画小车
car proc near
push cx1
push cy1
push cx2
push cy2
add cy1,50
sub cy2,10
rectangle cx1,cy1,cx2,cy2,12
sub cx1,10
sub cy1,10
add cx2,10
sub cy2,40
rectangle cx1,cy1,cx2,cy2,12
add cx1,20
sub cy1,20
sub cx2,20
sub cy2,10
rectangle cx1,cy1,cx2,cy2,12
sub cx1,20
add cy1,70
add cx2,20
add cy2,60
rectangle cx1,cy1,cx2,cy2,12
add cx1,20
sub cy1,40
sub cy2,10
vline cx1,cy1,cy2,5
add cx1,10
vline cx1,cy1,cy2,5
add cx1,10
vline cx1,cy1,cy2,5
sub cx1,10
sub cy1,50
sub cy2,70
vline cx1,cy1,cy2,15
sub cx1,15
sub cx2,15
hline cy1,cx1,cx2,12
pop cy2
pop cx2
pop cy1
pop cx1
ret
car endp


;第一车道卡车
cbus1 proc near
mov ccur,22
mov xx1,100
mov yy1,80
mov xx2,150
mov yy2,90;左边绿化带初始坐标值
mov xx11,450
mov yy11,80
mov xx22,500
mov yy22,90;右边绿化带初始坐标值
mov x1,160
mov y1,-280
mov x2,240
mov y2,-50;卡车初始坐标值
loop_x1:
push x1
push y1
push x2
push y2
add y1,60
rectangle x1,y1,x2,y2,12
add x1,10
sub y1,80
sub x2,10
sub y2,200
rectangle x1,y1,x2,y2,12
add x1,10
add y1,80
add y2,200
vline x1,y1,y2,12
add x1,20
vline x1,y1,y2,12
add x1,20
vline x1,y1,y2,12
pop y2
pop x2
pop y1
pop x1
call ltree
call rtree
push x1
push y1
push x2
push y2
add y1,60
rectangle x1,y1,x2,y2,12
add x1,10
sub y1,80
sub x2,10
sub y2,200
rectangle x1,y1,x2,y2,12
add x1,10
add y1,80
add y2,200
vline x1,y1,y2,12
add x1,20
vline x1,y1,y2,12
add x1,20
vline x1,y1,y2,12
pop y2
pop x2
pop y1
pop x1
cmp sgin,0
jz  c1
jmp  nextb1
c1:
mov di,y2
cmp cy1,di
jle d1
jmp nextb1
d1:
mov di,cy2
cmp y1,di;检测是否与卡车碰撞
jle d2
jmp nextb1
d2:
sound 494
daly 4240h,000fh
nosound

call blood
cmp lx1,0
jle b1
jmp nextt1
b1:
cscreen 7

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -