Here’s another game starter. The object is to shoot each red or blue ball before they go off the bottom of the screen. The red balls start with a random value from 20 to 100. The blue balls start out with a value of 50. Each time a red or blue ball is hit, the value goes down. If a red or blue ball goes off the bottom of the screen, any value above 0 is subtracted from the score. If a red or blue ball reaches 0, it turns green and no value will be subtracted. A blue ball that reaches 0 is worth 100 points and a red ball 50 points. If a ball reaches the bottom and has a value above 0, the remaining count is reduced by 1. When the remaining count reaches 0, the game is over. Double tap to restart. The bullets only shoot straight up while your finger is moving on the screen.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
function setup()
e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
e1.info=vec2(0,0)
e2=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
e2.info=vec2(0,0)
bTab={}
rTab={}
gTab={}
mTab={}
count=120
score=0
remaining=50
end
function draw()
background(40, 40, 50)
fill(255)
for z=#mTab,1,-1 do -- draw missiles
ellipse(mTab[z].x,mTab[z].y,10)
mTab[z].y=mTab[z].y+5
if mTab[z].y<0 then
mTab[z]:destroy()
table.remove(mTab,z)
end
end
fontSize(20)
for z=#rTab,1,-1 do -- draw red ball
fill(255,0,0)
if rTab[z].info.y==0 then
fill(0,255,0)
end
ellipse(rTab[z].x,rTab[z].y,40)
fill(255)
text(rTab[z].info.y,rTab[z].x,rTab[z].y)
-- red ball off screen
if rTab[z].y<0 then
if rTab[z].info.y>0 then
remaining=remaining-1
score=score-rTab[z].info.y
else
score=score+50
end
rTab[z]:destroy()
table.remove(rTab,z)
end
end
for z=#bTab,1,-1 do -- draw blue balls
fill(0,0,255)
if bTab[z].info.y==0 then
fill(0,255,0)
end
ellipse(bTab[z].x,bTab[z].y,40)
fill(255)
text(bTab[z].info.y,bTab[z].x,bTab[z].y)
-- blue ball off screen
if bTab[z].y<0 then
if bTab[z].info.y>0 then
remaining=remaining-1
score=score-bTab[z].info.y
else
score=score+100
end
bTab[z]:destroy()
table.remove(bTab,z)
end
end
fontSize(50)
sc=addComma(score)
text("Score "..sc,WIDTH/2,HEIGHT-50)
if remaining<=0 then
text("Game over",WIDTH/2,HEIGHT/2)
text("Double tap to restart",WIDTH/2,HEIGHT/2-100)
else
text("Remaining "..remaining,WIDTH/2,HEIGHT-100)
count=count+1
if count>90 then -- create red ball every 1.5 seconds
count=0
createRtab()
end
if math.random(200)==100 then -- create random blue ball
createBtab()
end
end
end
function collide(c) -- check for collision with missiles
if c.bodyA.info.y>0 then
c.bodyA.info.y=c.bodyA.info.y-1
elseif c.bodyB.info.y>0 then
c.bodyB.info.y=c.bodyB.info.y-1
end
end
function touched(t)
if t.state==BEGAN and t.tapCount==2 and remaining<=0 then
restart()
return
end
if t.state==MOVING and remaining>0 then -- shoot
createMtab(t.x,t.y)
end
end
function createBtab() -- create blue balls
local b=physics.body(CIRCLE,20)
b.x=math.random(50,WIDTH-50)
b.y=HEIGHT
b.info=vec2(2,50)
table.insert(bTab,b)
end
function createRtab() -- create red balls
local b=physics.body(CIRCLE,20)
b.x=math.random(50,WIDTH-50)
b.y=HEIGHT
b.info=vec2(1,math.random(20,100))
table.insert(rTab,b)
end
function createMtab(x,y) -- create white missiles
local b=physics.body(CIRCLE,5)
b.x=x
b.y=y
b.linearVelocity=vec2(0,400)
b.info=vec2(0,0)
table.insert(mTab,b)
end
function addComma(value)
loop=1
while loop>0 do
value,loop=string.gsub(value,"^(-?%d+)(%d%d%d)","%1,%2")
end
return value
end