Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2012 10:45
Show Gist options
  • Select an option

  • Save anonymous/2463428 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/2463428 to your computer and use it in GitHub Desktop.
main.lua
function love.load()
gfx = love.graphics
playerColor = {255, 242, 109}
groundColor = {72, 203, 93}
backgroundColor = {42, 103, 52}
xPos = 300
yPos = 300
playerWidth = 40
playerHeight = 40
xVelocity = 0
yVelocity = 0
playerState = "jump"
playerJumpVelocity = -800
runSpeed = 500
gravity = 1800
yFloor = 500
end
function love.update(dt)
--move
if love.keyboard.isDown("right") then
xVelocity = runSpeed
end
if love.keyboard.isDown("left") then
xVelocity = -1 * runSpeed
end
--jump
if not (playerState == "jump") and love.keyboard.isDown("x") then
--make the player jump
yVelocity = playerJumpVelocity
playerState = "jump"
end
--update player position
xPos = xPos + (xVelocity * dt)
yPos = yPos + (yVelocity * dt)
--apply gravity
yVelocity = yVelocity + (gravity * dt)
--stop player when they hit the ground
if yPos >= yFloor - playerHeight then
yPos = yFloor - playerHeight
yVelocity = 0
playerState = "stand"
end
end
function love.draw()
--draw the player shape and colour
gfx.setColor(playerColor)
gfx.rectangle("fill", xPos, yPos, playerWidth, playerHeight)
--draw the ground and colour
gfx.setColor(groundColor)
gfx.rectangle("fill", 0, yFloor, 800, 100)
--draw the background and colour
gfx.setBackgroundColor(backgroundColor)
end
function love.keyreleased(key)
if key == "escape" then
love.event.push("q") -- quit
end
if (key == "right") or (key == "left") then
xVelocity = 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment