After first colliding with the apple it somtimes moves but after that u have no chance of getting it to move. this is checked using the check_collision function the compers both the x and y of the apple to the x and y of the snake.
IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
Y dw 100
X dw 100
Board dw 200
TIME_AUX DB 0
PressFlag db 0
SnakeBody dw 8
Xapple dw 80
yapple dw 80
Random dw 50
; --------------------------
CODESEG
PROC moves
MOV AH, 01h ; Function 01h - Check for Key Press
INT 16h
JZ exit_moves ; Jump if ZF is set (no key pressed)
MOV AH, 00h ; Function 00h - Read Key Stroke
INT 16h
; Check if AH contains 'W' (ASCII value 87)
CMP AL, 'w'
je w_pressed
; Check if AH contains 'S' (ASCII value 83)
CMP AL, 's'
je s_pressed
; Check if AH contains 'D' (ASCII value 68)
CMP AL, 'd'
je d_pressed
; Check if AH contains 'A' (ASCII value 65)
CMP AL, 'a'
je a_pressed
; Check if AH contains '1B' (Escape key)
exit_moves:
RET
ENDP moves
PROC w_pressed
mov [PressFlag], 1
SUB [Y], 5
CHECK_TIME1: ;time checking loop
MOV AH,2Ch ;get the system time
INT 21h ;is the current time equal to the previous one(TIME_AUX)?
CMP DL,[TIME_AUX] ;is the current time equal to the previous one(TIME_AUX)?
JE CHECK_TIME1
MOV [TIME_AUX],DL
CALL draw_player
call moves
CMP [PressFlag],1
je w_pressed
RET
ENDP w_pressed
PROC d_pressed
mov [PressFlag], 2
ADD [X], 5
CHECK_TIME2: ;time checking loop
MOV AH,2Ch ;get the system time
INT 21h ;is the current time equal to the previous one(TIME_AUX)?
CMP DL,[TIME_AUX] ;is the current time equal to the previous one(TIME_AUX)?
JE CHECK_TIME2
MOV [TIME_AUX],DL
CALL draw_player
call moves
cmp [PressFlag],2
je d_pressed
ret
ENDP d_pressed
PROC a_pressed
mov [PressFlag], 3
SUB [X], 5
CHECK_TIME3: ;time checking loop
MOV AH,2Ch ;get the system time
INT 21h ;is the current time equal to the previous one(TIME_AUX)?
CMP DL,[TIME_AUX] ;is the current time equal to the previous one(TIME_AUX)?
JE CHECK_TIME3
MOV [TIME_AUX],DL
CALL draw_player
call moves
CMP [PressFlag],3
je a_pressed
ret
ENDP a_pressed
PROC s_pressed
ADD [Y], 5
mov [PressFlag], 4
CHECK_TIME4: ;time checking loop
MOV AH,2Ch ;get the system time
INT 21h ;is the current time equal to the previous one(TIME_AUX)?
CMP DL,[TIME_AUX] ;is the current time equal to the previous one(TIME_AUX)?
JE CHECK_TIME4
MOV [TIME_AUX],DL
CALL draw_player
call moves
CMP [PressFlag],4
je s_pressed
RET
ENDP s_pressed
PROC draw_player
MOV AX, 13h
INT 10h
MOV AX, 0C07h ; Function 0Ch, Set Pixel Color
MOV BH, 0 ; Page number (usually 0 in mode 13h)
MOV CX, [X] ; X-coordinate
MOV DX, [Y] ; Y-coordinate
MOV SI, 0
MOV DI, 0
MOV AL, 2; Color green
draw_row_loop:
draw_pixel_loop:
INC CX
INC DI
INT 10h
CMP DI, [SnakeBody]
JNE draw_pixel_loop
SUB CX, [SnakeBody]
MOV DI, 0
INC DX
INC SI
CMP SI, [SnakeBody]
JNE draw_row_loop
CALL check_collision
CALL draw_apple
RET
ENDP draw_player
PROC check_collision
mov ax, [yapple]
cmp ax, [Y]
jne skip_draw_apple
mov bx, [Xapple]
cmp bx, [X]
jne skip_draw_apple
call rnd
mov dx, [Random]
mov [Xapple], dx
CALL draw_apple
RET
skip_draw_apple:
RET
ENDP check_collision
PROC draw_apple
MOV AX, 0C07h ; Function 0Ch, Set Pixel Color
MOV BH, 0 ; Page number (usually 0 in mode 13h)
MOV CX, [Xapple] ; X-coordinate
MOV DX, [yapple] ; Y-coordinate
MOV AL, 2; Color red
INT 10h
RET
ENDP draw_apple
Proc rnd
mov ah, 2ch
int 21h
mov ax , [Random]
mov [byte ptr Random], dl
ADD [Random] , AX
ret
ENDP rnd
PROC game_logic
CHECK_TIME: ;time checking loop
MOV AH, 2Ch ;get the system time
INT 21h ;is the current time equal to the previous one(TIME_AUX)?
CMP DL, [TIME_AUX] ;is the current time equal to the previous one(TIME_AUX)?
JE CHECK_TIME ;if it is the same, check again
; If it reaches this point, it's because the time has passed
MOV [TIME_AUX], DL ;update time
CALL moves
CALL draw_player
RET
ENDP game_logic
MOV AX, 13h
INT 10h ; Set video mode 13h (320x200 pixels, 256 colors)
start:
MOV AX, @data
MOV DS, AX
MOV AX, 13h
INT 10h ; Set video mode 13h (320x200 pixels, 256 colors)
game_loop:
CALL game_logic
JMP game_loop
exit:
MOV AX, 4C00h
INT 21h
END start
I dont even know what to do to fix this im a begginer of x86 Tasm assembly and I was excpecting for this to be straight forward
The pixels were not correctly aligned so when the code tried to check for collision it check all the
yvalues of the snake and all theyvalues of the apple and same thing for thexvalues, that would not work because they are not the same size making the into a lucky draw if you will collide or not. I patched it by setting both the apple and the snake to the same size.