Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .model tiny
- .code
- .386
- locals
- org 100h
- screen_width = 320
- screen_heigth = 200
- platform_speed = 10
- columns_count = 320 / block_width
- lines_count = 80 / block_height
- block_width = 20
- block_height = 10
- ball_width = 8
- ball_speed = 5
- ball_height = 8
- platform_width = 42
- platform_height = 8
- start_platform_x = (screen_width - platform_width) / 2
- start_platform_y = screen_heigth - platform_height - ball_height
- start:
- ; 320x200
- mov ax, 13h
- int 10h
- mov ax, 0A000h
- mov es, ax
- call put_all_handlers
- game_loop:
- cmp [g_s], 0
- je game_loop
- cmp [g_s], 1
- je win
- lose:
- lea dx, you_lose
- jmp end_game
- win:
- lea dx, you_win
- end_game:
- mov ah, 09h
- int 21h
- call del_all_handlers
- call clear_keyboard_buffer
- mov ah, 4ch
- int 21h
- ret
- p_x dw start_platform_x
- p_y = start_platform_y
- p_m db 0
- p_d db 0
- b_x dw start_platform_x + (platform_width - ball_width) / 2
- b_y dw start_platform_y - ball_height
- b_m db 0
- bxd db 0
- byd db 0
- bdx db ball_speed
- bdy db ball_speed
- g_s db 0
- ; ------- HANDLERS UTILS -------
- interrupt_code db ?
- handler_offset dw ?
- save_interrupt dw ?
- move_vector_address_to_di proc
- mov al, [interrupt_code]
- mov ah, 0
- mov cl, 4
- mul cl
- mov di, ax
- ret
- endp move_vector_address_to_di
- put_handler proc
- push es
- xor ax, ax
- mov es, ax
- call move_vector_address_to_di
- mov bx, es:[di]
- mov cx, es:[di+2]
- mov ax, [handler_offset]
- mov es:[di], ax
- mov es:[di+2], ds
- mov di, [save_interrupt]
- mov [di], bx
- mov [di+2], cx
- pop es
- ret
- endp put_handler
- del_handler proc
- push es
- xor ax, ax
- mov es, ax
- mov di, [save_interrupt]
- mov bx, [di]
- mov dx, [di+2]
- call move_vector_address_to_di
- mov es:[di], bx
- mov es:[di+2], dx
- pop es
- ret
- endp del_handler
- put_09h_handler proc
- mov [interrupt_code], 09h
- lea bx, handler_09h
- mov [handler_offset], bx
- lea bx, old_09h
- mov [save_interrupt], bx
- call put_handler
- ret
- endp put_09h_handler
- put_1ch_handler proc
- mov [interrupt_code], 1ch
- lea bx, handler_1ch
- mov [handler_offset], bx
- lea bx, old_1ch
- mov [save_interrupt], bx
- call put_handler
- ret
- endp put_1ch_handler
- del_09h_handler proc
- mov [interrupt_code], 09h
- lea bx, old_09h
- mov [save_interrupt], bx
- call del_handler
- ret
- endp del_09h_handler
- del_1ch_handler proc
- mov [interrupt_code], 1ch
- lea bx, old_1ch
- mov [save_interrupt], bx
- call del_handler
- ret
- endp del_1ch_handler
- put_all_handlers proc
- call put_09h_handler
- call put_1ch_handler
- ret
- endp put_all_handlers
- del_all_handlers proc
- call del_09h_handler
- call del_1ch_handler
- ret
- endp del_all_handlers
- ; ------- KEYBOARD UTILS -------
- clear_keyboard_buffer proc
- cli
- push es
- xor ax, ax
- mov es, ax
- mov al, es:[41ah]
- mov es:[41ch], al
- pop es
- sti
- ret
- endp clear_keyboard_buffer
- button db 0
- left_button = 1
- right_button = 2
- space_button = 3
- other_button = 10
- get_button proc
- cmp al, 1
- je end_game
- mov ah, 1
- cmp al, 75
- je @find
- inc ah
- cmp al, 77
- je @find
- inc ah
- cmp al, 57
- je @find
- mov [button], 10
- ret
- @find:
- mov [button], ah
- ret
- endp get_button
- ; ------- HANDLERS FUNCTIONS -------
- old_09h dw ?, ?
- handler_09h proc
- pushf
- call dword ptr old_09h
- in al, 60h
- test al, 10000000b
- jnz @release
- @press:
- call get_button
- cmp [button], right_button
- je right
- cmp [button], space_button
- je space
- cmp [button], left_button
- je left
- iret
- left:
- mov [p_m], 1
- mov [p_d], 0
- iret
- right:
- mov [p_m], 1
- mov [p_d], 1
- iret
- space:
- mov [b_m], 1
- iret
- @release:
- mov [p_m], 0
- iret
- endp handler_09h
- old_1ch dw ?, ?
- handler_1ch proc
- cmp [p_m], 1
- jne skip_move_platform
- call move_platform
- skip_move_platform:
- cmp [b_m], 1
- jne skip_move_ball
- call move_ball
- skip_move_ball:
- call draw_platform
- call draw_ball
- cmp [map_changed], 1
- jne skip_draw_map
- call draw_map
- mov [map_changed], 0
- skip_draw_map:
- iret
- endp handler_1ch
- ; ------- SPRITES FUNCTIONS -------
- ; bx - sprite addr
- x dw 0 ; x coordinate
- y dw 0 ; y coordinate
- w dw 0 ; width
- h dw 0 ; height
- draw_pixels proc
- mov ax, [y]
- mov cx, 320
- mul cx
- add ax, [x]
- xor si, si
- mov di, ax
- mov cx, [h]
- lines:
- push cx
- mov cx, [w]
- columns:
- mov al, [bx][si]
- inc si
- stosb
- loop columns
- pop cx
- add di, 320
- sub di, [w]
- loop lines
- ret
- endp draw_pixels
- xc dw 0 ; x coordinate
- yc dw 0 ; y coordinate
- wc dw 0 ; width
- hc dw 0 ; height
- cc db 0 ; color for clean
- clear_pixels proc
- push ax
- push bx
- push cx
- push di
- mov ax, [yc]
- mov cx, 320
- mul cx
- add ax, [xc]
- mov di, ax
- mov cx, [hc]
- @@lines:
- push cx
- mov cx, [wc]
- @@columns:
- mov al, [cc]
- stosb
- loop @@columns
- pop cx
- add di, 320
- sub di, [wc]
- loop @@lines
- pop di
- pop cx
- pop bx
- pop ax
- ret
- endp clear_pixels
- ; bx - sprite nums addr
- mov_sprite_nums proc
- mov ax, word ptr [bx]
- mov [w], ax
- mov ax, word ptr [bx+2]
- mov [h], ax
- add bx, 4
- ret
- endp mov_sprite_nums
- clear_sprite_nums proc
- mov [w], 0
- mov [h], 0
- mov [x], 0
- mov [y], 0
- ret
- endp clear_sprite_nums
- clear_clear_nums proc
- mov [wc], 320
- mov [hc], 200
- mov [xc], 0
- mov [yc], 0
- ret
- endp clear_clear_nums
- ; ------- GAME FUNCTIONS -------
- chc db 0 ; check column
- chl db 0 ; check line
- check_point proc
- mov ax, [b_x]
- mov cl, block_width
- div cl
- cmp [b_x], ball_speed + 2
- jle left_border
- cmp [b_x], screen_width - (ball_speed + 2) - ball_width
- jge right_border
- cmp [b_y], start_platform_y - ball_height
- jge bottom_border
- cmp [b_y], ball_speed + 2
- jle top_border
- mov [chc], al
- cmp [chc], columns_count
- jg end_check_point
- mov ax, [b_y]
- mov cl, block_height
- div cl
- mov [chl], al
- cmp [chl], lines_count
- jg end_check_point
- lea bx, map
- mov ax, columns_count
- mul [chl]
- add bx, ax
- mov al, [chc]
- mov ah, 0
- add bx, ax
- sub bx, columns_count
- mov al, [bx]
- cmp al, 0
- je end_check_point
- cmp al, 0ffh
- je end_check_point
- mov al, [chc]
- mov ah, 0
- mov cl, block_width
- mul cl
- cmp [b_x], ax
- jl end_check_point
- add ax, block_width
- cmp ax, [b_x]
- jl end_check_point
- mov al, [chl]
- mov ah, 0
- mov cl, block_height
- mul cl
- cmp [b_y], ax
- jl end_check_point
- add ax, block_height
- cmp ax, [b_y]
- jl end_check_point
- cmp [byd], 0
- jne zero_dir
- mov [byd], 1
- jmp end_zero_dir
- zero_dir:
- mov [byd], 0
- end_zero_dir:
- mov al, [bx]
- cmp al, 0
- je skip_dec_al
- dec al
- dec [map_sum]
- cmp [map_sum], 0
- jne skip_dec_al
- mov [g_s], 1
- skip_dec_al:
- mov [bx], al
- mov [map_changed], 1
- end_check_point:
- ret
- left_border:
- mov [bxd], 1
- ret
- right_border:
- mov [bxd], 0
- ret
- bottom_border:
- mov ax, [b_y]
- cmp ax, screen_heigth - ball_speed
- jg end_game_f
- mov ax, [p_x]
- sub ax, ball_width / 2
- cmp [b_x], ax
- jl end_check_point
- add ax, platform_width + ball_width
- cmp [b_x], ax
- jg end_check_point
- mov [byd], 0
- mov ax, [p_x]
- add ax, platform_width / 2
- mov dx, [b_x]
- add ax, ball_width / 2
- cmp ax, dx
- jl ax_l
- sub ax, dx
- cmp ax, platform_width / 3
- jl sub_angle
- jmp add_angle
- ax_l:
- sub dx, ax
- cmp dx, platform_width / 3
- jl sub_angle
- sub_angle:
- cmp [bdx], 2
- jl end_check_point
- dec [bdx]
- jmp end_check_point
- add_angle:
- cmp [bdx], ball_speed + 2
- jg end_check_point
- inc [bdx]
- jmp end_check_point
- end_game_f:
- mov [g_s], 2
- ret
- top_border:
- mov [byd], 1
- ret
- endp check_point
- draw_map proc
- push ax
- push cx
- push bx
- mov [xc], 0
- mov [yc], 0
- mov [wc], block_width-1
- mov [hc], block_height
- lea bx, map
- mov al, 0
- mov cx, lines_count
- @@lines:
- push cx
- mov cx, columns_count
- @@columns:
- push ax
- xlat
- call get_block_color
- cmp al, 0ffh
- je skip_clear_pixels
- mov [cc], al
- cmp al, 0
- jne end_black_color
- black_color:
- mov di, bx
- mov ah, 0
- add di, ax
- mov al, 0ffh
- scasb
- end_black_color:
- call clear_pixels
- skip_clear_pixels:
- pop ax
- inc al
- add [xc], block_width
- loop @@columns
- add [yc], block_height
- pop cx
- loop @@lines
- pop bx
- pop cx
- pop ax
- ret
- endp draw_map
- get_block_color proc
- push bx
- lea bx, block_colors
- xlat
- pop bx
- ret
- endp get_block_color
- draw_ball proc
- lea bx, ball_nums
- call mov_sprite_nums
- mov ax, [b_x]
- mov [x], ax
- mov ax, [b_y]
- mov [y], ax
- call draw_pixels
- ret
- endp draw_ball
- draw_platform proc
- lea bx, platform_nums
- call mov_sprite_nums
- mov ax, [p_x]
- mov [x], ax
- mov ax, p_y
- mov [y], ax
- call draw_pixels
- ret
- endp draw_platform
- clear_ball proc
- mov ax, ball_width
- mov [wc], ax
- mov ax, ball_height
- mov [hc], ax
- mov [cc], 0
- mov ax, [b_x]
- mov [xc], ax
- mov ax, [b_y]
- mov [yc], ax
- call clear_pixels
- ret
- endp clear_ball
- clear_platform proc
- mov ax, platform_width
- mov [wc], ax
- mov ax, platform_height
- mov [hc], ax
- mov [cc], 0
- mov ax, [p_x]
- mov [xc], ax
- mov ax, p_y
- mov [yc], ax
- call clear_pixels
- ret
- endp clear_platform
- move_platform proc
- call clear_platform
- mov ax, [p_x]
- cmp [p_d], 0
- je @@left
- @@right:
- add ax, platform_speed
- cmp ax, 320 - platform_width
- jge skip_move_right
- mov [p_x], ax
- skip_move_right:
- cmp [b_m], 0
- je ball_on_platform
- ret
- @@left:
- sub ax, platform_speed
- cmp ax, 1
- jle skip_move_left
- mov [p_x], ax
- skip_move_left:
- cmp [b_m], 0
- je ball_on_platform
- ret
- ball_on_platform:
- call clear_ball
- mov ax, [p_x]
- mov [b_x], ax
- add [b_x], (platform_width - ball_width) / 2
- ret
- endp move_platform
- move_ball proc
- cmp [b_m], 0
- je @@ret
- call clear_ball
- mov al, [bdx]
- mov ah, 0
- cmp [bxd], 0
- je sub_bx
- add [b_x], ax
- jmp add_bx
- sub_bx:
- sub [b_x], ax
- add_bx:
- mov al, [bdy]
- mov ah, 0
- cmp [byd], 0
- je sub_by
- add [b_y], ax
- jmp add_by
- sub_by:
- sub [b_y], ax
- add_by:
- call check_point
- @@ret:
- ret
- endp move_ball
- ; ------- MAPS -------
- block_colors db 0,1,5,4
- map db 1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1
- db 1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1
- db 1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1
- db 1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1
- db 1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1
- db 1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1
- db 1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1,1,1,2,3,3,2,1,1
- db 1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1
- map_sum dw 2
- map_changed db 1
- ; ------- SPRITES -------
- platform_nums dw 42, 8
- platform db 0,0,0,4 dup (4),0,26 dup(8),0,4 dup(4),0,0,0
- db 0,0,4,12,12,12,4,0,26 dup (7),0,4 dup (12),4
- db 0,0,0,1,12,3 dup(15),4,0,26 dup(15),0,3 dup(15)
- db 12,4,1,0,0,1,4 dup(12),4,0,26 dup(7),0,4 dup(12)
- db 4,1,0,1,9,4 dup(12),4,0,26 dup(7),0,4 dup(12),4
- db 9,1,0,9,5 dup(4),0,26 dup(7),0,5 dup(4),9,0,0,0
- db 5 dup(4),0,26 dup(7),0,5 dup(4),0,0,0,0,0
- db 4 dup(4),0,26 dup(8),0,4 dup(4),0,0,0
- ball_nums dw 8, 8
- ball db 0,0, 2,10,10,2, 0,0
- db 0, 2,10,10,10,10,2 ,0
- db 0, 2,10,10,10,10,2 ,0
- db 2,10,10,10,10,10,10,2
- db 2,10,10,10,10,10,10,2
- db 0, 2,2,10,10,2,2 ,0
- db 0, 2,2,2,2,2,2 ,0
- db 0,0, 2,2,2,2 ,0,0
- you_win db 'You win!', 0dh, 0ah, '$'
- you_lose db 'You lose', 0dh, 0ah, '$'
- end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement