My player.lua file:
Code: Select all
player = {}
function player.load()
player.x = 5
player.y = 5
player.xvel = 0
player.yvel = 200
player.friction = 7
player.speed = 2250
player.width = 50
player.height = 50
player.jumpHeight = 200
player.startJump = 0
player.jumping = false
player.screenheight = 900
end
function player.draw()
love.graphics.setColor(0,255,0)
love.graphics.rectangle("fill",player.x,player.y,player.width,player.height)
end
function player.physics(dt)
player.x = player.x + player.xvel * dt
player.y = player.y + player.yvel * dt
if not player.jumping then
player.yvel = player.yvel + gravity * dt
else
if player.startJump - player.y >= player.jumpHeight then
player.jumping = false
player.yvel = 200
end
end
player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
end
function player.move(dt)
if love.keyboard.isDown("right") and
player.xvel < player.speed then
player.xvel = player.xvel + player.speed * dt
end
if love.keyboard.isDown("left") and
player.xvel > -player.speed then
player.xvel = player.xvel - player.speed * dt
end
if love.keyboard.isDown("up") and not player.jumping then
player.yvel = player.yvel - player.speed * dt
player.startJump = player.y
player.jumping = true
if player.y + player.height + player.width > player.screenheight then
gravity = 600
player.y = player.screenheight
end
end
end
function player.boundary()
if player.x < 0 then
player.x = 0
player.xvel = 0
end
if player.y + player.height >= groundLevel then
player.y = groundLevel - player.height
player.yvel = 0
end
end
function UPDATE_PLAYER(dt)
player.move(dt)
player.physics(dt)
player.boundary()
end
function DRAW_PLAYER()
player.draw()
end
Code: Select all
require "player"
function love.load()
love.graphics.getBackgroundColor(0,0,0)
groundLevel = 600
gravity = 900
--Loading classes
player.load()
end
function love.update(dt)
UPDATE_PLAYER(dt)
if player.yvel ~= 0 then
player.y = player.y + player.yvel * dt
player.yvel = player.yvel - gravity * dt
if player.y < 0 then
player.yvel = 0
player.y = 0
end
end
end
function love.draw()
DRAW_PLAYER()
end