A little game i made. Tap to place the block on the stack and try to stack as high as you can
--# Main
-- Main
supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)
function setup()
t=Tower()
t:addBlock({x=0,y=0,w=WIDTH,h=100})
o=Object()
o:spawn()
c=Cuts()
end
function draw()
background(0)
fill(255)
t:draw()
o:draw()
c:draw()
fill(0)
rect(0,0,300,50)
end
function touched(touch)
o:touched(touch)
end
--# Object
Object = class()
function Object:init()
self.alive=false
self.speed=1
self.direction=1
self.w=200
end
function Object:draw()
if self.alive then
fill(0,0,255)
rect(self.x,self.y,self.w,self.h)
self:move()
end
end
function Object:move()
if self.x<=0 or self.x+self.w>=WIDTH then
self.direction=-self.direction
end
self.x = self.x + self.speed*self.direction
end
function Object:touched(touch)
if touch.state==BEGAN then
self:drop()
end
end
function Object:spawn()
local top=t.stack[#t.stack]
self.x=math.random(10,WIDTH-self.w-10)
self.y=top.y+top.h
self.h=50
self.alive=true
self.speed = self.speed *1.2
end
function Object:drop()
local top=t.stack[#t.stack]
if not(
self:checkrange(self.x,top.x,top.x+top.w) or self:checkrange(self.x+self.w,top.x,top.x+top.w)or
self:checkrange(top.x,self.x,self.x+self.w) or self:checkrange(top.x+top.w,self.x,self.x+self.w)
)then
self.alive=false
return
end
local ow=self.w
local fx=math.max(self.x,top.x)
local ex=math.min(self.x+self.w,top.x+top.w)
local cpos=0
local cw=0
if self.x<fx then
cpos=self.x
cw=fx-self.x
else
cpos=top.x+top.w
cw=(self.x+self.w)-ex
end
c:spawn(cpos,self.y,cw,self.h)
self.w=ex-fx
t:addBlock({x=fx,y=self.y,w=self.w,h=self.h})
self.alive=false
self:spawn()
end
function Object:checkrange(n,l,h)
return (l<n and n<h)
end
--# Tower
Tower = class()
function Tower:init()
self.stack={}
end
function Tower:draw()
fill(255)
for i=1,#self.stack do
local r=self.stack[i]
rect(r.x,r.y,r.w,r.h)
end
end
function Tower:addBlock(p)
--p={x,y,w,h}
table.insert(self.stack,p)
end
--# Cut
Cut = class()
function Cut:init(x,y,w,h)
self.alive=true
self.x,self.y,self.w,self.h=x,y,w,h
self.vel=10
end
function Cut:draw()
self:move()
fill(255,0,0)
rect(self.x,self.y,self.w,self.h)
end
function Cut:move()
self.vel = self.vel * 1.001
self.y = self.y - self.vel
if self.x<-100 then
self.alive=false
end
end
Cuts=class()
function Cuts:init()
self.t={}
end
function Cuts:draw()
self:cull()
for i=1,#self.t do
self.t[i]:draw()
end
end
function Cuts:cull()
local i=1
while i<=#self.t do
if not self.t[i].alive then
self.t[i]=nil
table.remove(self.t,i)
i = i - 1
end
i = i + 1
end
end
function Cuts:spawn(x,y,w,h)
table.insert(self.t,
Cut(x,y,w,h))
end