Code for beginners: Lightning generator

Hello,
I write a lot of simple code, and I thought I might share it for the beginning coders to use for examples, so here’a my first one: Lightning:

displayMode(FULLSCREEN)

function setup()
    backingMode(RETAINED)
end

function draw()
    noStroke()
    fill(0, 0, 0, 13)
    rect(0,0,WIDTH,HEIGHT)
end

function touched(t)
    if t.state == ENDED then
        drawLightning()
    end
end

function drawLightning()
    local points = {}
    local variation = vec2(60,100)
    table.insert(points,vec2(math.random()*WIDTH,HEIGHT))
    for i = 1,10 do
        table.insert(
            points,
            vec2(
                points[i].x+(math.random()-.5)*variation.x,
                points[i].y-math.random()*variation.y
            ))
    end
    stroke(255, 255, 255, 255)
    strokeWidth(5)
    for i,v in ipairs(points) do
        if points[i+1] then
            line(v.x,v.y,points[i+1].x,points[i+1].y)
        end
    end
end

Nice, though it always slopes left to right. My preference would be to add a -15 (or half the x value of the variation variable) to the x part of the point insertion. Just my personal preference though :slight_smile:

points[i].x-15+math.random()*variation.x

@West - I thought I forgot to do something in the code. That’s it. Thanks! I updated the code.

I had a little fun with your code :slight_smile: Added stars, flash and sound.


--# Main
displayMode(FULLSCREEN)

function setup()
    backingMode(RETAINED)
    delay(2,drawLightning)
    flash = {0,0,0}
    Galaxy.init(500)
end

function draw()
    background(flash[1],flash[2], flash[3], 255)
    noStroke()
    fill(0, 0, 0, 13)
    rect(0,0,WIDTH,HEIGHT)
    Galaxy.draw()
end

function touched(t)
    if t.state == ENDED then
        drawLightning()
    end
end

function drawLightning()
    tween(1,flash,{255,255,255},tween.easing.bounceOutIn,function() flash = {0,0,0} end)
    sound(SOUND_EXPLODE, 49833)
    sound(DATA, "ZgNAAgBBXkcvcCR9H+lBP9a53T6wUVk+ZABZXV1aPztXXAdu")
    local points = {}
    local variation = vec2(60,100)
    table.insert(points,vec2(math.random()*WIDTH,HEIGHT))
    for i = 1,10 do
        table.insert(
            points,
            vec2(
                points[i].x+(math.random()-.5)*variation.x,
                points[i].y-math.random()*variation.y
            ))
    end
    stroke(255, 255, 255, 255)
    strokeWidth(15)
    for i,v in ipairs(points) do
        if points[i+1] then
            line(v.x,v.y,points[i+1].x,points[i+1].y)
        end
    end
end

function delay(time,callback)
   tween.delay(3,function() callback() delay(time,callback) end) 
    
end
--# Galaxy
Galaxy = {}
Galaxy.count = 0
function Galaxy.init(count)
    Galaxy.count = count
    galaxy = {}
    for i=1,count,1 do
       galaxy[i] = Stars() 
    end
    
end

function Galaxy.draw()
    for i=1,Galaxy.count,1 do
        
       galaxy[i]:draw() 
    end
    
    
end

Stars = class()

function Stars:init(x,y)
    -- you can accept and set parameters here
    self.x = math.random(1,WIDTH)
    self.y = math.random(1,HEIGHT)
    self.m = mesh()
    self.s1 = math.random(1,3)
    self.s2 = math.random(1,3)
    self.m.vertices = {vec3(0,0,0),
    vec3(self.s1,0,0),
    vec3(self.s1,self.s2,0)}
end

function Stars:draw()
    -- Codea does not automatically call this method
--background(0,0,0,255)
    
    pushMatrix()
    translate(self.x,self.y)
    self.m:setColors(255,255,255,math.random(1,255))
    self.m:draw()
    popMatrix()



end

function Stars:touched(touch)
    -- Codea does not automatically call this method
end

@Briarfox - Cool.

Just tossed my old stars project from when I first learned codea in. @Zoyt never thought of lightning :slight_smile: cool idea.

Sorry to be picky, but lightning and thunder aren’t simultaneous…give the thunder a delay of some minimal time, and possibly put a low pass filter on the sound?

@Aciolino, oh but it can! And thats when you need to be careful! I have not played much with codea sound. How do you do this low pass filter that you speak of?

Woops, got a string can not be parsed as XML error then bam! Multiple posts.

Deleted triple post

@Briarfox - I’m getting the same issue.
@Simeon - Any chance of finding the cause?
Thanks!

  • Double post -

@Briarfox, if you look at sound() there is an advanced version. It saves stuff as a string. I’d generate the base sound under advanced, save that string, then mess with the low-pass filter until it sounds “low”, save that string, then play() both sounds at the same time, with a param of .7 for the low sound and .3-.4 for the higher pitched sound.

EDIT: IOC that you already play two sounds. Add the third parameter for volume control. And don’t use the default SOUND_EXPLODE, too cheesy :slight_smile:

This sound stuff baffles me :slight_smile: