Skip to content

Instantly share code, notes, and snippets.

@barisdokudur
Last active September 20, 2016 11:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save barisdokudur/7397658 to your computer and use it in GitHub Desktop.
Save barisdokudur/7397658 to your computer and use it in GitHub Desktop.
Platform Game
--# Barier
Barier = class()
function Barier:init(x)
bariers={}
barierno=1
for i=1, 5 do
Barier:CreateBarier()
end
end
function Barier:CreateBarier()
offsetX=barierimage.width/2
offsetY=barierimage.height/2
bariers[barierno]={}
bariers[barierno].x = math.random(500,10*WIDTH)
bariers[barierno].y = imageground.height
bariers[barierno].info={offsetX, offsetY, "barier", true}
barierno = barierno + 1
c=-1
end
function Barier:draw()
for k=1, #bariers do
bariers[k].x = bariers[k].x - speed
if bariers[k].y<=imageground.height - barierimage.height/2 then
c=barierimage.height/10
elseif bariers[k].y>=imageground.height then
c=-1
end
bariers[k].y = bariers[k].y + c*0.5
if bariers[k].info[4]==true and bariers[k].x<WIDTH then
pushMatrix()
pushStyle()
resetMatrix()
resetStyle()
translate(bariers[k].x, bariers[k].y)
spriteMode(CENTER)
sprite(barierimage, bariers[k].info[1], bariers[k].info[2])
popStyle()
popMatrix()
if (hero[1].x-barierimage.width/2<bariers[k].x and bariers[k].x<hero[1].x+barierimage.width/2) and
(hero[1].y-barierimage.height/2<bariers[k].y and bariers[k].y<hero[1].y+barierimage.height/2) then
bariers[k].info[4]=false
live = live - 1
deadno = deadno + 1
sound(SOUND_EXPLODE, 43776)
end
end
end
end
--# Box
Box = class()
function Box:init(x)
boxes={}
Box:CreateRandomBox(.1)
end
function Box:CreateRandomBox(probabilty)
for i=boximage.width/2, platform, boximage.width do
a=math.random(0, 1/probabilty)
if a==1 and i>500 then
offsetX=boximage.width/2
offsetY=boximage.height/2
points = {vec2(0,0),vec2(0,boximage.height),vec2(boximage.width,boximage.height),vec2(boximage.width,0)}
poly2=physics.body(POLYGON, unpack(points))
poly2.type=STATIC
poly2.x = i
poly2.y = imageground.height
poly2.fixedRotation=true
poly2.angle=0
poly2.sleepingAllowed = true
poly2.restitution = 0
poly2.gravityScale = 2
poly2.info={offsetX, offsetY, "box"}
table.insert(boxes,poly2)
end
end
end
function Box:draw()
for k,poly2 in pairs(boxes) do
poly2.x = poly2.x - speed
if poly2.x<WIDTH then
pushMatrix()
pushStyle()
resetMatrix()
resetStyle()
translate(poly2.x, poly2.y)
rotate(poly2.angle)
spriteMode(CENTER)
sprite(boximage, poly2.info[1], poly2.info[2])
popStyle()
popMatrix()
end
end
end
--# Fps
Fps = class()
function Fps:init()
parameter.watch("fps")
parameter.watch("mem")
end
function Fps:update()
fps = math.ceil(string.format("%.2f", 1/DeltaTime))
collectgarbage()
collectgarbage()
collectgarbage()
mem = collectgarbage("count", 2)
end
--# Gold
Gold = class()
local goldno
function Gold:init()
golds={}
goldno=1
for i=1, 15 do
Gold:CreateGoldArc(math.random(500, 10*WIDTH), 100, 180, 5)
end
distance=0
beta=0
distanceclose=20
end
function Gold:CreateGoldArc(Cx, Cy, d1, number)
angleincrement = 90/number
Gold:CreateGold(Cx, Cy+d1)
angle=0
for j=1, number-1 do
angle = angle + angleincrement
Gold:CreateGold(Cx+math.cos(math.rad(angle))*d1, Cy+math.sin(math.rad(angle))*d1)
Gold:CreateGold(Cx-math.cos(math.rad(angle))*d1, Cy+math.sin(math.rad(angle))*d1)
end
end
function Gold:CreateGold(Gx, Gy)
offsetX=goldimage.width/2
offsetY=goldimage.height/2
golds[goldno]={}
golds[goldno].x = Gx
golds[goldno].y = Gy
golds[goldno].info={offsetX, offsetY, "gold", true, false}
goldno = goldno + 1
end
function Gold:draw()
herox=hero[1].x+heroimage[1].width/2-goldimage.width/2
heroy=hero[1].y+heroimage[1].height/2-goldimage.height/2
if magnetactive==true then
fill(31, 38, 112, 255)
font("SourceSansPro-Bold")
fontSize(26)
text(string.format("%s%d","Magnet Time: ", magnetactivetime-(os.clock()-magnetstart)),WIDTH-100,HEIGHT-25)
end
for k=1, #golds do
golds[k].x = golds[k].x - speed
if golds[k].x<WIDTH and golds[k].info[4]==true then
if os.clock()-magnetstart>magnetactivetime then
magnetactive=false
end
if magnetactive==true or golds[k].info[5]==true then
goldx=golds[k].x
goldy=golds[k].y
Gold:DistanceGoldHero(herox, heroy, goldx, goldy)
if distance<500 then
if (goldx>herox and goldy>heroy) then
beta = math.acos((goldx-herox)/distance)
golds[k].y=golds[k].y-math.sin(beta)*distanceclose
golds[k].x=golds[k].x-math.cos(beta)*distanceclose
elseif (goldx>herox and goldy<heroy) then
beta = math.acos((goldx-herox)/distance)
golds[k].y=golds[k].y+math.sin(beta)*distanceclose
golds[k].x=golds[k].x-math.cos(beta)*distanceclose
elseif (goldx<herox and goldy>heroy) then
beta = math.acos((herox-goldx)/distance)
golds[k].y=golds[k].y-math.sin(beta)*distanceclose
golds[k].x=golds[k].x+math.cos(beta)*distanceclose
elseif (goldx<herox and goldy<heroy) then
beta = math.acos((herox-goldx)/distance)
golds[k].y=golds[k].y+math.sin(beta)*distanceclose
golds[k].x=golds[k].x+math.cos(beta)*distanceclose
end
golds[k].info[5]=true
end
end
pushMatrix()
pushStyle()
resetMatrix()
resetStyle()
translate(golds[k].x, golds[k].y)
spriteMode(CENTER)
sprite(goldimage, golds[k].info[1], golds[k].info[2])
popStyle()
popMatrix()
if (hero[1].x-goldimage.width/2<golds[k].x and golds[k].x<hero[1].x+goldimage.width/2) and
(hero[1].y-goldimage.height/2<golds[k].y and golds[k].y<hero[1].y+goldimage.height/2) then
golds[k].info[4]=false
gold = gold + 1
sound(SOUND_PICKUP, 43765)
end
end
end
end
function Gold:DistanceGoldHero(Hx, Hy, Gx, Gy)
distance=math.sqrt(math.pow(Hx-Gx,2)+math.pow(Hy-Gy,2))
end
--# Ground
Ground = class()
local a
function Ground:init()
w,h=spriteSize(imageground)
walls = {physics.body(EDGE,vec2(0,h),vec2(platform,h)), physics.body(EDGE,vec2(0,HEIGHT),vec2(platform,HEIGHT))}
for k, v in ipairs(walls) do
v.restitution = .0
v.info = {"","","ground"}
end
a = 0
clouds={}
for i=1, 30, 1 do
table.insert(clouds, {math.random(0, platform), math.random(350, HEIGHT-150)})
end
gold=0
meter=0
end
function Ground:draw()
sprite(imagebackground,WIDTH/2,HEIGHT/2)
Barier:draw()
a = a - speed
meter = meter + .1
for i=w/2, platform, w do
sprite(imageground, i + a, 35)
end
for j,clouds in ipairs(clouds) do
sprite(imagecloud, clouds[1] + a, clouds[2])
end
end
--# Hero
Hero = class()
function Hero:init()
hero={}
heroimage={}
heroimage[1]=heroimage1
heroimage[2]=heroimage2
heroimage[3]=heroimage3
heroimage[4]=heroimage4
offsetX=heroimage[1].width/2
offsetY=heroimage[1].height/2
points = {vec2(0,0),vec2(0,heroimage[1].height),vec2(heroimage[1].width,heroimage[1].height),vec2(heroimage[1].width,0)}
poly=physics.body(POLYGON, unpack(points))
poly.type=DYNAMIC
poly.x = 30
poly.y = 285
poly.fixedRotation=true
poly.angle=0
poly.sleepingAllowed = true
poly.restitution = 0
poly.gravityScale = 2
poly.info={offsetX, offsetY, "hero"}
table.insert(hero,poly)
touchground=true
walkingindex=1
turn=1
end
function Hero:draw()
heartx=25
hearty=HEIGHT-25
fill(31, 38, 112, 255)
font("SourceSansPro-Bold")
fontSize(26)
text(string.format("%s%d%s%d","Distance: ", meter, " Gold: ", gold),WIDTH/2,HEIGHT-25)
if poly.x - math.fmod(poly.x,1)<30 then
poly.x = poly.x + 1 - math.fmod(poly.x,1)
elseif poly.x - math.fmod(poly.x,1)>30 then
poly.x = poly.x - 1 - math.fmod(poly.x,1)
end
pushMatrix()
pushStyle()
resetMatrix()
resetStyle()
translate(poly.x, poly.y)
rotate(poly.angle)
spriteMode(CENTER)
if touchground==true then
sprite(heroimage[walkingindex], poly.info[1], poly.info[2])
if turn + 1 > walkingspeed then
turn=1
else
turn = turn + 1
end
if turn==walkingspeed then
if walkingindex + 1 > #heroimage then
walkingindex = 1
else
walkingindex = walkingindex + 1
end
end
else
sprite(heroimage[1], poly.info[1], poly.info[2])
end
popStyle()
popMatrix()
fill(255, 0, 0, 255)
if intouch==true then
ellipse(xt, yt, 35)
fill(255, 0, 0, 255)
font("SourceSansPro-Bold")
fontSize(26)
text("TOUCHED",xt+80,yt)
end
for i=1, live do
sprite(heartimage1,heartx,hearty,heartimage1.width/1.6,heartimage1.height/1.6)
heartx = heartx + heartimage1.width/1.6+5
end
for i=live+1, live+deadno do
sprite(heartimage2,heartx,hearty,heartimage2.width/1.6,heartimage2.height/1.6)
heartx = heartx + heartimage2.width/1.6+5
end
end
function Hero:touched(touch)
xt=touch.x
yt=touch.y
if touch.state==BEGAN then
intouch=true
if touchground==true then
poly.linearVelocity=vec2(0,525)
touchground=false
sound(DATA, "ZgBAOCBPQEVAQEBAAAAAAO3iwDxxYLg9AABAf0BAQEBAQEBA")
end
elseif touch.state==ENDED then
intouch=false
end
end
function Hero:collide(contact)
if (contact.bodyA.info[3]=="hero" and contact.bodyB.info[3]=="ground") or
(contact.bodyA.info[3]=="ground" and contact.bodyB.info[3]=="hero") or
(contact.bodyA.info[3]=="hero" and contact.bodyB.info[3]=="box") or
(contact.bodyA.info[3]=="box" and contact.bodyB.info[3]=="hero") then
touchground=true
end
end
--# Magnet
Magnet = class()
local magnetno
function Magnet:init()
magnets={}
magnetno=1
magnetactive=false
magnetactivetime=10
magnetstart=0
for i=1, 2 do
Magnet:CreateMagnet(math.random(500, 10*WIDTH), 100)
end
end
function Magnet:CreateMagnet(Gx, Gy)
offsetX=magnetimage.width/2
offsetY=magnetimage.height/2
magnets[magnetno]={}
magnets[magnetno].x = Gx
magnets[magnetno].y = Gy
magnets[magnetno].info={offsetX, offsetY, "magnet", true}
magnetno = magnetno + 1
end
function Magnet:draw()
for k=1, #magnets do
magnets[k].x = magnets[k].x - speed
if magnets[k].info[4]==true and magnets[k].x<WIDTH then
pushMatrix()
pushStyle()
resetMatrix()
resetStyle()
translate(magnets[k].x, magnets[k].y)
spriteMode(CENTER)
sprite(magnetimage, magnets[k].info[1], magnets[k].info[2], 55, 55)
popStyle()
popMatrix()
if (hero[1].x-magnetimage.width/2<magnets[k].x and magnets[k].x<hero[1].x+magnetimage.width/2) and
(hero[1].y-magnetimage.height/2<magnets[k].y and magnets[k].y<hero[1].y+magnetimage.height/2) then
magnets[k].info[4]=false
magnetactive=true
magnetstart=os.clock()
sound(SOUND_PICKUP, 43782)
end
end
end
end
--The name of the project must match your Codea project name if dependencies are used.
--Project: Platform Game
--Version: Alpha 1.0
--Comments:
--# Game
supportedOrientations(LANDSCAPE_ANY)
function setup()
LoadImages()
end
function setup2()
lineCapMode(ROUND)
displayMode(FULLSCREEN)
platform=10*WIDTH
speed=4
walkingspeed=4
live=3
deadno=0
campos=WIDTH
defaultGravity = physics.gravity()
Ground = Ground()
Spring = Spring()
Box = Box()
Gold = Gold()
Hero = Hero()
Barier = Barier()
Magnet = Magnet()
monitor = Fps()
end
function draw()
if imageStatus=="Loading" then return end
physics.gravity(defaultGravity)
Ground:draw()
Box:draw()
Gold:draw()
Hero:draw()
Magnet:draw()
Spring:draw()
monitor:update()
end
function touched(touch)
Hero:touched(touch)
end
function collide(contact)
Hero:collide(contact)
end
function LoadImages()
imageStatus="Ready"
output.clear()
barierimage = LoadImage("Dropbox:spikes","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/spikes_zps553e79ab.png")
boximage = LoadImage("Dropbox:box","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/box_zpsd492d278.png")
goldimage=LoadImage("Dropbox:coinGold","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/coinGold_zps2f2ed721.png")
imageground=LoadImage("Dropbox:grassMid","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/grassMid_zps82253407.png")
imagecloud=LoadImage("Dropbox:cloud3","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/cloud3_zpsfb50ae7d.png")
imagebackground=LoadImage("Dropbox:Background","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/Background_zps9b3ce30b.png")
heroimage1=LoadImage("Dropbox:p1_walk03","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/p1_walk03_zpsbb126e5d.png")
heroimage2=LoadImage("Dropbox:p1_walk04","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/p1_walk04_zps13d5a79b.png")
heroimage3=LoadImage("Dropbox:p1_walk05","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/p1_walk05_zps7019c3d6.png")
heroimage4=LoadImage("Dropbox:p1_walk06","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/p1_walk06_zpsd0a01715.png")
springimage1=LoadImage("Dropbox:springboardDown","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/springboardDown_zpsa011e421.png")
springimage2=LoadImage("Dropbox:springboardUp","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/springboardUp_zps83181fa0.png")
heartimage1=LoadImage("Dropbox:hud_heartFull","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/hud_heartFull_zpseb3b197f.png")
heartimage2=LoadImage("Dropbox:hud_heartEmpty","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/hud_heartEmpty_zps89543cb1.png")
magnetimage=LoadImage("Dropbox:magnet (1)","http://i878.photobucket.com/albums/ab346/baris_dokudur/BARIS-VAIO/magnet1_zps9a6a73d5.png")
if imageStatus=="Ready" then
setup2()
end
end
function LoadImage(fileName,url)
local i=readImage(fileName)
if i~=nil then return i end
if imageTable==nil then imageTable={} end
imageTable[#imageTable+1]={name=fileName,url=url}
print("Queueing...",fileName)
imageStatus="Loading"
if #imageTable==1 then
http.request(imageTable[1].url,ImageDownloaded)
print("Loading...",imageTable[1].name)
end
end
function ImageDownloaded(img)
print(imageTable[1].name,"loaded")
saveImage(imageTable[1].name,img)
table.remove(imageTable,1)
if #imageTable>0 then
http.request(imageTable[1].url,ImageDownloaded)
print("Loading",imageTable[1].name)
else
LoadImages()
end
end
--# Spring
Spring = class()
function Spring:init(x)
springs={}
springno=1
for i=1, 5 do
Spring:CreateSpring()
end
end
function Spring:CreateSpring()
offsetX=springimage1.width/2
offsetY=springimage1.height/2
offsetX2=springimage2.width/2
offsetY2=springimage2.height/2
springs[springno]={}
springs[springno].x = math.random(500,10*WIDTH)
springs[springno].y = imageground.height
springs[springno].info={offsetX, offsetY, "spring", true, offsetX2, offsetY2}
springno = springno + 1
end
function Spring:draw()
for k=1, #springs do
springs[k].x = springs[k].x - speed
pushMatrix()
pushStyle()
resetMatrix()
resetStyle()
translate(springs[k].x, springs[k].y)
spriteMode(CENTER)
if springs[k].info[4]==true and springs[k].x<WIDTH then
sprite(springimage1, springs[k].info[1], springs[k].info[2])
else
sprite(springimage2, springs[k].info[1], springs[k].info[2])
end
popStyle()
popMatrix()
if springs[k].info[4]==true then
if (hero[1].x-springimage1.width/2<springs[k].x and springs[k].x<hero[1].x+springimage1.width/2) and
(hero[1].y-springimage1.height/2<springs[k].y and springs[k].y<hero[1].y+springimage1.height/2) then
springs[k].info[4]=false
hero[1].linearVelocity=vec2(0,725)
touchground=false
sound(DATA, "ZgBAOCBPQEVAQEBAAAAAAAAAgD9xYLg9AABAf0BAQEBAQEBA")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment