Files
crawos/source/kernel/games/snake.asm
2025-10-20 10:52:03 +01:00

95 lines
1.6 KiB
NASM

game_snake:
call .draw_screen
call .detect_input
.draw_screen:
call os_set_graphics_mode ; Clear screen
.draw_snake_loop:
mov ax, [snake_x]
mov [x_start], ax
mov [x_end], ax
mov bx, [snake_y]
mov [y_start], bx
mov [y_end], bx
call os_draw_graphical_rectangle
ret
; Player 1 movements
.up:
mov dword [snake_direction], 0
jmp .end_detect_input
.down:
mov dword [snake_direction], 2
jmp .end_detect_input
.left:
mov dword [snake_direction], 3
jmp .end_detect_input
.right:
mov dword [snake_direction], 1
jmp .end_detect_input
.move_up:
mov dx, [snake_y]
sub dx, 1
mov [snake_y], dx
ret
.move_down:
mov dx, [snake_y]
add dx, 1
mov [snake_y], dx
ret
.move_left:
mov dx, [snake_x]
sub dx, 1
mov [snake_x], dx
ret
.move_right:
mov dx, [snake_x]
add dx, 1
mov [snake_x], dx
ret
.move_snake:
cmp dword [snake_direction], 0
je .move_up
cmp dword [snake_direction], 1
je .move_right
cmp dword [snake_direction], 2
je .move_down
; Else it must be left
jmp .move_left
.detect_input:
call os_read_input
cmp al, 08h
je .finish
; Player 1
cmp al, 77h ; Pressed 'w' up
je .up
cmp al, 61h ; Pressed 'a' left
je .left
cmp al, 73h ; Pressed 's' down
je .down
cmp al, 64h ; Pressed 'd' right
je .right
.end_detect_input:
call .move_snake
call .draw_screen
jmp .detect_input
.finish:
call os_set_text_mode
call os_start_cli
section .data:
snake_x: dw 5
snake_y: dw 5
snake_direction: dw 1 ; 0=up, 1=right, 2=down, 3=left
snake_length: dw 1