Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Digital Rain

edited January 2012 in Code Sharing Posts: 622

My celebration of text in Codea 1.3

pic.twitter.com/dcjIJa7K

--matrix digital rain
function setup()
    displayMode(FULLSCREEN)
    font("Futura-CondensedExtraBold")
    fontSize(24)
    c = ElapsedTime
    ns = (math.ceil(WIDTH/fontSize())+1) --num of streams
    st = {} --stream table
    for i = 1,ns do st[i] = RainStream(i,fontSize()) end
end

function draw()
    if ElapsedTime > c + .2 then
        c = ElapsedTime
        for i = 1,ns do st[i]:drip() end
    end
    background(0,0,0,255)   
    for i = 1,ns do st[i]:draw() end
end

RainStream = class()

function RainStream:init(c,f)
    --column, fontSize   
    self.f = f --fontsize
    --number of characters from top to bottom of screen
    self.n = (math.ceil(HEIGHT/self.f)+1) 
    self.x = WIDTH - (c*self.f) --x of column
    self:reset()
    self.s = math.random(self.n) --start  
end

function RainStream:reset()
    self.t = kano() --text
    local i
    self.t = ""
    for i = 1,self.n do self.t = self.t .. self:randchar() end
    self.tl = math.random(self.n) --tail length
    self.s = 0
end

function RainStream:randchar()
    local r, c
    r = math.random((string.len(kano())/3))
    return string.sub(kano(),1+(r*3),(r*3)+3)   
end

function RainStream:draw()
    local dt --draw tail to here
    fill(0, 255, 0 , 255) --bright
    if self.s <= self.n then
        text(string.sub(self.t,1+(self.s*3),(self.s*3)+3),self.x,HEIGHT - (self.s*self.f))
    end
    fill(0, 255, 0 , 128) --dim
    if self.s - self.tl < 0 then 
        dt = 0 --draw to top of screen
    else 
        dt = self.s - self.tl 
    end
    local j
    for j = (self.s-1),dt,-1 do --draw dim chracters going up
        --kano characters are a length of 2 in a string
        --don't know why, they just are
        text(string.sub(self.t,1+(j*3),(j*3)+3),self.x,HEIGHT - (j*self.f))
    end 
end

function RainStream:drip()
    self.s = self.s + 1 --move drip down
    if math.random(100) > 50 then
        --rand a chracter
        local r = math.random(self.n-6)+3
        local nt = string.sub(self.t,1,(r*3))
        nt = nt .. self.randchar()
        nt = nt .. string.sub(self.t,(r*3)+4)
        self.t = nt 
    end
    --check for reset
    if self.s > (self.n + self.tl) then self:reset() end
end
 
function kano()
    --these aren't the true chracters, just ones I found
    return  "あかさたないきしちにうくすつぬえけせてねおこそとのはまやらわひみりをふむゆるんへめれ"
end

The Kano characters at the end confuse the codea editor a bit.

What was funny about making this is that it is so commonly reproduced in every environment, it's about impossible to find a link to the orginal movie. I had to put in the DVD. This similuates the screens in the film not the neo or opening credit effects.

Comments

  • Posts: 2,820

    AWSOME! LOVE IT!

  • Posts: 118

    Nicely done!

  • Posts: 2,820

    Emoji don't work even when I change the font to the emoji font. :-( Nor can I spell out "Codea 1.3 rocks!". It gets all jumbled. I know that's what you want, but it would be awesome if it could rain strings. :-D

  • Posts: 622

    When using a non-Kano set, those j*3 and the + around them may need removed or changed. Emoji may be 2 long instead of 3 or a mixture. The easiest is to put them all in a table, the string is just more compact.

    To remove the random set self.t = kano() or just put your string right there. Also in drip, change the random test to > 101

  • Posts: 2,820

    Alright. Thanks.

  • Really cool.

  • thx

  • Posts: 437

    I liked it, you can use it as screensaver :-)

  • Posts: 6

    can you just copy and paste the conde in to codea (sorry i am new to codea)

  • Posts: 266

    @Fr0st yep just put everything in the main

  • Posts: 6

    yay it works awesome!

  • How do I copy paste this code

  • Posts: 563

    On your iPad, hold your finger down on a bit of the code until the selection bars come up and then select all the code, tap copy.

    Open Codea, tap add new project, hold your finger down on a word of the default code, tap select all when the pop up comes up and then select paste!

  • Posts: 193
    I'm trying to change the font to English. If I just change just 1 letter in the function lane ( ), I get an error msg...I would like to change the fonts to emogis..
  • dave1707dave1707 Mod
    Posts: 8,456

    Here’s a version using English characters. Not sure why the emoji font doesn’t work.

    function setup()
        displayMode(FULLSCREEN)
        font("MarkerFelt-Wide")
        fontSize(24)
        c = ElapsedTime
        ns = (math.ceil(WIDTH/fontSize())+1) --num of streams
        st = {} --stream table
        for i = 1,ns do st[i] = RainStream(i,fontSize()) end
    end
    
    function draw()
        if ElapsedTime > c + .2 then
            c = ElapsedTime
            for i = 1,ns do st[i]:drip() end
        end
        background(0,0,0,255)   
        for i = 1,ns do st[i]:draw() end
    end
    
    RainStream = class()
    
    function RainStream:init(c,f)
        --column, fontSize   
        self.f = f --fontsize
        --number of characters from top to bottom of screen
        self.n = (math.ceil(HEIGHT/self.f)+1) 
        self.x = WIDTH - (c*self.f) --x of column
        self:reset()
        self.s = math.random(self.n) --start  
    end
    
    function RainStream:reset()
        self.t = kano() --text
        local i
        self.t = ""
        for i = 1,self.n do self.t = self.t .. self:randchar() end
        self.tl = math.random(self.n) --tail length
        self.s = 0
    end
    
    function RainStream:randchar()
        local r, c
        r = math.random((string.len(kano())//1))
        return string.sub(kano(),1+(r),(r)+1)   
    end
    
    function RainStream:draw()
        local dt --draw tail to here
        fill(0, 255, 0 , 255) --bright
        if self.s <= self.n then
            text(string.sub(self.t,1+(self.s),(self.s)+1),self.x,HEIGHT -(self.s*self.f))
        end
        fill(0, 255, 0 , 128) --dim
        if self.s - self.tl < 0 then 
            dt = 0 --draw to top of screen
        else 
            dt = self.s - self.tl 
        end
        local j
        for j = (self.s-1),dt,-1 do --draw dim chracters going up
            text(string.sub(self.t,1+(j),(j)+1),self.x,HEIGHT - (j*self.f))
        end 
    end
    
    function RainStream:drip()
        self.s = self.s + 1 --move drip down
        if math.random(100) > 50 then
            --rand a chracter
            local r = math.random(self.n-6)
            local nt = string.sub(self.t,1,(r))
            nt = nt .. self.randchar()
            nt = nt .. string.sub(self.t,(r))
            self.t = nt 
        end
        --check for reset
        if self.s > (self.n + self.tl) then self:reset() end
    end
    
    function kano()
        return "QSSXERFCVFTYHBNNMKKPOOIUNHTTFFEESAQZZwqazcerfvgtyunmjkopkk321456790865"
    end
    
  • Posts: 193
    Thanks....
  • Posts: 193
    Is there a way I could get access to some of the PGM's from 2016 ? My previous I-cloud acct becamed locked and I can't retrieve them from there, I had to make a new gmail acct and buy codea all over again....I learned a lot from Fuze programming so it wasn't a total waste, but I'll be back here from now on..
  • dave1707dave1707 Mod
    edited June 10 Posts: 8,456

    You have access to any program posted in this forum. You can go back to Sept 2013 using the page numbers at the bottom of each discussion list. You can also go back to about Sept 2011 if you know what to search for.

  • Posts: 193
    I seem to be getting error msg’s..
  • Posts: 193
    I seem to be getting error msg’s.....

    Error msg :smile:

    Please include cameras (not camera) as a dependency

    Stack traceback :[C] in function ‘assert’

    Main : 12 : in function ‘setup’

    Main : 37 : attempt to index a nil value (global ‘scene’)
    Stack traceback :

    Main : 37 : in function ‘update’

    Main : 29 : in function ‘draw’

    Does anyone have any idea what went wrong ?
    Using I-ph 6s....
  • Posts: 1,752
    @kendog400 - please post a little code, or point out which of the posts above you are referring to

    It looks like you are referring to a 3D Craft package with assert, cameras and scene referred to. Are you posting in the right thread?
  • dave1707dave1707 Mod
    Posts: 8,456

    @kendog400 That looks like one of my error messages that I display when you don’t have CAMERAS selected as a dependency. I’m not sure which program your referring to. Just bring up the dependency list in the program and check CAMERAS.

  • edited June 13 Posts: 193
    It was this PGM......
    It usted to work fine on i-pad (5)

    ~~~

    displayMode(FULLSCREEN)

    function setup()
    diameter=6
    sides=10
    assert(craft, "Please include Craft as a dependency")
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
    scene = craft.scene()
    v=scene.camera:add(OrbitViewer,vec3(0,0,0), 700, 0, 2000)
    v.camera.farPlane=3000
    v.ry=60
    for z=1,20 do
    pos,ind,col,nor={},{},{},{}
    bezier()
    createSkin()
    createTube()
    end
    createSphere(vec3(0,0,-200),20) -- starting red sphere
    createSphere(vec3(0,0,200),20) -- ending red sphere
    end


    function draw()
    update(DeltaTime)
    scene:draw()
    text("Slide your finger around the screen to rotate the image.",WIDTH/2,HEIGHT-25)
    text("Use two fingers to zoom in, zoom out or to move the image.",WIDTH/2,HEIGHT-50)
    end


    function update(dt)
    scene:update(dt)
    end


    function bezier(cnt)
    local val=400
    local tab1={}
    local x0 = vec3(0,0,-200)
    local x1 = vec3(math.random(-val,val),math.random(-val,val),math.random(-val,val))
    local x2 = vec3(math.random(-val,val),math.random(-val,val),math.random(-val,val))
    local x3 = vec3(0,0,200)
    local xt
    for t=0,1.01,.02 do
    xt = (1-t)^3*x0 + 3*t*(1-t)^2*x1 + 3*t^2*(1-t)*x2 + t^3*x3
    table.insert(tab1,xt)
    end
    for z=1,#tab1-1 do
    createLoop(tab1[z],tab1[z+1],false) -- createLoop on 1st to next to last point
    end
    createLoop(tab1[#tab1-1],tab1[#tab1],true) -- createLoop on last point
    end


    function createLoop(p1,p2,last)
    local rp=vec3(1,1,1)
    local v1=rp-p1
    local r1=v1:cross(p2-p1)
    local s1=r1:cross(p2-p1)
    r1,s1=r1:normalize(),s1:normalize()
    local n
    for a=0,359,360/sides do
    n = r1 * math.cos(math.rad(a)) + s1 * math.sin(math.rad(a))
    n=n*diameter
    if last then
    table.insert(pos,n + p2) -- add p2 to last loop
    else
    table.insert(pos,n + p1) -- add p1 to loop
    end
    table.insert(nor,n)
    end
    end


    function createSkin()
    o,p={1,2,3,4,5,6,3,2,1,6,5,4},{}
    for z=1,#pos-sides do
    p[1],p[2],p[3],p[4],p[5],p[6]=z,z+1,z+sides+1,z,z+sides+1,z+sides
    if z%sides==0 then
    p[2]=z-sides+1
    p[3]=z+1
    p[5]=z+1
    end
    for t=1,12 do
    table.insert(ind,p[o[t]])
    end
    end
    for z=1,#pos do
    if (z-1)%1000==0 then -- change color on each tube
    c=color(math.random(255),math.random(255),math.random(255))
    end
    table.insert(col,c)
    end
    end


    function createSphere(p,s)
    local pt=scene:entity()
    pt.position=p
    pt.model = craft.model.icosphere(s,2)
    pt.material = craft.material("Materials:Specular")
    pt.material.diffuse=color(255,0,0)
    end


    function createTube()
    local pt=scene:entity()
    pt.model = craft.model()
    pt.model.positions=pos
    pt.model.indices=ind
    pt.model.colors=col
    pt.model.normals=nor
    pt.material = craft.material("Materials:Specular")
    end

    ~~~
  • Posts: 193
    I liked the PGM....and now I'm getting better at figuring things, but yet not everything....
  • dave1707dave1707 Mod
    Posts: 8,456

    @kendog400 CAMERAS needs to be checked as a dependency, other than that the program works OK. Are you still having problems after you check CAMERAS as a dependency.

  • edited June 13 Posts: 1,752

    @kendog400 - very impressive demo (an old post from @dave1707).

    p.s. remember to format your code posts with a space in a line after and before the ~~~

  • Posts: 193
    How do I check the camera as a dependency ? Sounds easy, but i,'m still confused...
  • dave1707dave1707 Mod
    Posts: 8,456

    @kendog400 When you’re in the editor, in the upper right is Do. Tap on that and it creates a dropdown. Tap on Dependencies and scroll down until you find Cameras. Tap the square and it will be checked and moved to the top of the list. That’s it, the program should run OK.

Sign In or Register to comment.