Just bought Codea after struggling with the learning curve for Objective C. (Delphi background) This is so easy to start with compared to Objective C. Would appreciate any comments, this is the result of less than a day with Codea.
-- coOrdinator by John Melki-Wegner
-- Use this function to perform your initial setup
function setup()
print("coOrdinator!")
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
midx=WIDTH/2
midy=HEIGHT/2
-- grid scale
dx=WIDTH/15
timeleft=5000
-- playing 0 =not playing, 1 =playing
playing=0
-- current point
cx=0
cy=0
-- score
score=0
ellipsesize=10
toggle=true
end
-- get a new point
function getNewPoint()
cx=math.random(-5,5)
cy=math.random(-4,4)
end
function onGrid(tx,ty)
result = true
if tx>5 or tx<-5 then
result=false
end
if ty>4 or ty<-4 then
result=false
end
return result
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- draw grid and other objects=================
textMode(CORNER)
fontSize(32)
fill(3, 243, 24, 255)
text("coOrdinator",WIDTH-250,HEIGHT-50)
if playing==0 then
-- start button
fill(244, 243, 245, 255)
stroke(11, 13, 11, 255)
rect(10,HEIGHT-55,100,50)
fill(3, 3, 3, 255)
text("Start",25,HEIGHT-50)
end
-- draw axes
stroke(194, 63, 63, 255)
line(midx,dx,midx,HEIGHT-dx)
line(dx,midy,WIDTH-dx,midy)
-- draw grid points
stroke(132, 185, 85, 255)
for i=-5,5 do
for j=-4,4 do
ellipse(midx+i*dx,midy+j*dx,5,5)
end
end
-- show numbers
fontSize(16)
fill(248, 248, 248, 255)
for i=-5,5 do
text(i,midx+i*dx-5,midy)
end
for i=-4,4 do
text(i,midx+5,midy+i*dx-10)
end
-- show score
fontSize(30)
fill(3, 243, 24, 255)
sscore=string.format("Score=%d",score)
text(sscore,150,HEIGHT-50)
--game logic=========================
--timer
if playing==1 then
timeleft=timeleft-1
-- end game?
if timeleft<=0 then
playing=0
end
-- show timeleft by rectangle "progress bar"
stroke(130, 138, 131, 255)
fill(113, 156, 117, 255-timeleft/100)
rect(20,70,30,timeleft/8)
tl=string.format("%d",timeleft)
fontSize(14)
text(tl,20,timeleft/8+70)
end
-- touch events
-- touch position on grid
tx=math.floor(CurrentTouch.x/dx)-7
ty=math.floor(CurrentTouch.y/dx)-5
if playing==1 then
fill(ellipsesize*5, ellipsesize*5, 11, 255)
stroke(ellipsesize*2, ellipsesize*2, 11, 255)
if onGrid(tx,ty) then
ellipse(midx+tx*dx,midy+ty*dx,ellipsesize,ellipsesize)
sprite("Tyrian Remastered:Eggstroid",midx+tx*dx,midy+ty*dx)
end
fill(3, 243, 24, 255)
s=string.format("(%d,%d)",cx,cy)
fontSize(30)
text(s,midx-30,HEIGHT-50)
end
-- button pressed?
if CurrentTouch.x<=110 then
if CurrentTouch.y>=HEIGHT-55 then
if playing==0 then
-- button pressed
playing=1
timeleft=5000
score=0
getNewPoint()
end
end
end
-- correct point chosen?
if tx==cx and ty==cy then
score = score + 1
getNewPoint()
ellipsesize = ellipsesize + 1
end
-- annimation for correct point
if ellipsesize>10 then
sprite("Tyrian Remastered:Button Glow",midx+tx*dx,midy+ty*dx)
ellipsesize = ellipsesize + 2
if ellipsesize>= 50 then
ellipsesize=10
end
end
end