Screensaver! Kind of...

Hey guys! I was doing some tests for a game today, and this kind of happened on accident. Really simple program, but I had a lot of fun with it! I probably watched it for about half an hour, and I would totally put this as my computer screensaver.

--# Main
-- Screensaver

-- Use this function to perform your initial setup
function setup()"fps")
    boxes = {}
    counter = 0
    boxGrav = 1

-- This function gets called once every frame
function draw()
    background(212, 228, 229, 255)
    fps = 1/DeltaTime
    local x = -math.random(-350,350)
    local y = -math.random(100,350)
    local width = math.random(50,100)
    local height = math.random(25,50)
    counter = counter + 1
    if counter == 60 then counter = 0
        for i = 1, 2 do
            if i == 1 then box = makeBox(x,y,width,height) end
            if i == 2 then box = makeBox(x,-y,width,height) end
            box.gravityScale = boxGrav
            box.restitution = 0.5
            boxes[#boxes+1] = Platform(box,x,y,width,height)
            boxGrav = -boxGrav
    for i, b in pairs(boxes) do
        if b.body.y > HEIGHT/2 + 100 or b.body.y < -HEIGHT/2 - 100
        or b.body.x > WIDTH/2 + 100 or b.body.x < -WIDTH/2 - 100 then
            b.body = nil
            boxes[i] = nil
    -- print(#boxes)

function makeBox(x,y,w,h)
    body = physics.body(POLYGON,vec2(-w/2,h/2),vec2(-w/2,-h/2),vec2(w/2,-h/2),vec2(w/2,h/2))
    body.position = vec2(-x,-y)
    return body

function colors()
    colorTab = {
    color(34, 155, 153, 255),
    color(180, 94, 30, 255),
    color(51, 63, 84, 255)

--# Platform
Platform = class()

function Platform:init(b,x,y,w,h)
    -- you can accept and set parameters here
    self.body = b
    self.width = w
    self.height = h
    self.colr = colorTab[math.random(#colorTab)]

function Platform:draw()
    -- Codea does not automatically call this method

    local x = self.body.x
    local y = self.body.y
    local w = self.width
    local h = self.height

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

Hey, and check out my Pixel Sprite Creator too! I’m new and would love some feedback! Thanks guys!!!

I watched it for almost a half hour too - and I’m at work. Very cool. Thank you for sharing…

Simple but effective - and neatly programmed :-bd

i watch it carefully hour and hour? then i think of domino

nice, thanks for sharing.

how to make gravity direction from up-down to left-right?boxgrav cos?

Try setting “if counter == 60 then counter = 0” to "if counter = 10 then counter = 0 >: )

Thanks for the comments and feedback everyone!
@wildcat_JK I totally agree.
@firewolf for left and right gravity I would just rotate 90 degrees.
Cool ideas, guys!

Great idea! Here’s how you rotate the direction of physics gravity 90 degrees clockwise:

    local g = physics.gravity()

@Dwins this is a great screensaver, I’ll admit I too have been staring at it for ages. They only issue I have is that I have an iPad 3 and so after around 300 boxes the FPS starts to drop considerably. Is there a way to delete boxes once they go off the screen so my FPS stays up?

The box draw function could be optimised a lot by using a single mesh with setRect. Much much faster than making individual draw calls

@yojimbo2000 thanks for the gravity suggestion! I haven’t tried meshes before, so I’ll definitely take a look at that. @JonoGaming00 the draw function does delete boxes outside the screen. The only thing that slowed it down for me was printing the number of boxes. After commenting that out it ran at 60 fps on my iPad mini

Ok @dwins I’ll check because I have been using it with the print command.