'A work in progress...
' - Added better lightnig affects.
' - Added screen flash
' - Worked on colors.
' - Add a ground plane
' - Maybe Add splash effects?
' - Add cloud layer
' - Always working on code efficiency.
'Author: Phlashlite, Jan 2022
' NOTES: If you see code enclosed in "++++" there are adjustable setting to play with in there.
' The original code was translated from watching this Coding Challenge Video:
' [youtube]https://www.youtube.com/watch?v=KkyIDI6rQJI[/youtube]
'Most of the adjustable settings+++++++++++++++++++++++++++++++++++++++++++++++
'Screen________________________________________________________________________
'Your screen width
'Your screen height
CONST SCR_HGHT
= SCR_WDTH
* .5625
'Virtual depth of the scene
'Rain__________________________________________________________________________
'Number of raindrops to draw
'Lower limit of raindrop (drp) speed
'Upper limit of drp speed
'Lower limit of drp length
'Upper limit of drp length
'Lower limit of drp width
'Upper limit of drp width
'Lightning_____________________________________________________________________
'Bolt detail, smaller is more detailed, 1 is default
'How crazy the bolt path is (angle displacement) 650 is default
'Bolt width
'Ticks Bolt is on screen
'Frequency of Bolts. Range is 1 to 1000. > = less frequency.
'Lightning Bolt glow___________________________________________________________
'One side gradient width
CONST BLT_GRD_HLF_WDTH
= 17
'Intensity of the gradient
CONST BLT_GRD_INTENSTY
= 168
'Map gradient to 255 color scale
CONST BLT_GRD_CALC
= BLT_GRD_INTENSTY
/ BLT_GRD_HLF_WDTH
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
_MOUSEHIDE 'hide the mouse if you want (use if you are going to use as a screensaver '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
_SCREENMOVE _MIDDLE '(-SCR_WDTH / 3) - 3, -26 'Moves the screen to hide widows title bars and frames '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'This is a drp
'GroundY = SCR_HGHT * 0.75
'Initialize and fill the rain array____________________________________________
fillDropArray (i!)
'Initialize and fill the glow array____________________________________________
gradtable(g!) = BLT_GRD_INTENSTY - BLT_GRD_CALC * g!
'Main loop to draw each Drop and call the lightning bolts______________________
'Fall
Rain(i!).y = Rain(i!).y + Rain(i!).spd
'Draw
LINE (Rain
(i!
).x
, Rain
(i!
).y
)-(Rain
(i!
).x
+ Rain
(i!
).wth
, Rain
(i!
).y
+ Rain
(i!
).lng
), _RGBA(205, 205, 205, 21), BF
'Fill
'Restart each Drop in the array if it's y value passes the bottom of the screen (SCR_HGHT)
fillDropArray (i!)
'Call the lightning bolt (blt)_____________________________________________
'How far the bolt wanders++++++++++++++++++++++++++++++++++++++++++++++++++
x1! = Rndm!(-10, SCR_WDTH + 10) 'Sets blts x axis starting point range
x2! = Rndm!(x1! - SCR_WDTH / 2, x1! + SCR_WDTH / 2) 'Sets how much a blt's end x axis point
' varies from its starting x axis point
y1! = 0 ' Rndm!(0, SCR_HGHT - 1000) 'Sets blts y axis starting point range
y2! = Rndm!(1100, SCR_HGHT) 'Sets blts y axis ending point range
'Adjust how often bolts strike_____________________________________________
k! = Rndm(1, 1000)
drawLightning x1!, y1!, x2!, y2!, BLT_DISP
'_DISPLAY
'DO
'LOOP UNTIL INKEY$ <> ""
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0
'______________________________________________________________________________
'Does all the drop calculations and fills the array.
Rain(i!).x = Rndm(0, SCR_WDTH)
Rain(i!).y = Rndm(-SCR_HGHT, 0) 'start drops off screen on the y axis
'bias rnd# gen for more drops farther away (shorter, slower, narrower)___
j! = Rndm(1, SCR_DPTH * 2.5)
IF j!
> SCR_DPTH
THEN j!
= Rndm
(1, SCR_DPTH
* .25)
Rain(i!).z = j! 'Rndm(1, SCR_DPTH)
Rain(i!).wth = map(Rain(i!).z, 0, SCR_DPTH, MIN_DRP_WDTH, MAX_DRP_WDTH)
Rain(i!).lng = map(Rain(i!).z, 0, SCR_DPTH, MIN_DRP_LNGTH, MAX_DRP_LNGTH)
Rain(i!).spd = map(Rain(i!).z, 0, SCR_DPTH, MIN_DRP_SPEED, MAX_DRP_SPEED)
'______________________________________________________________________________
'Does all the lightning calculations usimg a "midpoint displacement alrorithm".
SUB drawLightning
(x1!
, y1!
, x2!
, y2!
, BLT_DISP
)
x3! = x1! - BLT_HLF_WDTH 'start x1 adjusted for width
x4! = x1! + BLTWHLFDTH 'end x1 adjusted for width
x5! = x2! - BLT_HLF_WDTH 'start x2 adjusted for width
x6! = x2! - BLT_HLF_WDTH 'end x2 adjusted for width
FOR bw!
= 1 TO BLT_HLF_WDTH
LINE (x1!
, y1!
)-(x2!
, y2!
), _RGBA(255, 255, 255, 200) LINE (x1!
+ bw!
, y1!
)-(x2!
+ bw!
, y2!
), _RGBA(255, 255, 255, 200) LINE (x1!
- bw!
, y1!
)-(x2!
- bw!
, y2!
), _RGBA(255, 255, 255, 200)
FOR bgw!
= 1 TO BLT_GRD_HLF_WDTH
LINE (x3!
+ bgw!
, y1!
)-(x4!
+ bgw!
, y2!
), _RGBA(gradtable
(bgw!
), gradtable
(bgw!
), gradtable
(bgw!
), gradtable
(bgw!
)) LINE (x3!
- bgw!
, y1!
)-(x4!
- bgw!
, y2!
), _RGBA(gradtable
(bgw!
), gradtable
(bgw!
), gradtable
(bgw!
), gradtable
(bgw!
))
midX = (x1! + x2!) / 2
midY = (y1! + y2!) / 2
midX!
= midX!
+ (RND - .5) * BLT_DISP
midY!
= midY!
+ (RND - .5) * BLT_DISP
drawLightning x1!, y1!, midX!, midY!, BLT_DISP / 2
drawLightning x2!, y2!, midX!, midY!, BLT_DISP / 2
'______________________________________________________________________________
'Standard random numbers in a range of mn! (min) to mx! (max)
Rndm!
= RND * (mx!
- mn!
) + mn!
'______________________________________________________________________________
' Map function I found or translated from somewhere.
' The Coding Train" guy on youtube, where I translated the rain code from explained it in one of his videos.
FUNCTION map!
(value!
, minRange!
, maxRange!
, newMinRange!
, newMaxRange!
) map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!