'=========================================================================== ' Subject: CONTAINED BOUNCING BALLS Date: 10-26-97 (20:22) ' Author: William Deer Code: QB, QBasic, PDS ' Origin: ag312350@student.uq.edu.au Packet: EGAVGA.ABC '=========================================================================== ' BALLS.BAS by William DEER c/o. ag312350@student.uq.oz.au ' ' This program is really simple. It draws round blips on the screen and then ' moves them around, bouncing off the walls. You can change the size of the ' box and the pressure inside changes. Hit Escape to exit the program. SCREEN 12 DIM X, Y, Box1, Box2, Boy1, Boy2, Dx, Dy, BlipNo, rad AS INTEGER DIM A, C, D AS INTEGER DIM Xpos, Ypos, Xvel, Yvel, FutX, FutY AS SINGLE DIM STP, Pressure AS SINGLE RANDOMIZE (TIMER / 16) X = 639: Y = 300 ' Screen Size Box1 = 0: Box2 = X ' Starting Coords for Box Boy1 = 0: Boy2 = Y Dx = 50: Dy = 50 ' Minimum Size of Box BlipNo = 20 ' Number of Blips rad = 5 ' Radius of Blips STP = X * Y Pressure = 1 DIM Blip(BlipNo, 6) ' Array for Blip Information DIM Ball(484) AS INTEGER: ' Graphics Array for a square of side 10 pixels CLS : CIRCLE (rad, rad), rad, 15: PAINT (rad, rad), 15, 15: GET (0, 0)-(10, 10), Ball: CLS FOR A = 1 TO BlipNo ' Initialise Blip Information Blip(A, 1) = ((RND * 50) + (X / 2)) Blip(A, 2) = ((RND * 50) + (Y / 2)) Blip(A, 5) = (RND * 5) + 1 Blip(A, 6) = (RND * 5) + 1 Blip(A, 3) = Blip(A, 5) * Pressure Blip(A, 4) = Blip(A, 6) * Pressure PUT (Blip(A, 1) - rad, Blip(A, 2) - rad), Ball, XOR ' Draw Blips NEXT A A$ = " " ' ---------------------- ' Start of the main loop ' ---------------------- WHILE A$ <> CHR$(27) ' Hit to end program IF A$ <> "" THEN LINE (Box1, Boy1)-(Box2, Boy2), 0, B ' Erase Old Border Box ' Key has been pressed. SELECT CASE A$ CASE "1": IF Boy2 - Boy1 > Dy THEN Boy1 = Boy1 + 1 CASE "2": IF Boy1 > 0 THEN Boy1 = Boy1 - 1 CASE "3": IF Boy2 - Boy1 > Dy THEN Boy2 = Boy2 - 1 CASE "4": IF Boy2 < Y THEN Boy2 = Boy2 + 1 CASE "5": IF Box2 - Box1 > Dx THEN Box1 = Box1 + 1 CASE "6": IF Box1 > 0 THEN Box1 = Box1 - 1 CASE "7": IF Box2 - Box1 > Dx THEN Box2 = Box2 - 1 CASE "8": IF Box2 < X THEN Box2 = Box2 + 1 END SELECT ' Calculate Pressure .: Relative Speed of Blips. Pressure = STP / ((ABS(Box2 - Box1) * ABS(Boy2 - Boy1))) FOR C = 1 TO BlipNo Blip(C, 3) = SGN(Blip(C, 3)) * Blip(C, 5) * Pressure Blip(C, 4) = SGN(Blip(C, 3)) * Blip(C, 6) * Pressure NEXT C ' Redraw Box and values in corners LINE (Box1, Boy1)-(Box2, Boy2), 14, B, X LOCATE 20, 1: PRINT "BALLS.BAS", , ; PRINT "Box Size =("; Box1; ","; Boy1; ")-("; Box2; ","; Boy2; ")"; LOCATE 21, 1: PRINT "Keys are as follows;" LOCATE 22, 1: PRINT "<1> Lower Roof", "<2> Raise Roof" LOCATE 23, 1: PRINT "<3> Raise Floor", "<4> lower Floor" LOCATE 24, 1: PRINT "<5> Left wall in", "<6> Left wall out" LOCATE 25, 1: PRINT "<7> Right wall in", "<8> Right wall in" LOCATE 27, 30: PRINT "Pressure over STP = "; : PRINT USING "###.#"; Pressure; PRINT " Ball No. = "; BlipNo; LOCATE 29, 1: PRINT "Press to exit program "; END IF FOR C = 1 TO BlipNo Xpos = Blip(C, 1): Ypos = Blip(C, 2) Xvel = Blip(C, 3): Yvel = Blip(C, 4) FutX = Xpos + Xvel: FutY = Ypos + Yvel IF FutX < Box1 + rad + 2 THEN Xvel = -1! * Xvel ' Gone past the left wall IF FutX > Box2 - rad - 2 THEN Xvel = -1! * Xvel ' Gone past the right wall IF FutY < Boy1 + rad + 2 THEN Yvel = -1! * Yvel ' Gone past the top wall IF FutY > Boy2 - rad - 2 THEN Yvel = -1! * Yvel ' Gone past the right wall Blip(C, 1) = Xpos + Xvel: Blip(C, 2) = Ypos + Yvel Blip(C, 3) = Xvel: Blip(C, 4) = Yvel PUT (Xpos - rad, Ypos - rad), Ball, XOR ' Remove old ball image PUT (Blip(C, 1) - rad, Blip(C, 2) - rad), Ball, XOR ' Put new image NEXT C A$ = INKEY$ WEND