Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Balls rain from the top, some bouce then sink away.
#1
I'm sure making this kind of thing is a piece of cake to a lot of you, but it was a learning experience for me.  Had a little help on it.

Balls rain from the top, some bounce with gravity, then sink away.

- Dav

Code: (Select All)
'============
'BALLRAIN.BAS
'============
'Balls rain from the top, some drop, some bounce then sink away.
'For QB64 by Dav, SEP/2023

SCREEN _NEWIMAGE(1000, 600, 32)

balls = 200 'number of balls on screen, 200 is my laptop's comfort zone

DIM ballx(balls), bally(balls), ballxvel(balls), ballyvel(balls), ballsize(balls)
DIM ballred(balls), ballgrn(balls), ballblu(balls)

'make random ball values
FOR b = 1 TO balls
    ballx(b) = RND * (_WIDTH) 'x position
    bally(b) = RND * -(_HEIGHT) 'y position
    ballxvel(b) = INT(RND * 7) 'x speed
    ballyvel(b) = INT(RND * 3) 'y speed
    ballsize(b) = RND * 15 + 10 'ball size
    ballred(b) = RND * 255 'red color
    ballgrn(b) = RND * 255 'green color
    ballblu(b) = RND * 255 'blue color
NEXT

DO
    CLS

    FOR b = 1 TO balls

        IF bally(b) < _HEIGHT - ballsize(b) THEN

            ballx(b) = ballx(b) + ballxvel(b)
            bally(b) = bally(b) + ballyvel(b)

            IF ballx(b) < ballsize(b) OR ballx(b) > _WIDTH - ballsize(b) THEN
                ballxvel(b) = -ballxvel(b)
            END IF

            IF bally(b) < ballsize(b) OR bally(b) > _HEIGHT - (ballsize(b) * 2) THEN
                ballyvel(b) = -ballyvel(b)
            END IF

            ballyvel(b) = ballyvel(b) + 3 'gravity value
            'ballxvel(b) = ballxvel(b) + (Rnd * 1.2 - Rnd * 1.2) 'x shake

            'draw gradient ball
            FOR y2 = bally(b) - ballsize(b) TO bally(b) + ballsize(b)
                FOR x2 = ballx(b) - ballsize(b) TO ballx(b) + ballsize(b)
                    clr = (ballsize(b) - (SQR((x2 - ballx(b)) * (x2 - ballx(b)) + (y2 - bally(b)) * (y2 - bally(b))))) / ballsize(b)
                    IF clr > 0 THEN PSET (x2, y2), _RGB(clr * ballred(b), clr * ballgrn(b), clr * ballblu(b))
                NEXT
            NEXT

        END IF
    NEXT

    'see if balls done
    onscreen = 0
    FOR b = 1 TO balls
        IF bally(b) < _HEIGHT - ballsize(b) THEN onscreen = 1
    NEXT
    IF onscreen = 0 THEN EXIT DO

    _DISPLAY
    _LIMIT 24

LOOP

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
Balls rain from the top, some bouce then sink away. - by Dav - 10-03-2023, 01:05 PM



Users browsing this thread: 2 Guest(s)