org 0x100
jmp start
checkBorder:
push bp
mov bp,sp
push ax
push bx
push cx
push dx
mov ax,[bp+6]
mov bx,[bp+4]
cmp ax,17
jz over
cmp ax,54
jz over
cmp bx,2
jz over
cmp bx,23
jz over
jmp nover
over:
mov ax,0xb800
mov es,ax
mov di,1974
mov si,gover
mov cx,[glen]
mov ah,0x70
cld
next:
lodsb
stosw
loop next
mov ah, 0 ; service 0 – get keystroke
int 0x16 ; call BIOS keyboard service
nover:
pop dx
pop cx
pop bx
pop ax
pop bp
ret 4
start:
call clrscr
mov bx,20
mov dx,10
mov [direction],0
mov word[snake_location],
l3:
mov ax,0
push ax
call printScore
mov ax,3
push ax
call printLives
call printRec
mov ax, bx
push ax ; push x position
mov ax, dx
push ax ; push y position
push word [length] ; push message length
push word[direction]
call printSnake ; call the printstr subroutine
call delay
call delay
call delay
call clrscr ; clear the screen
push bx
push dx
call checkBorder
mov ah,0x01
int 0x16
jz skip
mov ah, 0
int 0x16
cmp al,0x77
jz u
cmp al,0x73
jz d
cmp al,0x61
jz l
cmp al,0x64
jz r
cmp al,0x1B
jz ex
skip:
cmp al,0x71
jnz l3
u:
sub dx,1
mov [direction],2
mov [snake_location]
jmp l3
d:
add dx,1
mov [direction],3
jmp l3
l:
sub bx,1
mov [direction],1
jmp l3
r:
add bx,1
mov [direction],0
jmp l3
ex:
mov ax, 0x4c00
int 0x21
length: dw 4
score: db 'Score: '
live: db 'Lives: '
slen: dw 7
gover: db 'Game Over!'
glen: dw 10
direction: db 0
snake_location:dw 0
Above is the code snippet for snake game in 8088 assembly language. It is not the complete code but includes the subroutine to check border for collision. Now I have no clue how to check for collision. I have tried to hard code but still it isn't efficient. Suggest me a way to check for collision of the snake. the snake has default length 4 with additional '0' character so it looks something like this "****0". The snake is printed from left to right. The collision will always be of '0' character but I don't know its position as snake is moving as keys are pressed so I can't figure out how to check for collision.
I am getting wrong results due to miscalculation of location of snakes head. I would appreciate any suggestions that would make code better. Thank you!