' This program exported from BASIC Anywhere Machine (Version [5.2.3].[2024.09.09.00.00]) on 2024.09.12 at 03:39 (Coordinated Universal Time)
' This program by Charlie Veniot is a port and mod
' of the "Lightning Globe" QB64 program by SMcNeill
' (https://qb64phoenix.com/forum/showthread.php?tid=3022&pid=28211#pid28211)

_INITAUDIO
_SNDWAVE "sawtooth"

midx% = 640 : midy% = 480
SCREEN _NEWIMAGE( midx% *2 + 1, midy% * 2 + 1, 27 )
DIM Kolor AS ULONG
CONST LevelOfVariance = 5 'Change this value to have fun with the strength of the "draw" to target.
DO
    CLS
    CIRCLE( midx%, midy% ), 5, &HAAAAFF, , , ,F
    IF _MOUSEBUTTON THEN
        SOUND 30,0.1
        StartX = midx%: StartY = midy%: EndX = _MOUSEX: EndY = _MOUSEY
        c% = INT( RND * 255 )
        Kolor = _RGB( c%, c%, 255 )
        COLOR Kolor
        DO WHILE StartX <> EndX OR StartY <> EndY
            CoinToss = RND * 300 'The strength of "draw" which pulls the lightning to the target.
            IF CoinToss < LevelOfVariance THEN 'Higher values meander less and go directly to the target.
                XChange = Sgn(EndX - StartX) '-1,0,1, drawn always towards the mouse
                YChange = Sgn(EndY - StartY)
            ELSE
                XChange = Int(Rnd * 3) - 1 '-1, 0, or 1, drawn in a random direction to let the lightning wander
                YChange = Int(Rnd * 3) - 1
            END IF
            StartX = StartX + XChange
            StartY = StartY + YChange
            PSet (StartX, StartY), Kolor
        LOOP
    END IF
    SLEEP 0.01
LOOP