我寫的蘋果
==
在主函式加這個
``` cpp
call getinput
call makefloor
call makewall
...
"⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣⭣"
call makeapple
"⭡⭡⭡⭡⭡⭡⭡⭡⭡⭡⭡⭡⭡"
...
call campus
call drawCross
```
makeapple
--
```
;--------------------------------------------
makeapple PROC uses eax ebx ecx edx esi edi
; create apple in screen buffer (use ray-casting)
; Input: Nothing
; Output: Nothing
;--------------------------------------------
mov esi, camd
sub esi, SCREEN_W/2
cmp esi, 0
mov edi, 0
jl w_negative
jmp w_positive
w_negative:
add esi, 360
w_positive:
mov dl, SCREEN_W-1
mov ebx, 0
push ebx
w_loop:
cmp dl, 0
jl w_loop_done ;(counter)loop through fov angles and shoot rays
.IF esi >= 360
sub esi, 360
.ENDIF
mov eax, esi ;save angle to eax
call shootray_for_apple ;shoot ray to current direction vector
mov eax, ebx ;save distance to eax
cmp ebx, 0
jl no_intersect
.if ecx == 4
pop ebx
.if bl == 0
mov bl, dl ;bl is right aplle
.endif
mov bh, dl ;bh is left apple
push ebx
.endif
no_intersect:
inc esi
dec dl
jmp w_loop
w_loop_done:
pop ebx
.if bl != bh
mov cl, bl
sub cl, bh
mov dh, SCREEN_H/2
mov dl, bh
add dl, bl
shr dl, 1
; 19, 12, 7, 5, 3
.if cl > 3
.if cl > 19
mov ecx, 1
.elseif cl > 12
mov ecx, 2
.elseif cl > 7
mov ecx, 3
.elseif cl > 5
mov ecx, 4
.else
mov ecx, 5
.endif
call fillapple
.endif
.endif
ret
makeapple ENDP
```
shootray_for_apple
---
```
;--------------------------------------------
shootray_for_apple PROC uses eax edx esi ebp
; cast ray to selected angle
; Input: eax=angle
; Output: ebx=max length, ecx=type, edi=2nd length
;--------------------------------------------
mov esi, eax
mov ebx, -1
mov ecx, 0
mov edi, 0
;loop through all blocks
mov edx, 0
.WHILE dh < WORLD_Y ;loop y
.WHILE dl < WORLD_X ;loop x
call wctoi ;index is in eax
movzx eax, world[eax]
mov ebp, eax
.IF eax == 1 || eax == 4 ; is a block
call wctorc
;south
mov eax, tmp[0*TYPE vector].x
mov tmp[1*TYPE vector].x, eax
mov eax, tmp[0*TYPE vector].y
mov tmp[1*TYPE vector].y, eax
mov eax, tmp[1*TYPE vector].x
add eax, BLOCK_SIZE
mov tmp[1*TYPE vector].x, eax
call intersect
.IF eax < ebx || ebx == -1
mov edi, ebx
mov ebx, eax
mov ecx, ebp
.ENDIF
;east
add tmp[0*TYPE vector].x, BLOCK_SIZE
add tmp[1*TYPE vector].y, BLOCK_SIZE
call intersect
.IF eax < ebx || ebx == -1
mov edi, ebx
mov ebx, eax
mov ecx, ebp
.ENDIF
;south
add tmp[0*TYPE vector].y, BLOCK_SIZE
sub tmp[1*TYPE vector].x, BLOCK_SIZE
call intersect
.IF eax < ebx || ebx == -1
mov edi, ebx
mov ebx, eax
mov ecx, ebp
.ENDIF
;west
sub tmp[0*TYPE vector].x, BLOCK_SIZE
sub tmp[1*TYPE vector].y, BLOCK_SIZE
call intersect
.IF eax < ebx || ebx == -1
mov edi, ebx
mov ebx, eax
mov ecx, ebp
.ENDIF
.ENDIF
inc dl
.ENDW
mov dl, 0
inc dh
.ENDW
ret
shootray_for_apple ENDP
```
fillapple
--
```
fillapple proc uses eax esi
; fill apple in screen buffer (use ray-casting)
; Input: edx(center coordinate), ecx(apple type)
; Output: Nothing
;--------------------------------------------
; 將圖案字串的地址載入 esi
.if ecx == 1
lea esi, apple_pic1
.elseif ecx == 2
lea esi, apple_pic2
.elseif ecx == 3
lea esi, apple_pic3
.elseif ecx == 4
lea esi, apple_pic4
.elseif ecx == 5
lea esi, apple_pic5
.endif
sub dl, [esi]
inc esi
sub dh, [esi]
inc esi
mov bh, dl
loop_h:
lodsb ; 載入字元到 AL
mov bl, al
cmp bl, 0 ; 讀完了
je done
cmp bl, 0dh ; 檢查是否是換行符
je new_line ; 如果是換行符,跳到新行
cmp bl, '*'
je skip
; 計算索引
call ctoi ; 呼叫 ctoi,結果存入 EAX
mov pixels[eax], bl ; 將圖案字元寫入 pixels 陣列
skip:
inc dl
jmp loop_h
new_line:
inc dh ; y 值加 1
mov dl, bh
jmp loop_h ; 繼續處理
done:
ret
fillapple endp
```