Here’s another starter game. The object is to catch the positive balls and let the negative balls fall off the screen. If you catch a negative ball or a positive ball falls off the screen, your score goes down. Your score goes up when you catch a positive ball. The screen is green as long as you score matches the current high score. The screen turns red when you start to lose points. You win when you reach a score of 1000 or more. The game starts with a ball dropping every 3 seconds. As your score goes up, the balls will drop at a faster rate. A ball every 1/2 second is the fastest. To move the bucket, just slide your finger anywhere left or right. This is for anyone who wants to add to it or just see how things are done.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
btab={} -- ball table
createBall()
dtab={} -- dots table
createDots()
speed=3
time={3,2.5,2,1.5,1,.5}
currHS=0
score=0
px=300
et=0
end
function draw()
-- determine background color
if score<currHS then
background(100,0,0)
else
background(0,100,0)
end
fill(255)
fontSize(30)
text("Score "..score.." Curr High "..currHS,WIDTH/2,60)
-- draw dots
for a,b in pairs(dtab) do
ellipse(b.x,b.y,4)
end
-- draw balls and values
fontSize(17)
for a,b in pairs(btab) do
fill(255)
ellipse(b.x,b.y,36)
fill(255,0,0)
text(b.info,b.x,b.y)
if b.x<0 then -- off left screen, enter right screer
b.x=WIDTH
end
if b.x>WIDTH then -- off right screen, enter left screen
b.x=0
end
end
if score>=1000 then
fontSize(80)
text("You won",WIDTH/2,HEIGHT-300)
else
fontSize(30)
text("New ball every "..speed.." seconds",WIDTH/2,HEIGHT-30)
-- create a new ball
if ElapsedTime-et>speed then
createBall()
et=ElapsedTime
end
-- check for ball caught or off screen
for a,b in pairs(btab) do
if b.y>70 and b.y<130 and b.x>px-50 and b.x<px+50 then
score = score + b.info
destroyBall(a,b) -- remove ball
if b.info>0 then
sound(SOUND_JUMP, 46230) -- positive ball
else
sound(SOUND_JUMP, 21150) -- negative ball
end
elseif b.y<0 then -- off bottom of screen
if b.info>0 then
score = score - b.info -- positive ball
sound(SOUND_JUMP, 21150)
end
destroyBall(a,b) -- remove ball
end
end
-- draw bucket
sprite("Cargo Bot:Level Select Frame",px,110,110,50)
-- save current high score
if score>currHS then
currHS=score
end
-- determine ball drop delay based on score
s=math.floor(score/100)+1
if s<1 then
s=1
end
if s>6 then
s=6
end
speed=time[s]
end
end
function touched(t)
px=px+t.deltaX*2 -- move bucket left or right
if px<0 then -- check if off left
px=0
end
if px>WIDTH then -- check if off right
px=WIDTH
end
end
function createBall() -- add ball to table
c=physics.body(CIRCLE,18)
c.x=math.random(WIDTH)
c.y=1100
c.restitution=.8
c.info=math.random(-50,50)
table.insert(btab,c)
end
function destroyBall(a,b) -- remove ball from table
b:destroy()
b=nil
table.remove(btab,a)
end
function createDots() -- add dots to table
for x=0,16 do
for y=1,9 do
d=physics.body(CIRCLE,2)
d.x=5+x*50
d.y=1000-y*80
d.gravityScale=0
d.type=STATIC
table.insert(dtab,d)
end
end
for x=0,15 do
for y=1,8 do
d=physics.body(CIRCLE,2)
d.x=5+x*50+20
d.y=960-y*80
d.gravityScale=0
d.type=STATIC
table.insert(dtab,d)
end
end
end