Advertisement
Guest User

2D Platformer

a guest
Jun 3rd, 2015
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. --# Main
  4. --Platformer
  5. supportedOrientations(CurrentOrientation)
  6. -- Use this function to perform your initial setup
  7. function setup()
  8.     --Character setup
  9.     player = Character()
  10.     --Level setup
  11.     stage = Level()
  12.     GUI:init()
  13.      Download = readProjectData("Download",0)
  14.      assetname = ""
  15.      assetno = 0
  16.     if Download==0 then
  17.         print("Downloading Assets...")
  18.         downloadimage(assetno)
  19.     end
  20. end
  21.  
  22.  
  23. function draw()
  24.     if Download==1 then
  25.         background(0, 151, 255, 255)
  26.         strokeWidth(5)
  27.         camera(mainCharacter.x-WIDTH/2, mainCharacter.y-HEIGHT/3, 10, mainCharacter.x-WIDTH/2, mainCharacter.y-HEIGHT/3, 1)
  28.         player:draw()
  29.         stage:draw()
  30.         controls:draw()
  31.         GUI:draw()
  32.     end
  33. end
  34.  
  35.  
  36. function touched(touch)
  37.     controls:touched(touch)
  38. end
  39.  
  40. function collide(contact)
  41.     player:collide(contact)
  42. end
  43.  
  44. function saveimage(image)
  45.     saveImage("Project:"..assetname,image)
  46.     assetno=assetno+1
  47.     downloadimage(assetno)
  48. end
  49.  
  50. function errorimage()
  51.     error("Unable to Download Assets")
  52. end
  53.  
  54. function downloadimage(no)
  55.     if no==0 then
  56.         http.request("https://www.dropbox.com/s/pfqwew7of1gmdnn/D-Pad%402x.png?dl=1",saveimage,errorimage)
  57.         assetname="D-Pad"
  58.         print("Asset 1/5")
  59.     elseif no==1 then
  60.         http.request("https://www.dropbox.com/s/qlf2zf14senz2x9/buttona.png?dl=1",saveimage,errorimage)
  61.         assetname="ButtonA"
  62.         print("Asset 2/5")
  63.     elseif no==2 then
  64.         http.request("https://www.dropbox.com/s/2pyvbwmcjmr59wt/buttonb.png?dl=1",saveimage,errorimage)
  65.         assetname="ButtonB"
  66.         print("Asset 3/5")
  67.     elseif no==3 then
  68.         http.request("https://www.dropbox.com/s/selodyqipndccj6/char3.png?dl=1",saveimage,errorimage)
  69.         assetname="Char3"
  70.         print("Asset 4/5")
  71.     elseif no==4 then
  72.     http.request("https://www.dropbox.com/s/h7kl73yvkt2q7n3/char4.png?dl=1",saveimage,errorimage)
  73.         assetname="Char4"
  74.         print("Asset 5/5")
  75.         elseif no==5 then
  76.         saveProjectData("Download",1)
  77.         restart()
  78. else
  79. end
  80. end
  81.  
  82. --# Character
  83. Character = class()
  84.  
  85. function Character:init(x)
  86.     -- you can accept and set parameters here
  87.     spriteMode(CENTER)
  88.     characterSize = 75
  89.     characterSpeed = 6
  90.     characterMaxSpeed = 400
  91.     mainCharacter = physics.body(CIRCLE, characterSize/1.5)
  92.     mainCharacter.gravityScale = 2
  93.     mainCharacter.restitution = .2
  94.     mainCharacter.x = WIDTH/2
  95.     mainCharacter.y = HEIGHT/2
  96.     characterGrounded = 0 -- 0 is false, 1 is true
  97.     mainCharacter.friction = .2
  98.     mainCharacter.fixedRotation = true
  99.    
  100.     CharAniPic=1
  101.     anitime=ElapsedTime
  102.    
  103.     Lives=5
  104. end
  105.  
  106. function Character:draw()
  107.    
  108.     if right == true and mainCharacter.linearVelocity.x < characterMaxSpeed and characterGrounded==1 then
  109.         mainCharacter:applyForce(vec2(300,0))
  110.     elseif right == true and mainCharacter.linearVelocity.x < characterMaxSpeed and characterGrounded==0 then
  111.         mainCharacter:applyForce(vec2(100,0))
  112.     elseif left == true and mainCharacter.linearVelocity.x > -characterMaxSpeed and characterGrounded==1 then
  113.         mainCharacter:applyForce(vec2(-300,0))
  114.     elseif left == true and mainCharacter.linearVelocity.x > -characterMaxSpeed and characterGrounded==0 then
  115.         mainCharacter:applyForce(vec2(-100,0))
  116.     end
  117.    
  118.     if CharAniPic==1 then
  119.         sprite("Project:Char3", mainCharacter.x, mainCharacter.y, characterSize)
  120.     elseif CharAniPic==2 then
  121.         sprite("Project:Char4", mainCharacter.x, mainCharacter.y, characterSize)
  122.     elseif CharAniPic==0 then
  123.         sprite("Platformer Art:Guy Jump", mainCharacter.x,mainCharacter.y,characterSize)
  124.     elseif CharAniPic==10 then
  125.         sprite("Platformer Art:Guy Standing", mainCharacter.x,mainCharacter.y,characterSize)
  126.     end
  127.    
  128.     if characterGrounded==0 then
  129.         CharAniPic=0
  130.     elseif ElapsedTime-anitime > 0.3 and characterGrounded==1 and mainCharacter.linearVelocity.x > 50 then
  131.         anitime=ElapsedTime
  132.         CharAniPic=CharAniPic+1
  133.     elseif mainCharacter.linearVelocity.x < 50 and mainCharacter.linearVelocity.y < 50 then
  134.         CharAniPic=10
  135.         characterMoving=false
  136.     end
  137.    
  138.     if CharAniPic>2 and CharAniPic<10 or CharAniPic>10 then
  139.         CharAniPic=1
  140.     end
  141.  
  142. end
  143.  
  144. function Character:collide(contact)
  145.     if contact.state == BEGAN then
  146.         if contact.bodyA ~= nil then
  147.             if contact.bodyA.info == "ground" then
  148.                 characterGrounded = 1
  149.             elseif contact.bodyB.info == "coin" then
  150.                 contact.bodyB.dest=true
  151.                 removeCoin()
  152.                 Coins=Coins+1
  153.             end
  154.         end
  155.     end
  156. end
  157. --# Level
  158. Level = class()
  159.  
  160. function Level:init()
  161.     -- you can accept and set parameters here
  162.     rectMode(CENTER)
  163.     --0: empty
  164.     --1:ground
  165.     --2: wall
  166.     --3: cloud
  167.     --4: levelEnd
  168.     --5: coin
  169.    
  170.     levelLayout =
  171.     {
  172.     {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},
  173.     {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},
  174.     {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},
  175.     {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},
  176.     {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},
  177.     {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},
  178.     {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}
  179.     }
  180.    
  181.     tiles={}
  182.     coins={}
  183.    
  184.     for i, row in ipairs(levelLayout) do
  185.         for j, block in pairs(row) do
  186.             if levelLayout[i][j]==1 or levelLayout[i][j]==2 then
  187.                 table.insert(tiles, TilePhysics(j,i,100,100,levelLayout[i][j]))
  188.             elseif levelLayout[i][j]==5 then
  189.                 table.insert(coins, TileCoins(j,i,50,50,levelLayout[i][j]))
  190.             end
  191.                 if levelLayout[i][j] == 4 then
  192.                     starSize = 100
  193.                     star = physics.body(CIRCLE, starSize/2)
  194.                     star.gravityScale = 5
  195.                     star.restitution = .2
  196.                    
  197.                     star.x = j*100
  198.                     star.y = HEIGHT-(100*1)
  199.                     star.info = "star"
  200.                 end
  201.         end
  202.     end
  203. end
  204.  
  205. function Level:draw()
  206.     -- Codea does not automatically call this method
  207.     for i, row in ipairs(levelLayout) do
  208.         for j, block in pairs(row) do
  209.             if levelLayout[i][j] == 1 then
  210.                 sprite("Platformer Art:Block Grass", j*100, HEIGHT-(100*i), 100, 100)
  211.             end
  212.             if levelLayout[i][j] == 2 then
  213.                 sprite("Platformer Art:Block Brick", j*100, HEIGHT-(100*i), 100, 100)
  214.             end
  215.             if levelLayout[i][j] == 3 then
  216.                 sprite("Platformer Art:Cloud 2", j*100, HEIGHT-(100*i), 200, 100)
  217.             end
  218.            
  219.             if levelLayout[i][j] == 4 then
  220.                 sprite("Small World:Flag", j*100, HEIGHT-(100*i), 70, 100)
  221.             end
  222.             if levelLayout[i][j] == 5 then
  223.                 sprite("Platformer Art:Coin", j*100, HEIGHT-(100*i), 50, 50)
  224.             end
  225.         end
  226.     end
  227.    
  228. end
  229.  
  230. function TilePhysics(x,y,w,h,p)
  231.    
  232.     local tile = physics.body(POLYGON,
  233.     vec2(-w/2, -h/2), --bottom left
  234.     vec2(-w/2, h/2), --top left
  235.     vec2(w/2, h/2), --top right
  236.     vec2(w/2, -h/2) --bottom right
  237.    
  238.     )
  239.    
  240.     tile.gravityScale = 0
  241.     tile.restitution = 0.1
  242.    
  243.     tile.x = x*100
  244.     tile.y = HEIGHT-(100*y)
  245.    
  246.     tile.type = STATIC
  247.     tile.friction = 1
  248.    
  249.     if p == 1 then
  250.         tile.info = "ground"
  251.     end
  252.     if p == 2 then
  253.         tile.info = "wall"
  254.     end
  255.    
  256.     return tile
  257. end
  258.  
  259. function TileCoins(x,y,w,h,p)
  260.    
  261.     local tile = physics.body(CIRCLE, w/2)
  262.  
  263.     tile.gravityScale = 0
  264.     tile.restitution = 0.1
  265.    
  266.     tile.x = x*100
  267.     tile.y = HEIGHT-(100*y)
  268.    
  269.     tile.type = STATIC
  270.     tile.friction = 0
  271.     tile.dest = false
  272.    
  273.     if p == 5 then
  274.         tile.info = "coin"
  275.     end
  276.    
  277.     return tile
  278. end
  279.  
  280. function removeCoin()
  281.     for x,y in pairs(coins) do
  282.         if y.dest then    -- if true then
  283.         y:destroy()    -- destroy body
  284.         --table.remove(coins,y)    -- remove body from table
  285.         end
  286.     end
  287. end
  288.  
  289. --# Controls
  290. controls = class()
  291.  
  292. function controls:init()
  293.    
  294. end
  295.  
  296. function controls:draw()
  297.     sprite("Project:D-Pad",mainCharacter.x-400,mainCharacter.y-150,192)
  298.     sprite("Project:ButtonA",mainCharacter.x+450,mainCharacter.y-100,96)
  299.     sprite("Project:ButtonB",mainCharacter.x+365,mainCharacter.y-190,96)
  300.    
  301.     function controls:touched(touch)
  302.         if touch.state == BEGAN then
  303.             --Jump
  304.             if characterGrounded == 1
  305.             and touch.x > 835 and touch.x < 935
  306.             and touch.y > 15 and touch.y < 115 then
  307.                 jump()
  308.             end
  309.         end
  310.         if touch.state ~= ENDED then
  311.             if touch.x > 130 and touch.x < 195 and touch.y > 80 and touch.y < 135 then
  312.                 right=true
  313.                 left=false
  314.             elseif touch.x > 20 and touch.x < 85 and touch.y > 80 and touch.y < 135 then
  315.                 left=true
  316.                 right=false
  317.             end
  318.         end
  319.         if touch.state==ENDED then
  320.             right=false
  321.             left=false
  322.         end
  323.        
  324.     end
  325.    
  326. end
  327.  
  328. function jump()
  329.     mainCharacter:applyForce(vec2(0, 5500))
  330.     characterGrounded=0
  331.     sound(SOUND_JUMP, 46551)
  332. end
  333.  
  334. --# GUI
  335. GUI = class()
  336.  
  337. function GUI:init()
  338.     Coins=0
  339.     TimeLeft=500
  340.     time=ElapsedTime
  341. end
  342.  
  343. function GUI:draw()
  344.     sprite("Platformer Art:Coin",mainCharacter.x+400,mainCharacter.y+485)
  345.     sprite("Small World:Flag",mainCharacter.x-485,mainCharacter.y+485)
  346.     sprite("Small World:Heart",mainCharacter.x-20,mainCharacter.y+490)
  347.     pushStyle()
  348.     fill(255)
  349.     font("ArialRoundedMTBold")
  350.     fontSize(22)
  351.     textMode(CORNER)
  352.     text("x "..Coins,mainCharacter.x+435,mainCharacter.y+475)
  353.     text(TimeLeft,mainCharacter.x-460,mainCharacter.y+470)
  354.     text("x "..Lives,mainCharacter.x,mainCharacter.y+475)
  355.     popStyle()
  356.    
  357.     if ElapsedTime-time > 1 then
  358.         TimeLeft=TimeLeft-1
  359.         time=ElapsedTime
  360.     end
  361. end
Advertisement
RAW Paste Data Copied
Advertisement