Breakout from within

I was bored watching reruns on TV so I thought I’d write another useless game. There are 2 way to play.

1.) Try to get the green ball off the screen by destroying the least amount of white squares.

2.) Try to destroy the greatest amount of white squares before the green ball go off the screen.

Slide your finger to move the red ball to hit the green ball and change its direction.

I didn’t spend a lot of time on this, so there might be problems.

PS. This doesn’t work on an iPhone or maybe some large sized iPad. I’ll try to fix that at a later time. If it doesn’t work and you want to fix it, change the w1 and h1 values in the “if portrait” statement near the end of setup() so the balls are in the blank area.

PS1. I modified the below code. I think it will work on any size device.

viewer.mode=FULLSCREEN

function setup()
    rectMode(CENTER)
    adjustSizes()
    physics.gravity(0,0)    -- turn off gravity
    gameOver,gameRestart=false,false
    w=WIDTH//(size*2)       -- number of squares wide
    h=HEIGHT//(size*2)      -- number of squared high
    count,tx,ty=0,0,0
    
    tab={}
    w1,h1=0,1
    for z=1,w*h do
        w1=w1+1
        if w1>w then
            w1=1
            h1=h1+1
        end
        box=fMin//(size*2)//6
        if w1<w//2-box or w1>w//2+box or h1<h//2-box or h1>h//2+box then
            table.insert(tab,rec(w1*size*2,h1*size*2,size))
        end
    end
    
    createSphere1(vec2(WIDTH/2,HEIGHT/2),s1Size)
    createSphere2(vec2(WIDTH/2+40,HEIGHT/2),s2Size)
end

function draw()
    background(25, 103, 162)
    
    offscreen()    
    if gameOver then
        return
    end
    
    drawGrid()
    drawCircles()
    
    text(count,WIDTH/2,HEIGHT/2)
end

function touched(t)
    if t.state==BEGAN and gameOver then
        if t.tapCount==2 then
            gameRestart=true
        end
    end
    if t.state==CHANGED then
        tx=tx+t.deltaX
        ty=ty+t.deltaY
    end
    if t.state==ENDED then
        tx,ty=0,0
    end
end

function collide(c)
    if c.state==BEGAN then
        for z=1,#tab do
            if tab[z].a~=nil then
                if c.bodyB.x~=s1.x or c.bodyB.y~=s1.y then
                    if tab[z].a.x==c.bodyA.x and tab[z].a.y==c.bodyA.y then
                        tab[z].a:destroy()
                        tab[z].a=nil
                        count=count+1
                    end
                end
            end
        end
    end
end

function adjustSizes()
    fMin=math.min(WIDTH,HEIGHT) -- get smaller size
    size=fMin//80-1     -- size of blocks
    s1Size=fMin//40     -- size of red ball
    s2Size=fMin//80     -- size of green ball
end

function drawGrid()
    fill(255)    
    for z=1,#tab do     -- draw squares using class
        if tab[z]~=nil then
            tab[z]:draw()
        end
    end
end

function drawCircles()
    fill(255,0,0)
    ellipse(s1.x,s1.y,s1Size*2)
    s1.linearVelocity=(vec2(tx*5,ty*5))
    
    fill(0,255,0)
    ellipse(s2.x,s2.y,s2Size*2)    
end

function offscreen()
    if s1.x<0 or s1.x>WIDTH or s1.y<0 or s1.y>HEIGHT then
        s1.x=WIDTH/2
        s1.y=HEIGHT/2
        tx,ty=0,0
    end
    if s2.x<0 or s2.x>WIDTH or s2.y<0 or s2.y>HEIGHT then
        gameOver=true
        text("Game over",WIDTH/2,HEIGHT/2)
        text("Squares removed  "..count,WIDTH/2,HEIGHT/2-50)
        text("To play again, double tap the screen.",WIDTH/2,HEIGHT/2-100)
    end
    if gameRestart then
        viewer.restart()
    end
end

function createSphere1(pos,size)
    s1=physics.body(CIRCLE,size)
    s1.type=DYNAMIC
    s1.restitution=.9
    s1.position=pos
end

function createSphere2(pos,size)
    s2=physics.body(CIRCLE,size)
    s2.type=DYNAMIC
    s2.restitution=.9
    s2.position=pos
end

rec=class()

function rec:init(x,y,s)
    local a
    self.size=s
    self.a=physics.body(POLYGON,vec2(-size,-size),vec2(size,-size),vec2(size,size),vec2(-size,size))
    self.a.x=x
    self.a.y=y
    self.a.type=STATIC
    self.a.restitution=.9
    return(a)
end

function rec:draw()
    if self.a~=nil then
        rect(self.a.x,self.a.y,self.size*2) 
    end
end