Build a Platformer with Love2D

Creating Platforms

20 min+40 XP

Creating Platforms

Now let's add platforms for our player to jump on. We'll create a table of platforms and draw them all.

Platform Data Structure

We'll store platforms as a list of rectangles:

lua
platforms = { {x = 0, y = 250, width = 400, height = 50}, -- Ground {x = 50, y = 180, width = 80, height = 15}, -- Platform 1 {x = 200, y = 130, width = 100, height = 15}, -- Platform 2 }

Drawing Multiple Platforms

We use a for loop to draw all platforms:

lua
for i, platform in ipairs(platforms) do love.graphics.rectangle("fill", platform.x, platform.y, platform.width, platform.height) end

Complete Code with Platforms

lua
function love.load() player = { x = 50, y = 200, width = 30, height = 40, speed = 200, velocityY = 0, gravity = 800, jumpForce = -350, onGround = false } -- Define platforms platforms = { {x = 0, y = 250, width = 400, height = 50}, -- Ground {x = 50, y = 190, width = 80, height = 15}, -- Low left {x = 180, y = 150, width = 100, height = 15}, -- Middle {x = 300, y = 100, width = 80, height = 15}, -- High right } end function love.update(dt) -- Horizontal movement if love.keyboard.isDown("left") or love.keyboard.isDown("a") then player.x = player.x - player.speed * dt end if love.keyboard.isDown("right") or love.keyboard.isDown("d") then player.x = player.x + player.speed * dt end -- Apply gravity player.velocityY = player.velocityY + player.gravity * dt player.y = player.y + player.velocityY * dt -- Reset ground state player.onGround = false -- Check collision with all platforms for i, platform in ipairs(platforms) do if checkCollision(player, platform) then -- Land on top of platform if player.velocityY > 0 then player.y = platform.y - player.height player.velocityY = 0 player.onGround = true end end end -- Screen boundaries if player.x < 0 then player.x = 0 end if player.x + player.width > 400 then player.x = 400 - player.width end end function checkCollision(a, b) return a.x < b.x + b.width and a.x + a.width > b.x and a.y < b.y + b.height and a.y + a.height > b.y end function love.keypressed(key) if (key == "space" or key == "w" or key == "up") and player.onGround then player.velocityY = player.jumpForce player.onGround = false end end function love.draw() -- Sky love.graphics.setColor(0.4, 0.7, 1) love.graphics.rectangle("fill", 0, 0, 400, 300) -- Draw platforms love.graphics.setColor(0.4, 0.8, 0.4) for i, platform in ipairs(platforms) do love.graphics.rectangle("fill", platform.x, platform.y, platform.width, platform.height) end -- Player love.graphics.setColor(1, 0.3, 0.3) love.graphics.rectangle("fill", player.x, player.y, player.width, player.height) -- Eyes love.graphics.setColor(1, 1, 1) love.graphics.circle("fill", player.x + 10, player.y + 12, 5) love.graphics.circle("fill", player.x + 22, player.y + 12, 5) love.graphics.setColor(0, 0, 0) love.graphics.circle("fill", player.x + 12, player.y + 12, 2) love.graphics.circle("fill", player.x + 24, player.y + 12, 2) love.graphics.setColor(1, 1, 1) love.graphics.print("Jump on the platforms!", 10, 10) end

Try it! Jump up and land on the different platforms!

Ready to experiment?

Open the sandbox to modify and run the code yourself.