Kind of Platformer

For a while I have been looking at making a platformer in Codea and I have recently started trying stuff out. Right now it’s not a side scroller, the code is pretty messy, and it does not look like all that much. But I figured I’d post the code I have right now here anyway to see if anyone might have suggestions on how I can improve on it while it’s still pretty simple and easy for me to change without working too much on how it affects other parts of the code. Sorry if this is not the right category to put this sort of thing in, I figured it was the best place for it to go even though the code is by no means finished.

The code:

displayMode(FULLSCREEN)
function setup()
    g = readImage("Platformer Art:Block Grass")
    d = readImage("Platformer Art:Block Brick")
    c = readImage("Platformer Art:Crate")
    b = nil
    padx={50,155,WIDTH-110.1,51.5,154.5,WIDTH-110}
    pady={185,195,85,85,95,185}
    jump = 0
    jumping = false
    jusJump = 0
    touches={}
    char=vec2(40,40)
    curLvl = 1
    wot14 = 14
    lvls = {
    {
    {g,g,g,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d},
    {b,b,b,g,d,d,d,d,d,c,c,c,c,c,c,d,d,d,c,d},
    {b,b,b,b,g,d,d,d,d,c,d,d,d,d,c,d,d,d,c,d},    
    {b,b,b,b,b,g,d,d,d,c,d,d,d,d,c,c,d,d,c,d},
    {b,b,b,b,b,b,g,d,d,c,c,d,d,d,c,c,c,c,c,d},
    {b,b,b,b,b,b,b,g,c,c,c,c,c,c,c,c,d,c,c,c},
    {b,b,b,b,b,b,b,b,g,c,c,c,c,d,c,c,c,c,c,c},
    {b,b,b,b,b,b,b,b,b,c,c,c,d,d,c,c,d,d,c,c},
    {b,b,b,b,b,b,b,b,b,b,g,c,c,d,c,d,d,d,c,d},
    {b,b,b,b,b,b,b,b,b,b,b,g,c,c,c,d,d,d,c,d},
    {b,b,b,b,b,b,b,b,b,b,b,b,g,c,c,c,c,c,c,d},
    {b,b,b,b,b,b,b,b,b,b,b,b,b,d,c,c,c,d,d,d},
    {b,b,b,b,b,b,b,b,b,b,b,b,b,d,d,d,d,d,c,d},
    {b,b,b,b,b,b,b,b,b,b,b,b,b,g,d,c,c,c,c,d},
    {b,b,b,b,b,b,b,b,b,b,b,b,b,b,g,c,c,c,d,d},
    {b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,c,d,d,d,d},
    },
    {
    {g,g,g,g,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d},
    {b,b,b,b,g,d,d,d,d,d,d,d,d,d,d,g,d,c,c,d},
    {b,b,b,b,b,g,d,d,d,d,d,d,d,d,g,b,d,d,c,c},    
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,b,b,d,d,c,d},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,b,b,d,d,d,d},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,b,b,d,d,d,c},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,d,b,d,d,d,c},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,d,d,d,c,d,d},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,d,c,c,c,d,d},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,g,d,d,d,c,c},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,b,g,d,d,c,d},
    {d,d,d,d,c,c,c,c,c,c,c,c,c,c,b,b,g,d,c,c},
    {g,d,d,d,c,c,c,c,c,c,c,c,c,c,b,b,b,g,d,d},
    {b,g,g,g,c,c,c,c,c,c,c,c,c,c,b,b,b,b,g,g},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,b,b,b,b,b,b},
    {b,b,b,b,b,b,c,c,c,c,c,c,c,c,b,b,b,b,b,b},
    }
    }
end
function draw()
    background(0, 0, 0, 255)
 for w,i in pairs(lvls[curLvl]) do
   for c,d in pairs(i) do 
    sprite(d,WIDTH/20*(c-1)+25,(w-1)*50,50.5,50)  
   end
 end 
text("FPS: "..math.floor(1/DeltaTime), WIDTH-50, HEIGHT-25)
    var1=math.ceil((char.y+10.5)/50)
    var2=math.ceil((char.x+wot14)/(WIDTH/20))
    if var1 >= 16 then 
         var1 =16 
    elseif var1 < 1 then
         var1 =1
    end
    fill(127, 127, 127, 175)
     for o = 1,#padx do 
         ellipse(padx[o],pady[o],90)
      for a,t in pairs(touches) do
            z = math.sqrt(math.pow(padx[o]-t.x,2)+math.pow(pady[o]-t.y,2))
            math.randomseed(t.id)
        if z < 45 then   
            if padx[o] == padx[1] then 
                char.x = char.x - 2
               if lvls[curLvl][var1-1][var2-1] ~= b then
                wot14 = -14
               end
            end  
            if  padx[o] == padx[2] then 
                char.x = char.x + 2
              if lvls[curLvl][var1-1][var2+1] ~= b then
                wot14 = 14
              end  
            end 
          if padx[o] == padx[6] and jumping == false and lvls[curLvl][var1+1][var2] == b then 
                    jump = 15
                end  
          if padx[o] == padx[5] and lvls[curLvl][var1][var2+1] == c then 
                    lvls[curLvl][var1][var2+1] = b
                end
          if padx[o] == padx[4] and lvls[curLvl][var1][var2-1] == c then
                    lvls[curLvl][var1][var2-1] = b
                end
          if jump == 0 and padx[o] == padx[3] and lvls[curLvl][var1-1][var2] == c then 
                    lvls[curLvl][var1-1][var2] = b
                end
         if padx[o] == padx[6] and lvls[curLvl][var1+1][var2] == c then
                    lvls[curLvl][var1+1][var2] = b
                end
            end
        end 
    end 
    char.y = char.y + jump 
    lvls[curLvl][0] = lvls[curLvl][1]
    if lvls[curLvl][var1-1][var2] ~=b then 
    if char.y <= var1*50-60 then 
         char.y = var1*50-60
         jumping = false 
         jump = 0
         end 
    else
        jumping = true
        jump = -2.5
    end  
    if lvls[curLvl][var1-1][var2+1] == b then
       wot14=-14
    end 
   if lvls[curLvl][var1][var2+1] ~= b then
     if char.x >= WIDTH/20*var2-14.6 then
            char.x = WIDTH/20*var2-14.6
            char.x = char.x + 0 
        end  
    end 
    if lvls[curLvl][var1-1][var2-1] == b then
           wot14=14
        end
   if lvls[curLvl][var1][var2-1] ~= b then
      if char.x <= WIDTH/20*var2-34 then
            char.x = WIDTH/20*var2-34
            char.x = char.x - 0
        end  
     end 
    fill(0, 182, 255, 255)
    ellipse(char.x,char.y,30,30)
    if curLvl == 1 and char.x > WIDTH then 
        char.x = 40
        char.y = 40 
        curLvl = 2
    end
end
function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    else
        touches[touch.id] = touch
    end
end

Good start!

I did a bunch of posts starting here

https://coolcodea.wordpress.com/2014/09/09/156-creating-a-2d-platform-game-part-1/

@Ignatz, when you put

local mapText={ 
    '-----------------------',
    '-----------------------',
    '-----------------------',
    '-----------------------',
    '-----------------------',
    '-----------------------',
    '----m-----cu-----------',
    '----m---m-r------------',
    'b---m---b-------kb----b',
    'b@-----bb-----t--bF--lb',
    'ggggggggggggglggggggglg',
    'ggggggggggggglggggggglg',
    'ggggggggggggglggggggglg',
    'ggggggggggggglggggggglg',
    'g-----------glggggggglg',
    'g-L---------glggggggglg',
    'g--m---h-f---lggggggglg',
    'g-------i----lggggggglg',
    'g-ggggggggggggggggggglg',
    'g--------------------lg',
    'g----------m-----m---lg',
    'g-------y----s-------lg',
    'ggggwwggggggggggggggggg',
    'ggggggggggggggggggggggg'
}

Is this the real map or is a way to view how is gonna be on the screen?

The letters are just tokens, so ‘g’ means ‘use a grass tile’

@Ignatz, now I got it!

PS: Your blog, is amazing! I’ll use it to learn more! Thank you!

It’a a pleasure, try the ebooks too (see top of the index page).

Thanks @Ignatz your platformer tutorial is very in depth and is very useful. I think I could learn a lot from it. Although I must say I looked at the final code for the project and at nearly 1700 lines it does look a bit intimidating

I can assure you I wrote it one line at a time. The trick is to build it in small chunks and carefully put them together, testing all the time.

you know what would be a great tool? I was looking at this project. it would an awesome tool if when you copy/pasted code like your project into a new project in Codea, if it automatically split the classes up into their own tabs. your project doesn’t seem as daunting when each tab is only 50-100 lines long, compared to a single tab that’s 1700+ lines long.

you can do that with --#TabName If you long press your own project on the project selector screen and hit copy, it will be copied as a single item with the tabs separated like this

Thanks for sharing that @yojimbo2000 I didn’t know you could do that before but it makes it much more easier to view the code

Forgot to add the most important part, when you want to paste some code formatted in this way into a project in Codea, long press the add new project button in the project selector and as long as the contents of the clipboard has at least a --#Main tab, a paste into project option will show, hit that and it will reverse the process, separating the clipboard into separate tabs.

@thebombdiggity I suggest that you don’t putt any game code into main. It could make it harder to expand it in unforeseen ways because theres nothing higher. Its a lot easier to change this now than later.

Thanks for the suggestion @Goatboy76 I have been recently learning classes using Ignatz amazing tutorials but I when I was doing the project I didn’t feel I had them down well enough