Advertisement
Guest User

Untitled

a guest
May 29th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .model tiny
  2. .code
  3. .386
  4. locals
  5. org 100h
  6.  
  7. screen_width = 320
  8. screen_heigth = 200
  9. platform_speed = 10
  10. columns_count = 320 / block_width
  11. lines_count = 80 / block_height
  12. block_width = 20
  13. block_height = 10
  14. ball_width = 8
  15. ball_speed = 5
  16. ball_height = 8
  17. platform_width = 42
  18. platform_height = 8
  19.  
  20. start_platform_x = (screen_width - platform_width) / 2
  21. start_platform_y = screen_heigth - platform_height - ball_height
  22.  
  23. start:
  24.   ; 320x200
  25.   mov ax, 13h
  26.   int 10h
  27.   mov ax, 0A000h
  28.   mov es, ax
  29.  
  30.   call put_all_handlers
  31.  
  32.   game_loop:
  33.   cmp [g_s], 0
  34.   je game_loop
  35.  
  36.   cmp [g_s], 1
  37.   je win
  38.  
  39.   lose:
  40.   lea dx, you_lose
  41.   jmp end_game
  42.  
  43.   win:
  44.   lea dx, you_win
  45.  
  46.   end_game:
  47.   mov ah, 09h
  48.   int 21h
  49.  
  50.   call del_all_handlers
  51.   call clear_keyboard_buffer
  52.   mov ah, 4ch
  53.   int 21h
  54.   ret
  55.  
  56.  
  57. p_x dw start_platform_x
  58. p_y = start_platform_y
  59. p_m db 0
  60. p_d db 0
  61.  
  62. b_x dw start_platform_x + (platform_width - ball_width) / 2
  63. b_y dw start_platform_y - ball_height
  64. b_m db 0
  65. bxd db 0
  66. byd db 0
  67. bdx db ball_speed
  68. bdy db ball_speed
  69.  
  70. g_s db 0
  71.  
  72. ; ------- HANDLERS UTILS -------
  73.  
  74.   interrupt_code db ?
  75.   handler_offset dw ?
  76.   save_interrupt dw ?
  77.  
  78.   move_vector_address_to_di proc
  79.     mov al, [interrupt_code]
  80.     mov ah, 0
  81.     mov cl, 4
  82.     mul cl
  83.     mov di, ax
  84.     ret
  85.   endp move_vector_address_to_di
  86.  
  87.   put_handler proc
  88.     push es
  89.     xor ax, ax
  90.     mov es, ax
  91.     call move_vector_address_to_di
  92.     mov bx, es:[di]
  93.     mov cx, es:[di+2]
  94.     mov ax, [handler_offset]
  95.     mov es:[di], ax
  96.     mov es:[di+2], ds
  97.     mov di, [save_interrupt]
  98.     mov [di], bx
  99.     mov [di+2], cx
  100.     pop es
  101.     ret
  102.   endp put_handler
  103.  
  104.   del_handler proc
  105.     push es
  106.     xor ax, ax
  107.     mov es, ax
  108.     mov di, [save_interrupt]
  109.     mov bx, [di]
  110.     mov dx, [di+2]
  111.     call move_vector_address_to_di
  112.     mov es:[di], bx
  113.     mov es:[di+2], dx
  114.     pop es
  115.     ret
  116.   endp del_handler
  117.  
  118.   put_09h_handler proc
  119.     mov [interrupt_code], 09h
  120.     lea bx, handler_09h
  121.     mov [handler_offset], bx
  122.     lea bx, old_09h
  123.     mov [save_interrupt], bx
  124.     call put_handler
  125.     ret
  126.   endp put_09h_handler
  127.  
  128.   put_1ch_handler proc
  129.     mov [interrupt_code], 1ch
  130.     lea bx, handler_1ch
  131.     mov [handler_offset], bx
  132.     lea bx, old_1ch
  133.     mov [save_interrupt], bx
  134.     call put_handler
  135.     ret
  136.   endp put_1ch_handler
  137.  
  138.   del_09h_handler proc
  139.     mov [interrupt_code], 09h
  140.     lea bx, old_09h
  141.     mov [save_interrupt], bx
  142.     call del_handler
  143.     ret
  144.   endp del_09h_handler
  145.  
  146.   del_1ch_handler proc
  147.     mov [interrupt_code], 1ch
  148.     lea bx, old_1ch
  149.     mov [save_interrupt], bx
  150.     call del_handler
  151.     ret
  152.   endp del_1ch_handler
  153.  
  154.   put_all_handlers proc
  155.     call put_09h_handler
  156.     call put_1ch_handler
  157.     ret
  158.   endp put_all_handlers
  159.  
  160.   del_all_handlers proc
  161.     call del_09h_handler
  162.     call del_1ch_handler
  163.     ret
  164.   endp del_all_handlers
  165.  
  166. ; ------- KEYBOARD UTILS -------
  167.  
  168.   clear_keyboard_buffer proc
  169.     cli
  170.     push es
  171.     xor ax, ax
  172.     mov es, ax
  173.     mov al, es:[41ah]
  174.     mov es:[41ch], al
  175.     pop es
  176.     sti
  177.     ret
  178.   endp clear_keyboard_buffer
  179.  
  180.   button db 0
  181.   left_button = 1
  182.   right_button = 2
  183.   space_button = 3
  184.   other_button = 10
  185.   get_button proc
  186.     cmp al, 1
  187.     je end_game
  188.     mov ah, 1
  189.     cmp al, 75
  190.     je @find
  191.     inc ah
  192.     cmp al, 77
  193.     je @find
  194.     inc ah
  195.     cmp al, 57
  196.     je @find
  197.     mov [button], 10
  198.     ret
  199.  
  200.     @find:
  201.       mov [button], ah
  202.       ret
  203.   endp get_button
  204.  
  205. ; ------- HANDLERS FUNCTIONS -------
  206.  
  207.   old_09h dw ?, ?
  208.   handler_09h proc
  209.     pushf
  210.     call dword ptr old_09h
  211.     in al, 60h
  212.     test al, 10000000b
  213.     jnz @release
  214.  
  215.     @press:
  216.       call get_button
  217.       cmp [button], right_button
  218.       je right
  219.       cmp [button], space_button
  220.       je space
  221.       cmp [button], left_button
  222.       je left
  223.       iret
  224.  
  225.       left:
  226.       mov [p_m], 1
  227.       mov [p_d], 0
  228.       iret
  229.  
  230.       right:
  231.       mov [p_m], 1
  232.       mov [p_d], 1
  233.       iret
  234.  
  235.       space:
  236.       mov [b_m], 1
  237.       iret
  238.  
  239.     @release:
  240.       mov [p_m], 0
  241.       iret
  242.   endp handler_09h
  243.  
  244.   old_1ch dw ?, ?
  245.   handler_1ch proc
  246.     cmp [p_m], 1
  247.     jne skip_move_platform
  248.     call move_platform
  249.     skip_move_platform:
  250.  
  251.     cmp [b_m], 1
  252.     jne skip_move_ball
  253.     call move_ball
  254.     skip_move_ball:
  255.  
  256.     call draw_platform
  257.     call draw_ball
  258.     cmp [map_changed], 1
  259.     jne skip_draw_map
  260.     call draw_map
  261.     mov [map_changed], 0
  262.     skip_draw_map:
  263.     iret
  264.   endp handler_1ch
  265.  
  266. ; ------- SPRITES FUNCTIONS -------
  267.  
  268.   ; bx - sprite addr
  269.   x dw 0  ; x coordinate
  270.   y dw 0  ; y coordinate
  271.   w dw 0  ; width
  272.   h dw 0  ; height
  273.   draw_pixels proc
  274.     mov ax, [y]
  275.     mov cx, 320
  276.     mul cx
  277.     add ax, [x]
  278.  
  279.     xor si, si
  280.     mov di, ax
  281.     mov cx, [h]
  282.    
  283.     lines:
  284.       push cx
  285.       mov cx, [w]
  286.       columns:
  287.         mov al, [bx][si]
  288.         inc si
  289.         stosb
  290.       loop columns
  291.       pop cx
  292.       add di, 320
  293.       sub di, [w]
  294.     loop lines
  295.     ret
  296.   endp draw_pixels
  297.  
  298.   xc dw 0  ; x coordinate
  299.   yc dw 0  ; y coordinate
  300.   wc dw 0  ; width
  301.   hc dw 0  ; height
  302.   cc db 0  ; color for clean
  303.   clear_pixels proc
  304.     push ax
  305.       push bx
  306.         push cx
  307.           push di
  308.  
  309.     mov ax, [yc]
  310.     mov cx, 320
  311.     mul cx
  312.     add ax, [xc]
  313.     mov di, ax
  314.     mov cx, [hc]
  315.  
  316.     @@lines:
  317.       push cx
  318.       mov cx, [wc]
  319.       @@columns:
  320.         mov al, [cc]
  321.         stosb
  322.       loop @@columns
  323.       pop cx
  324.       add di, 320
  325.       sub di, [wc]
  326.     loop @@lines
  327.  
  328.           pop di
  329.         pop cx
  330.       pop bx
  331.     pop ax
  332.     ret
  333.   endp clear_pixels
  334.  
  335.   ; bx - sprite nums addr
  336.   mov_sprite_nums proc
  337.     mov ax, word ptr [bx]
  338.     mov [w], ax
  339.     mov ax, word ptr [bx+2]
  340.     mov [h], ax
  341.     add bx, 4
  342.     ret
  343.   endp mov_sprite_nums
  344.  
  345.   clear_sprite_nums proc
  346.     mov [w], 0
  347.     mov [h], 0
  348.     mov [x], 0
  349.     mov [y], 0
  350.     ret
  351.   endp clear_sprite_nums
  352.  
  353.   clear_clear_nums proc
  354.     mov [wc], 320
  355.     mov [hc], 200
  356.     mov [xc], 0
  357.     mov [yc], 0
  358.     ret
  359.   endp clear_clear_nums
  360.  
  361.  
  362. ; ------- GAME FUNCTIONS -------
  363.  
  364.   chc db 0  ; check column
  365.   chl db 0  ; check line
  366.   check_point proc
  367.     mov ax, [b_x]
  368.     mov cl, block_width
  369.     div cl
  370.  
  371.     cmp [b_x], ball_speed + 2
  372.     jle left_border
  373.  
  374.     cmp [b_x], screen_width - (ball_speed + 2) - ball_width
  375.     jge right_border
  376.  
  377.     cmp [b_y], start_platform_y - ball_height
  378.     jge bottom_border
  379.  
  380.     cmp [b_y], ball_speed + 2
  381.     jle top_border
  382.  
  383.     mov [chc], al
  384.  
  385.     cmp [chc], columns_count
  386.     jg end_check_point
  387.  
  388.     mov ax, [b_y]
  389.     mov cl, block_height
  390.     div cl
  391.     mov [chl], al
  392.  
  393.     cmp [chl], lines_count
  394.     jg end_check_point
  395.  
  396.  
  397.     lea bx, map
  398.     mov ax, columns_count
  399.     mul [chl]
  400.     add bx, ax
  401.     mov al, [chc]
  402.     mov ah, 0
  403.     add bx, ax
  404.  
  405.     sub bx, columns_count
  406.  
  407.     mov al, [bx]
  408.     cmp al, 0
  409.     je end_check_point
  410.     cmp al, 0ffh
  411.     je end_check_point
  412.  
  413.     mov al, [chc]
  414.     mov ah, 0
  415.     mov cl, block_width
  416.     mul cl
  417.     cmp [b_x], ax
  418.     jl end_check_point
  419.  
  420.     add ax, block_width
  421.     cmp ax, [b_x]
  422.     jl end_check_point
  423.  
  424.     mov al, [chl]
  425.     mov ah, 0
  426.     mov cl, block_height
  427.     mul cl
  428.     cmp [b_y], ax
  429.     jl end_check_point
  430.  
  431.     add ax, block_height
  432.     cmp ax, [b_y]
  433.     jl end_check_point
  434.  
  435.     cmp [byd], 0
  436.     jne zero_dir
  437.     mov [byd], 1
  438.     jmp end_zero_dir
  439.     zero_dir:
  440.     mov [byd], 0
  441.     end_zero_dir:
  442.  
  443.     mov al, [bx]
  444.     cmp al, 0
  445.     je skip_dec_al
  446.     dec al
  447.     dec [map_sum]
  448.     cmp [map_sum], 0
  449.     jne skip_dec_al
  450.     mov [g_s], 1
  451.     skip_dec_al:
  452.     mov [bx], al
  453.  
  454.     mov [map_changed], 1
  455.     end_check_point:
  456.     ret
  457.  
  458.     left_border:
  459.     mov [bxd], 1
  460.     ret
  461.  
  462.     right_border:
  463.     mov [bxd], 0
  464.     ret
  465.  
  466.     bottom_border:
  467.     mov ax, [b_y]
  468.     cmp ax, screen_heigth - ball_speed
  469.     jg end_game_f
  470.  
  471.     mov ax, [p_x]
  472.     sub ax, ball_width / 2
  473.     cmp [b_x], ax
  474.     jl end_check_point
  475.  
  476.     add ax, platform_width + ball_width
  477.     cmp [b_x], ax
  478.     jg end_check_point
  479.  
  480.     mov [byd], 0
  481.  
  482.     mov ax, [p_x]
  483.     add ax, platform_width / 2
  484.  
  485.     mov dx, [b_x]
  486.     add ax, ball_width / 2
  487.  
  488.     cmp ax, dx
  489.     jl ax_l
  490.     sub ax, dx
  491.     cmp ax, platform_width / 3
  492.     jl sub_angle
  493.     jmp add_angle
  494.  
  495.     ax_l:
  496.     sub dx, ax
  497.     cmp dx, platform_width / 3
  498.     jl sub_angle
  499.  
  500.     sub_angle:
  501.     cmp [bdx], 2
  502.     jl end_check_point
  503.     dec [bdx]
  504.     jmp end_check_point
  505.  
  506.     add_angle:
  507.     cmp [bdx], ball_speed + 2
  508.     jg end_check_point
  509.     inc [bdx]
  510.     jmp end_check_point
  511.  
  512.     end_game_f:
  513.     mov [g_s], 2
  514.     ret
  515.  
  516.     top_border:
  517.     mov [byd], 1
  518.     ret
  519.   endp check_point
  520.  
  521.   draw_map proc
  522.     push ax
  523.       push cx
  524.         push bx
  525.  
  526.     mov [xc], 0
  527.     mov [yc], 0
  528.     mov [wc], block_width-1
  529.     mov [hc], block_height
  530.     lea bx, map
  531.     mov al, 0
  532.     mov cx, lines_count
  533.  
  534.     @@lines:
  535.       push cx
  536.       mov cx, columns_count
  537.  
  538.       @@columns:
  539.         push ax
  540.         xlat
  541.         call get_block_color
  542.         cmp al, 0ffh
  543.         je skip_clear_pixels
  544.         mov [cc], al
  545.         cmp al, 0
  546.         jne end_black_color
  547.         black_color:
  548.         mov di, bx
  549.         mov ah, 0
  550.         add di, ax
  551.         mov al, 0ffh
  552.         scasb
  553.         end_black_color:
  554.  
  555.         call clear_pixels
  556.         skip_clear_pixels:
  557.         pop ax
  558.         inc al
  559.         add [xc], block_width
  560.       loop @@columns
  561.  
  562.       add [yc], block_height
  563.       pop cx
  564.     loop @@lines
  565.  
  566.         pop bx
  567.       pop cx
  568.     pop ax
  569.  
  570.     ret
  571.   endp draw_map
  572.  
  573.   get_block_color proc
  574.     push bx
  575.     lea bx, block_colors
  576.     xlat
  577.     pop bx
  578.     ret
  579.   endp get_block_color
  580.  
  581.   draw_ball proc
  582.     lea bx, ball_nums
  583.     call mov_sprite_nums
  584.     mov ax, [b_x]
  585.     mov [x], ax
  586.     mov ax, [b_y]
  587.     mov [y], ax
  588.     call draw_pixels
  589.     ret
  590.   endp draw_ball
  591.  
  592.   draw_platform proc
  593.     lea bx, platform_nums
  594.     call mov_sprite_nums
  595.     mov ax, [p_x]
  596.     mov [x], ax
  597.     mov ax, p_y
  598.     mov [y], ax
  599.     call draw_pixels
  600.     ret
  601.   endp draw_platform
  602.  
  603.   clear_ball proc
  604.     mov ax, ball_width
  605.     mov [wc], ax
  606.     mov ax, ball_height
  607.     mov [hc], ax
  608.     mov [cc], 0
  609.     mov ax, [b_x]
  610.     mov [xc], ax
  611.     mov ax, [b_y]
  612.     mov [yc], ax
  613.     call clear_pixels
  614.     ret
  615.   endp clear_ball
  616.  
  617.   clear_platform proc
  618.     mov ax, platform_width
  619.     mov [wc], ax
  620.     mov ax, platform_height
  621.     mov [hc], ax
  622.     mov [cc], 0
  623.     mov ax, [p_x]
  624.     mov [xc], ax
  625.     mov ax, p_y
  626.     mov [yc], ax
  627.     call clear_pixels
  628.     ret
  629.   endp clear_platform
  630.  
  631.   move_platform proc
  632.     call clear_platform
  633.     mov ax, [p_x]
  634.     cmp [p_d], 0
  635.     je @@left
  636.  
  637.     @@right:
  638.     add ax, platform_speed
  639.     cmp ax, 320 - platform_width
  640.     jge skip_move_right
  641.     mov [p_x], ax
  642.     skip_move_right:
  643.     cmp [b_m], 0
  644.     je ball_on_platform
  645.     ret
  646.  
  647.     @@left:
  648.     sub ax, platform_speed
  649.     cmp ax, 1
  650.     jle skip_move_left
  651.     mov [p_x], ax
  652.     skip_move_left:
  653.     cmp [b_m], 0
  654.     je ball_on_platform
  655.     ret
  656.  
  657.     ball_on_platform:
  658.     call clear_ball
  659.     mov ax, [p_x]
  660.     mov [b_x], ax
  661.     add [b_x], (platform_width - ball_width) / 2
  662.     ret
  663.   endp move_platform
  664.  
  665.   move_ball proc
  666.     cmp [b_m], 0
  667.     je @@ret
  668.  
  669.     call clear_ball
  670.  
  671.     mov al, [bdx]
  672.     mov ah, 0
  673.     cmp [bxd], 0
  674.     je sub_bx
  675.     add [b_x], ax
  676.     jmp add_bx
  677.     sub_bx:
  678.     sub [b_x], ax
  679.     add_bx:
  680.  
  681.     mov al, [bdy]
  682.     mov ah, 0
  683.     cmp [byd], 0
  684.     je sub_by
  685.     add [b_y], ax
  686.     jmp add_by
  687.     sub_by:
  688.     sub [b_y], ax
  689.     add_by:
  690.  
  691.  
  692.     call check_point
  693.    
  694.  
  695.     @@ret:
  696.     ret
  697.   endp move_ball
  698.  
  699.  
  700.  
  701. ; ------- MAPS -------
  702.  
  703.   block_colors db 0,1,5,4
  704.  
  705.   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
  706.       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
  707.       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
  708.       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
  709.       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
  710.       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
  711.       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
  712.       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
  713.  
  714.   map_sum dw 2
  715.   map_changed db 1
  716.  
  717. ; ------- SPRITES -------
  718.  
  719.   platform_nums dw 42, 8
  720.   platform db 0,0,0,4 dup (4),0,26 dup(8),0,4 dup(4),0,0,0
  721.            db 0,0,4,12,12,12,4,0,26 dup (7),0,4 dup (12),4
  722.            db 0,0,0,1,12,3 dup(15),4,0,26 dup(15),0,3 dup(15)
  723.            db 12,4,1,0,0,1,4 dup(12),4,0,26 dup(7),0,4 dup(12)
  724.            db 4,1,0,1,9,4 dup(12),4,0,26 dup(7),0,4 dup(12),4
  725.            db 9,1,0,9,5 dup(4),0,26 dup(7),0,5 dup(4),9,0,0,0
  726.            db 5 dup(4),0,26 dup(7),0,5 dup(4),0,0,0,0,0
  727.            db 4 dup(4),0,26 dup(8),0,4 dup(4),0,0,0
  728.  
  729.   ball_nums dw 8, 8
  730.   ball db 0,0,  2,10,10,2,  0,0
  731.        db 0, 2,10,10,10,10,2 ,0
  732.        db 0, 2,10,10,10,10,2 ,0
  733.        db 2,10,10,10,10,10,10,2
  734.        db 2,10,10,10,10,10,10,2
  735.        db 0,  2,2,10,10,2,2  ,0
  736.        db 0,   2,2,2,2,2,2   ,0
  737.        db 0,0,   2,2,2,2   ,0,0
  738.  
  739.   you_win db 'You win!', 0dh, 0ah, '$'
  740.   you_lose db 'You lose', 0dh, 0ah, '$'
  741.  
  742. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement