summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fruit.asm54
-rw-r--r--src/input.asm19
-rw-r--r--src/main.asm107
3 files changed, 130 insertions, 50 deletions
diff --git a/src/fruit.asm b/src/fruit.asm
new file mode 100644
index 0000000..31226af
--- /dev/null
+++ b/src/fruit.asm
@@ -0,0 +1,54 @@
+section .data
+
+    ; position of the fruit
+    global fruit_x
+    global fruit_y
+    fruit_x db 0
+    fruit_y db 0
+
+section .text
+    global draw_fruit
+    global spawn_fruit
+    extern width
+    extern height
+    extern reset_cursor
+    extern write_byte
+    extern move_cursor_right
+    extern move_cursor_down
+
+spawn_fruit:
+
+    ; x position
+    rdtsc
+    shr ax, 5
+    div byte [width]
+    mov byte [fruit_x], ah
+;; TODO
+    rdtsc
+    shr ax, 5
+    div byte [height]
+    mov byte [fruit_y], ah
+
+    ret
+
+draw_fruit:
+    push rbx
+    call reset_cursor
+    mov bh, byte [fruit_x]
+    mov bl, byte [fruit_y]
+
+_30:call move_cursor_right
+    dec bh
+    cmp bh, 0
+    jnz _30
+_31:call move_cursor_down
+    dec bl
+    cmp bl, 0
+    jnz _31
+
+    mov rax, '*'
+    call write_byte
+
+    pop rbx
+    call reset_cursor
+    ret
diff --git a/src/input.asm b/src/input.asm
index 278b640..f411eed 100644
--- a/src/input.asm
+++ b/src/input.asm
@@ -1,5 +1,5 @@
 section .bss
-    input_buffer resb 1
+    input_buffer resb 250
 
 section .text
     global input
@@ -13,6 +13,7 @@ input:
 
 handle_input:
     mov al, byte [input_buffer]
+    mov ah, byte [direction]
     cmp al, 'w'
     je _input_up
     cmp al, 's'
@@ -26,15 +27,31 @@ handle_input:
     ret
 
 _input_up:
+    cmp ah, 1
+    jnz _set_up
+    ret
+_set_up:
     mov byte [direction], 3
     ret
 _input_down:
+    cmp ah, 3
+    jnz _set_down
+    ret
+_set_down:
     mov byte [direction], 1
     ret
 _input_left:
+    cmp ah, 0
+    jnz _set_left
+    ret
+_set_left:
     mov byte [direction], 2
     ret
 _input_right:
+    cmp ah, 2
+    jnz _set_right
+    ret
+_set_right:
     mov byte [direction], 0
     ret
 
diff --git a/src/main.asm b/src/main.asm
index 170f6a2..559e4bd 100644
--- a/src/main.asm
+++ b/src/main.asm
@@ -9,12 +9,8 @@ section .data
     ; array of snake - 1000 thousand bytes means max length is 500
     ; x head is at snake[0]
     ; y head is at snake[1]
-    snake TIMES 1000 db 0
-    snake_length dw 1
-
-    ; position of the fruit
-    fruit_x db 0
-    fruit_y db 0
+    snake TIMES 250 db 0
+    snake_length db 2
 
     ; direction of the snake:
     ;   3
@@ -23,6 +19,7 @@ section .data
     global direction
     direction db 0
 
+    ; delay of the game loop (0.5s in the beginning)
     timespec dq 0 
              dq 500000000
 
@@ -31,10 +28,14 @@ section .rodata
     ; (without the borders)
     global width
     global height
-    width db 70
-    height db 30
+    width db 25
+    height db 10
 
 section .text
+    extern fruit_x
+    extern fruit_y
+    extern spawn_fruit
+    extern draw_fruit
     extern input
     extern draw_border
     extern write_byte
@@ -49,27 +50,52 @@ section .text
 
 draw_snake:
     push rbx
+    ;;push r12
+    xor rdx, rdx
+
+    push rdx
     call reset_cursor
-    mov bh, byte [snake]
-    mov bl, byte [snake+1]
+    pop rdx
 
-_10:call move_cursor_right
+_12:
+    mov bh, byte [snake+(2*rdx)]
+    mov bl, byte [snake+(2*rdx)+1]
+
+_10:push rdx
+    call move_cursor_right
+    pop rdx
     dec bh
     cmp bh, 0
     jnz _10
-_11:call move_cursor_down
+_11:push rdx
+    call move_cursor_down
+    pop rdx
     dec bl
     cmp bl, 0
     jnz _11
 
+    push rdx
     mov rax, 'x'
     call write_byte
+    call reset_cursor
+    pop rdx
 
+    inc rdx
+    cmp dl, byte [snake_length]
+    jnz _12
+
+    ;;pop r12
     pop rbx
-    call reset_cursor
     ret
 
 move_snake:
+    ;; TODO: loop this
+    mov ah, byte [snake]
+    mov al, byte [snake+1]
+    mov byte [snake+2], ah
+    mov byte [snake+3], al
+    ;;
+
     cmp byte [direction], 0
     je _move_right
     cmp byte [direction], 1
@@ -78,7 +104,7 @@ move_snake:
     je _move_left
     cmp byte [direction], 3
     je _move_up
-    ret
+    ret ; should not be accessed
 
 _move_right:
     inc byte [snake]
@@ -93,48 +119,29 @@ _move_up:
     dec byte [snake+1]
     ret
 
-fruit_position:
-
-    ; x position
-    rdtsc
-    shr ax, 5
-    div byte [width]
-    mov byte [fruit_x], ah
-;; TODO
-    rdtsc
-    shr ax, 5
-    div byte [height]
-    mov byte [fruit_y], ah
-
+check_eat_fruit:
+    mov ah, byte [fruit_x]
+    mov al, byte [fruit_y]
+    cmp byte [snake], ah 
+    jz _check_y
     ret
-
-draw_fruit:
-    push rbx
-    call reset_cursor
-    mov bh, byte [fruit_x]
-    mov bl, byte [fruit_y]
-
-_30:call move_cursor_right
-    dec bh
-    cmp bh, 0
-    jnz _30
-_31:call move_cursor_down
-    dec bl
-    cmp bl, 0
-    jnz _31
-
-    mov rax, '*'
-    call write_byte
-
-    pop rbx
-    call reset_cursor
+_check_y:
+    cmp byte [snake+1], al
+    jz _same_position
+    ret
+_same_position:
+    ; get longer
+    inc byte [snake_length]
+    call spawn_fruit
     ret
 
 _start:
     call hide_cursor
     mov byte [snake], 5
     mov byte [snake+1], 5
-    call fruit_position
+    mov byte [snake+2], 4
+    mov byte [snake+3], 5
+    call spawn_fruit 
 
 main_loop:
     call clear_screen
@@ -149,11 +156,13 @@ main_loop:
 
     call input
     call move_snake
+    call check_eat_fruit
 
     jmp main_loop
 
 exit: ; exit syscall with return code 0
     call show_cursor
+    call clear_screen
     mov rax, 60                     
     xor rdi, rdi                    
     syscall