Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- --Platformer
- supportedOrientations(CurrentOrientation)
- -- Use this function to perform your initial setup
- function setup()
- --Character setup
- player = Character()
- --Level setup
- stage = Level()
- GUI:init()
- Download = readProjectData("Download",0)
- assetname = ""
- assetno = 0
- if Download==0 then
- print("Downloading Assets...")
- downloadimage(assetno)
- end
- end
- function draw()
- if Download==1 then
- background(0, 151, 255, 255)
- strokeWidth(5)
- camera(mainCharacter.x-WIDTH/2, mainCharacter.y-HEIGHT/3, 10, mainCharacter.x-WIDTH/2, mainCharacter.y-HEIGHT/3, 1)
- player:draw()
- stage:draw()
- controls:draw()
- GUI:draw()
- end
- end
- function touched(touch)
- controls:touched(touch)
- end
- function collide(contact)
- player:collide(contact)
- end
- function saveimage(image)
- saveImage("Project:"..assetname,image)
- assetno=assetno+1
- downloadimage(assetno)
- end
- function errorimage()
- error("Unable to Download Assets")
- end
- function downloadimage(no)
- if no==0 then
- http.request("https://www.dropbox.com/s/pfqwew7of1gmdnn/D-Pad%402x.png?dl=1",saveimage,errorimage)
- assetname="D-Pad"
- print("Asset 1/5")
- elseif no==1 then
- http.request("https://www.dropbox.com/s/qlf2zf14senz2x9/buttona.png?dl=1",saveimage,errorimage)
- assetname="ButtonA"
- print("Asset 2/5")
- elseif no==2 then
- http.request("https://www.dropbox.com/s/2pyvbwmcjmr59wt/buttonb.png?dl=1",saveimage,errorimage)
- assetname="ButtonB"
- print("Asset 3/5")
- elseif no==3 then
- http.request("https://www.dropbox.com/s/selodyqipndccj6/char3.png?dl=1",saveimage,errorimage)
- assetname="Char3"
- print("Asset 4/5")
- elseif no==4 then
- http.request("https://www.dropbox.com/s/h7kl73yvkt2q7n3/char4.png?dl=1",saveimage,errorimage)
- assetname="Char4"
- print("Asset 5/5")
- elseif no==5 then
- saveProjectData("Download",1)
- restart()
- else
- end
- end
- --# Character
- Character = class()
- function Character:init(x)
- -- you can accept and set parameters here
- spriteMode(CENTER)
- characterSize = 75
- characterSpeed = 6
- characterMaxSpeed = 400
- mainCharacter = physics.body(CIRCLE, characterSize/1.5)
- mainCharacter.gravityScale = 2
- mainCharacter.restitution = .2
- mainCharacter.x = WIDTH/2
- mainCharacter.y = HEIGHT/2
- characterGrounded = 0 -- 0 is false, 1 is true
- mainCharacter.friction = .2
- mainCharacter.fixedRotation = true
- CharAniPic=1
- anitime=ElapsedTime
- Lives=5
- end
- function Character:draw()
- if right == true and mainCharacter.linearVelocity.x < characterMaxSpeed and characterGrounded==1 then
- mainCharacter:applyForce(vec2(300,0))
- elseif right == true and mainCharacter.linearVelocity.x < characterMaxSpeed and characterGrounded==0 then
- mainCharacter:applyForce(vec2(100,0))
- elseif left == true and mainCharacter.linearVelocity.x > -characterMaxSpeed and characterGrounded==1 then
- mainCharacter:applyForce(vec2(-300,0))
- elseif left == true and mainCharacter.linearVelocity.x > -characterMaxSpeed and characterGrounded==0 then
- mainCharacter:applyForce(vec2(-100,0))
- end
- if CharAniPic==1 then
- sprite("Project:Char3", mainCharacter.x, mainCharacter.y, characterSize)
- elseif CharAniPic==2 then
- sprite("Project:Char4", mainCharacter.x, mainCharacter.y, characterSize)
- elseif CharAniPic==0 then
- sprite("Platformer Art:Guy Jump", mainCharacter.x,mainCharacter.y,characterSize)
- elseif CharAniPic==10 then
- sprite("Platformer Art:Guy Standing", mainCharacter.x,mainCharacter.y,characterSize)
- end
- if characterGrounded==0 then
- CharAniPic=0
- elseif ElapsedTime-anitime > 0.3 and characterGrounded==1 and mainCharacter.linearVelocity.x > 50 then
- anitime=ElapsedTime
- CharAniPic=CharAniPic+1
- elseif mainCharacter.linearVelocity.x < 50 and mainCharacter.linearVelocity.y < 50 then
- CharAniPic=10
- characterMoving=false
- end
- if CharAniPic>2 and CharAniPic<10 or CharAniPic>10 then
- CharAniPic=1
- end
- end
- function Character:collide(contact)
- if contact.state == BEGAN then
- if contact.bodyA ~= nil then
- if contact.bodyA.info == "ground" then
- characterGrounded = 1
- elseif contact.bodyB.info == "coin" then
- contact.bodyB.dest=true
- removeCoin()
- Coins=Coins+1
- end
- end
- end
- end
- --# Level
- Level = class()
- function Level:init()
- -- you can accept and set parameters here
- rectMode(CENTER)
- --0: empty
- --1:ground
- --2: wall
- --3: cloud
- --4: levelEnd
- --5: coin
- levelLayout =
- {
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,5,5,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2,2,1,1,0,0,0,0,0,4,2},
- {2,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,2}
- }
- tiles={}
- coins={}
- for i, row in ipairs(levelLayout) do
- for j, block in pairs(row) do
- if levelLayout[i][j]==1 or levelLayout[i][j]==2 then
- table.insert(tiles, TilePhysics(j,i,100,100,levelLayout[i][j]))
- elseif levelLayout[i][j]==5 then
- table.insert(coins, TileCoins(j,i,50,50,levelLayout[i][j]))
- end
- if levelLayout[i][j] == 4 then
- starSize = 100
- star = physics.body(CIRCLE, starSize/2)
- star.gravityScale = 5
- star.restitution = .2
- star.x = j*100
- star.y = HEIGHT-(100*1)
- star.info = "star"
- end
- end
- end
- end
- function Level:draw()
- -- Codea does not automatically call this method
- for i, row in ipairs(levelLayout) do
- for j, block in pairs(row) do
- if levelLayout[i][j] == 1 then
- sprite("Platformer Art:Block Grass", j*100, HEIGHT-(100*i), 100, 100)
- end
- if levelLayout[i][j] == 2 then
- sprite("Platformer Art:Block Brick", j*100, HEIGHT-(100*i), 100, 100)
- end
- if levelLayout[i][j] == 3 then
- sprite("Platformer Art:Cloud 2", j*100, HEIGHT-(100*i), 200, 100)
- end
- if levelLayout[i][j] == 4 then
- sprite("Small World:Flag", j*100, HEIGHT-(100*i), 70, 100)
- end
- if levelLayout[i][j] == 5 then
- sprite("Platformer Art:Coin", j*100, HEIGHT-(100*i), 50, 50)
- end
- end
- end
- end
- function TilePhysics(x,y,w,h,p)
- local tile = physics.body(POLYGON,
- vec2(-w/2, -h/2), --bottom left
- vec2(-w/2, h/2), --top left
- vec2(w/2, h/2), --top right
- vec2(w/2, -h/2) --bottom right
- )
- tile.gravityScale = 0
- tile.restitution = 0.1
- tile.x = x*100
- tile.y = HEIGHT-(100*y)
- tile.type = STATIC
- tile.friction = 1
- if p == 1 then
- tile.info = "ground"
- end
- if p == 2 then
- tile.info = "wall"
- end
- return tile
- end
- function TileCoins(x,y,w,h,p)
- local tile = physics.body(CIRCLE, w/2)
- tile.gravityScale = 0
- tile.restitution = 0.1
- tile.x = x*100
- tile.y = HEIGHT-(100*y)
- tile.type = STATIC
- tile.friction = 0
- tile.dest = false
- if p == 5 then
- tile.info = "coin"
- end
- return tile
- end
- function removeCoin()
- for x,y in pairs(coins) do
- if y.dest then -- if true then
- y:destroy() -- destroy body
- --table.remove(coins,y) -- remove body from table
- end
- end
- end
- --# Controls
- controls = class()
- function controls:init()
- end
- function controls:draw()
- sprite("Project:D-Pad",mainCharacter.x-400,mainCharacter.y-150,192)
- sprite("Project:ButtonA",mainCharacter.x+450,mainCharacter.y-100,96)
- sprite("Project:ButtonB",mainCharacter.x+365,mainCharacter.y-190,96)
- function controls:touched(touch)
- if touch.state == BEGAN then
- --Jump
- if characterGrounded == 1
- and touch.x > 835 and touch.x < 935
- and touch.y > 15 and touch.y < 115 then
- jump()
- end
- end
- if touch.state ~= ENDED then
- if touch.x > 130 and touch.x < 195 and touch.y > 80 and touch.y < 135 then
- right=true
- left=false
- elseif touch.x > 20 and touch.x < 85 and touch.y > 80 and touch.y < 135 then
- left=true
- right=false
- end
- end
- if touch.state==ENDED then
- right=false
- left=false
- end
- end
- end
- function jump()
- mainCharacter:applyForce(vec2(0, 5500))
- characterGrounded=0
- sound(SOUND_JUMP, 46551)
- end
- --# GUI
- GUI = class()
- function GUI:init()
- Coins=0
- TimeLeft=500
- time=ElapsedTime
- end
- function GUI:draw()
- sprite("Platformer Art:Coin",mainCharacter.x+400,mainCharacter.y+485)
- sprite("Small World:Flag",mainCharacter.x-485,mainCharacter.y+485)
- sprite("Small World:Heart",mainCharacter.x-20,mainCharacter.y+490)
- pushStyle()
- fill(255)
- font("ArialRoundedMTBold")
- fontSize(22)
- textMode(CORNER)
- text("x "..Coins,mainCharacter.x+435,mainCharacter.y+475)
- text(TimeLeft,mainCharacter.x-460,mainCharacter.y+470)
- text("x "..Lives,mainCharacter.x,mainCharacter.y+475)
- popStyle()
- if ElapsedTime-time > 1 then
- TimeLeft=TimeLeft-1
- time=ElapsedTime
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement