1 year ago

#330951

test-img

Zixer

How do I change this code into having gravity in assembly 8086 ,

So ye here is the code with all the notes after ";", and right now its a small red pixel that bounces around in a 320*200 black square similar to how a pong ball would bounce around without the paddles

 STACK SEGMENT PARA STACK
    DB 64 DUP (' ')
    STACK ENDS
    
    DATA SEGMENT PARA 'DATA' ;stores data
        
            
    WINDOW_WIDTH DW 140h ;sets the width of window (320 px in hex)
    WINDOW_HEIGHT DW 0C8h ;sets the height of window (200 px in hex)
    WINDOW_BOUNDS DW 6    ;variable used to check collisions early
    
    TIME_AUX DB 0 ;variable used to check if time is different
    
    BALL_ORIGINAL_X DW 01Eh
    BALL_ORIGINAL_Y DW 01Eh
    BALL_X DW 0Ah ;x position for ball
    BALL_Y DW 0Ah ;y position for ball
    BALL_SIZE DW 04h ;size of the ball (how many pixels does the ball have width and height)
    BALL_VELOCITY_X DW 05h ;X (horisontal) velocity of the ball
    BALL_VELOCITY_Y DW 02h ;Y (vertical) velocity of the ball
DATA ENDS

CODE SEGMENT PARA 'CODE' 

    
    
    MAIN PROC FAR
    ASSUME CS:CODE,DS:DATA,SS:STACK ;assume as code, data and stack segments the respective regiters    
    PUSH DS         ;push to stack the DS segment
    SUB AX,AX       ;clean the AX regiter
    PUSH AX         ;push AX to the stack
    MOV AX,DATA     ;save on the AX regiter the contents of the DATA segment
    MOV DS,AX       ;save on the DS segment the contents of AX
    POP AX          ;release the top item from the stack to the AX
    POP AX          
        
        
        CHECK_TIME:
            MOV AH,2Ch ;get the system time
            INT  21h    ;Entry: CH = hour CL = minute DH = second DL = 1/100 seconds
    
            CMP DL,TIME_AUX
            JE CHECK_TIME
            ;if it's different, then draw, move, et.
            
            MOV TIME_AUX,DL ;update time per 1/100th of sec
            
            

            CALL CLEAR_SCREEN
            
            CALL MOVE_BALL
            CALL DRAW_BALL
        
            JMP CHECK_TIME
        
        RET
    MAIN ENDP

This is the part im guessing i have to change to add a constant down force to ball_y and make it infinitly bounce off the ground

    MOVE_BALL PROC NEAR
            MOV AX,BALL_VELOCITY_X 
            ADD BALL_X,AX   ;move the ball on x axis , adds value to x axis of ball)
                            ;if BALL_X < 0 
                            
            MOV AX,WINDOW_BOUNDS
            CMP BALL_X,AX
            JL NEG_VELOCITY_X
            
            MOV AX,WINDOW_WIDTH         ; Y--> collided
            CMP BALL_X,AX               ;if BALL_X > WINDOW_WIDTH 
            JG NEG_VELOCITY_X           ;Y---> collided
            
            
            
            MOV AX,BALL_VELOCITY_Y
            ADD BALL_Y,AX   
            
            MOV AX,WINDOW_BOUNDS
            CMP BALL_Y,AX
            JL NEG_VELOCITY_Y
            
            MOV AX,WINDOW_HEIGHT
            SUB AX,BALL_SIZE
            SUB AX,WINDOW_BOUNDS; X--> collided
            CMP BALL_Y,AX               ;if BALL_Y > WINDOW_HEIGHT 
            JG NEG_VELOCITY_Y
            RET
            
            
            NEG_VELOCITY_X:
                NEG BALL_VELOCITY_X ; make BALL_VELOCITY_X negatice
                RET
                
            NEG_VELOCITY_Y:
                NEG BALL_VELOCITY_Y ; make BALL_VELOCITY_X negatice
                RET
    MOVE_BALL ENDP
        
    DRAW_BALL PROC NEAR
        
        MOV CX,BALL_X ;set the initial column (X)
        MOV DX,BALL_Y ;set the initial line (Y)
        
        DRAW_BALL_HORIZONTAL:
            MOV AH,0CH ;says to draw a pixel
            MOV AL,0Ch ;choose white as color
            MOV BH,00h ;set the page number
            INT 10h
            
            
            INC CX          ;CX = CX+1
            MOV AX,CX      ;CA - BALL_X > BALL_SIZE (Y -->We go to the next line, n-> we go to the next column
            SUB AX,BALL_X
            CMP AX,BALL_SIZE
            JNG DRAW_BALL_HORIZONTAL
            
            MOV CX,BALL_X ;the CX register goes back to the initial column
            INC DX ;we advance one line
            
            MOV AX,DX           ;DX - BALL_Y > BALL_SIZE (Y -> we exit the procedure, N -> we continue to the next line
            SUB AX,BALL_Y
            CMP AX,BALL_SIZE
            JNG DRAW_BALL_HORIZONTAL
        
        
    
        RET
        
        
    DRAW_BALL ENDP
    
        CLEAR_SCREEN PROC NEAR
            MOV AH,00h ;sets video config
            MOV AL,13h ;sets the video mode 
            INT 10h ;executes the config
            
            MOV AH,0Bh ;set the config
            MOV BH,00h ;to the background color
            MOV BL,00h ;choses black as background color
            INT 10h
            
        RET
CODE ENDS ;Ends code
END ;Ends program 

assembly

game-physics

x86-16

gravity

0 Answers

Your Answer

Accepted video resources