Recurring Lightning.bas

Well they liked it at the JB forum...


'recurring lightning.bas for SmallBASIC 0.12.9 2017-08-22
'translated from: recurring lightning.txt for JB (B+=MGA) 2017-08-21

def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def distance(x1, y1, x2, y2) = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5

drawlandscape
land = image(0, 0, xmax, ymax)
while 1
land.show(0, 0)
delay 65 '<< adjust time as needed for your system
cls
rPause = rand(30, 3500)
delay rPause
rStop = rand(1, 4)
xo = rand(.25 * XMAX, .75 * XMAX)
yo = rand(0, .35 * YMAX)
for i = 1 to rStop
xe = rand(0, XMAX)
ye = rand(0, .5 * YMAX)
dist = distance(xo, yo, xe, ye)
d = rand(.3 * dist, .9 * dist)
Lightning xo, yo, xe, ye, d
next
delay 50
wend

sub
Lightning(x1, y1, x2, y2, d)
local mx, my
if d < 5 then
color rgb(225, 225, 245)
line x1, y1, x2, y2
else
mx = (x2 + x1) / 2
my = (y2 + y1) / 2
mx = mx + -.5 * rnd * d * .4 * rand(-2, 2)
my = my + -.5 * rnd * d * .4 * rand(-2, 2)
Lightning x1, y1, mx, my, d/2
Lightning x2, y2, mx, my, d/2
end if
end
sub

sub
drawLandscape
'the sky
for i = 0 to ymax
midInk 0, 0, 0, 128, 128, 128, i/ymax
line 0, i, XMAX, i
next
'the land
startH = YMAX - 200
rr = 70 : gg = 70 : bb = 90
for mountain = 1 to 6
Xright = 0
y = startH
while Xright < XMAX
' upDown = local up / down over range, change along Y
' range = how far up / down, along X
upDown = (rnd *.8 - .35) * (mountain *.5)
range = Xright + rand(15, 25) * 2.5/mountain
for x = Xright -1 to range
y = y + upDown
color rgb(rr, gg, bb)
line x, y, x, YMAX
next
Xright = range
wend
rr = rand(rr - 15, rr) : gg = rand(gg - 15, gg) : bb = rand(bb - 25, bb)
if rr < 0 then rr = 0
if gg < 0 then gg = 0
if bb < 0 then bb = 0
startH = startH + rand(5, 20)
next
end sub

sub
midInk(r1, g1, b1, r2, g2, b2, fr)
color rgb(r1+(r2-r1)*fr, g1+(g2-g1)*fr, b1+(b2-b1)*fr)
end



Thanks to shian who had written some simple demos of Image in the Language Reference.