A few days ago my friend challenged me to create a flappy bird copy. I had an idea to play the game as yourself by using pictures. @West’s code helped me a lot and I added a camera function but everytime I take a picture codea crashes. Is this a bug in my game or a bug in codea?
Btw, this is my code
-- flappy
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
function setup()
READY=1
PICTURE=2
PLAYING=3
DYING=4
ROUNDUP=5
gamestate=READY
pipe={}
pipetop={}
t={}
t2={}
you="Planet Cute:Character Boy"
if readLocalData("hiscore")~=nil then
hiscore=readLocalData("hiscore")
else
hiscore=0
end
base={}
for i=-200,WIDTH+200,100 do
table.insert(base,{x=i})
end
ground=physics.body(EDGE,vec2(0,100),vec2(WIDTH,70))
bird=physics.body(CIRCLE, 30)
bird.gravityScale=7
initialise()
end
function initialise()
bird.type=STATIC
bird.position=vec2(300,500)
gap=250
w=30
speed=10
flash=0
deadcount=0
startdist=700
score=0
for i=1,3 do
t[i]=math.random(HEIGHT-600)+150
t2[i]=HEIGHT-(t[i]+gap)
x=startdist+i*400
pipe[i]=physics.body(POLYGON,vec2(-w,-t[i]/2),vec2(-w,t[i]/2),vec2(w,t[i]/2),vec2(w,-t[i]/2))
pipe[i].position=vec2(x,t[i]/2)
pipe[i].type=STATIC
pipetop[i]=physics.body(POLYGON,vec2(-w,-t2[i]/2),vec2(-w,t2[i]/2),vec2(w,t2[i]/2),vec2(w,-t2[i]/2))
pipetop[i].position=vec2(x,HEIGHT-t2[i]/2)
pipetop[i].type=STATIC
pipetop[i].flag=0
end
end
function draw()
if flash==1 then
background(255,200,20)
flash=flash+1
else
background(6, 163, 252, 255)
end
if gamestate==READY then
bird.type=STATIC
rect(3*WIDTH/12,5*HEIGHT/14,WIDTH/2,2*HEIGHT/14)
sprite("Documents:Camera",WIDTH/2,6*HEIGHT/14, 100, 100)
fill(255)
font("Inconsolata")
fontSize(64)
text("FLAPPY YOU!",WIDTH/2,2*HEIGHT/3)
fontSize(32)
text("Tap to start",WIDTH/2,HEIGHT/3)
else
bird.type=DYNAMIC
end
sprite(you,bird.x,bird.y,100,200)
for k,p in pairs(pipe) do
fill(0, 255, 11, 255)
rect(p.x+p.points[1].x,p.y+p.points[1].y,2*w,t[k])
if gamestate==PLAYING then
p.x = p.x + -speed
if p.x<-w then p.x = p.x + 1200 end
end
end
for k,p in pairs(pipetop) do
rect(p.x+p.points[1].x,p.y+p.points[1].y,2*w,t2[k])
if gamestate==PICTURE then
background(255, 255, 255, 255)
cameraSource(CAMERA_FRONT)
bird.type=STATIC
sprite(CAMERA, WIDTH/2, HEIGHT/2)
end
if gamestate==PLAYING then
p.x = p.x + -speed
if p.x<bird.x and p.flag==0 then
score = score + 1
sound(SOUND_PICKUP, 36930)
p.flag=1
end
if p.x<-w then p.x = p.x + 1200 p.flag=0 end
end
end
for i,b in pairs(base) do
sprite("Platformer Art:Water",b.x,45,100,100)
if gamestate==READY or gamestate==PLAYING then
b.x = b.x -speed
if b.x<-100 then b.x = b.x + 1200 end
end
end
--display score
fill(255)
fontSize(64)
text(score,WIDTH/2,11*HEIGHT/12)
if gamestate==DYING then
deadcount = deadcount + 1
if deadcount>25 then
gamestate=ROUNDUP
sound(SOUND_EXPLODE, 41960)
end
end
if gamestate==ROUNDUP then
if score>hiscore then
hiscore=score
saveLocalData("hiscore",score)
end
fill(255)
fontSize(64)
text("GAME OVER!",WIDTH/2,7*HEIGHT/9)
fill(168, 144, 51, 255)
rect(WIDTH/6,13*HEIGHT/24,2*WIDTH/3,HEIGHT/6)
fill(255)
fontSize(32)
text("Score: "..score,WIDTH/2,16*HEIGHT/24)
text("High Score: "..hiscore,WIDTH/2,14*HEIGHT/24)
fill(218, 195, 72, 255)
rect(3*WIDTH/12,5*HEIGHT/14,WIDTH/2,2*HEIGHT/14)
fill(166, 39, 39, 255)
fontSize(64)
text("Go!",WIDTH/2,6*HEIGHT/14)
end
end
function collide(c)
if c.state==BEGAN then
gamestate=DYING
flash = flash + 1
end
end
function takePhoto()
capturedImage = image(CAMERA)
if takePhoto() then
you=capturedImage
end
end
function touched(t)
if CurrentTouch.state==BEGAN then
if gamestate==READY and
CurrentTouch.x>3*WIDTH/12 and
CurrentTouch.x<3*WIDTH/12+WIDTH/2 and
CurrentTouch.y>5*HEIGHT/14 and
CurrentTouch.y<5*HEIGHT/14+2*HEIGHT/14 then
gamestate=PICTURE
initialise()
elseif gamestate==READY then
gamestate=PLAYING
bird.type=DYNAMIC
elseif gamestate==PICTURE then
takePhoto()
gamestate=READY
bird.linearVelocity=vec2(0,500)
elseif gamestate==PLAYING then
bird.linearVelocity=vec2(0,700)
elseif gamestate==ROUNDUP and
CurrentTouch.x>3*WIDTH/12 and
CurrentTouch.x<3*WIDTH/12+WIDTH/2 and
CurrentTouch.y>5*HEIGHT/14 and
CurrentTouch.y<5*HEIGHT/14+2*HEIGHT/14 then
gamestate=READY
initialise()
end
end
end