I'm making the snake game and I got to the part where there are (for now) 3 asterisks that make the snake. When I move from vertical to horizontal and from horizontal to vertical it adds another asterisk
And for some reason just adds another *
to the row of *
s in the same place every time
For now my code is:
IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
; Your variables here
; --------------------------
saveal db, ' ' ;used in line 223-233
app dw 0 ;place of the apple
st_am dw 3
stars dw 0, 0, 0 ;places of the *
CODESEG
proc black
body:
mov [es:si], ax
add si, 2
cmp si, 25*80*2
jnz body
ret
endp black
proc up
mov di, 80*2
cmp si, di
jb not_move_up
cmp si, [app]
jnz move_up
call apple
move_up:
call delete
mov di, [stars+2]
mov [stars+4], di
mov di, [stars]
mov [stars+2], di
sub di, 80*2
mov ah, 156
mov al, '*'
mov [es:di], ax
mov [stars], si
sub si, 80*2
mov ah, 156
mov al, '*'
mov [es:si], ax
not_move_up:
ret
endp up
proc down
mov di, (24*80*2)-1
cmp si, di
jg not_move_down
cmp si, [offset app]
jnz move_down
call apple
move_down:
call delete
mov di, [stars+2]
mov [stars+4], di
mov di, [stars]
mov [stars+2], di
add di, 80*2
mov ah, 156
mov al, '*'
mov [es:di], ax
mov [stars], si
add si, 80*2
mov ah, 156
mov al, '*'
mov [es:si], ax
not_move_down:
ret
endp down
proc left
mov dx, 0
mov bx, si
mov ax, si
mov si, 80*2
div si
mov si, bx
cmp dx,0
jz not_move_left
cmp si, [offset app]
jnz move_left
call apple
move_left:
call delete
mov di, [stars+2]
mov [stars+4], di
mov di, [stars]
mov [stars+2], di
mov ah, 156
mov al, '*'
mov [es:di], ax
mov [stars], si
sub si, 2
mov ah, 156
mov al, '*'
mov [es:si], ax
not_move_left:
ret
endp left
proc right
mov dx, 0
mov bx, si
mov ax, si
mov si, 80*2
div si
mov si, bx
cmp dx,158
jz not_move_right
cmp si, [app]
jnz move_right
call apple
move_right:
call delete
mov di, [stars+2]
mov [stars+4], di
mov di, [stars]
mov [stars+2], di
mov ah, 156
mov al, '*'
mov [es:di], ax
mov [stars], si
add si, 2
mov ah, 156
mov al, '*'
mov [es:si], ax
not_move_right:
ret
endp right
proc apple
mov ax, 40h
mov es, ax
mov ax, [es:6ch]
and ax, 0000001111111110b
mov di,ax
mov [offset app], di
mov ax, 0b800h
mov es, ax
mov al, '@'
mov ah, 154
mov [es:di], ax
ret
endp apple
proc delete
mov bx, offset stars
mov di, [st_am]
dec di
shl di, 1
mov di, [bx+di]
mov ax, 0b800h
mov es, ax
mov al, ' '
mov ah, 0
mov [es:di], ax
ret
endp delete
start:
mov ax, @data
mov ds, ax
; --------------------------
; Your code here
; --------------------------
mov ax, 0b800h
mov es, ax
mov si,0
mov al, ' '
mov ah, 0
call black
mov bx, offset stars
mov si, ((12*80+40)*2)-2
mov al, '*'
mov ah, 156
mov [es:si], ax
mov [bx], si
mov si, (12*80+40)*2
mov al, '*'
mov ah, 156
mov [es:si], ax
mov [bx+2], si
mov si, ((12*80+40)*2)+2
mov al, '*'
mov ah, 156
mov [es:si], ax
mov [bx+4], si
mov si, ((12*80+40)*2)-2
call apple
yesOrno:
mov ah, 1h
int 21h
mov [byte ptr saveal], al
cmp [byte ptr saveal], 'w'
jz w
cmp [byte ptr saveal], 'a'
jz a
cmp [byte ptr saveal], 's'
jz s
cmp [byte ptr saveal], 'd'
jz d
cmp [byte ptr saveal], 'q'
jmp exit
w:
call up
jmp yesOrno
s:
call down
jmp yesOrno
a:
call left
jmp yesOrno
d:
call right
jmp yesOrno
exit:
mov ax, 4c00h
int 21h
END start
I tried to think myself, but I couldn't think of something.
So sorry for asking a lot.
stars dw 0, 0, 0 ; addresses for the Head, Body, and Tail
You are not updating your stars array with the correct location of the snake's new head!
The mov [stars], si
instruction needs to go after the update of the SI register (be it minus 160, plus 160, minus 2, or plus 2).
You are writing a redundant *
in all of your movement procedures.
In move_up and move_down a redundant second *
is placed where the new head will be drawn. And in move_left and move_right a redundant second *
is placed where the old head is already drawn.
You can not allow the snake to move to a location that already contains a part of the snake! Just check whether the screen already has a *
and abandon the move accordingly. As an example the move_up code. The others are similar:
...
move_up:
lea di, [si - 160] ; Intended address for the new head
cmp byte [es:di], '*'
je not_move_up ; Illegal move
call delete
mov ax, [stars+2] \ These 3 lines really belong to
mov [stars+4], ax | the DELETE procedure (right before RET).
mov [stars+2], si / Try to not repeat too much code.
sub si, 160
mov [stars], si ; Update for the snake's new head
mov ax, 156 * 256 + '*'
mov [es:si], ax
not_move_up:
ret
endp up
proc apple mov ax, 40h mov es, ax mov ax, [es:6ch] and ax, 0000001111111110b mov di,ax mov [offset app], di
For me, this way to choose a random location for the apple is broken. Do you think there is anything wrong with the suggestion I made in the answer to your previous question?