Mario style platformer physics
Posted: Sat Feb 20, 2016 5:10 pm
Hi! I'm new to LOVE and programming in general. I was able to make some basic character movements for a platformer. Jumping and X axis movement. Now I'm trying to make the character movement smoother. I've looked through YouTube and I've dug through the forums here but I still can't find an answer. I still have much to learn but I was hoping that some of you could help me figure out why I can't get my character to move smoothly. Once, I had friction working and smooth X axis movement, but my jump was disabled. Here's my code (It's sloppy I know).
Thanks in advance! I'm sorry if this has been covered somewhere before. I searched through a lot of threads before posting this.
Code: Select all
love.graphics.setDefaultFilter('nearest', 'nearest')
function love.load()
face_right = true
love.window.setMode(1280, 720, {resizable=true, vsync=false, minwidth=1280, minheight=720})
bckgrnd = love.graphics.newImage('mario_background.png')
player_image = {}
player_image[1] = love.graphics.newImage('mariostand.png')
player_image[2] = love.graphics.newImage('mariojump.png')
player_image[3] = love.graphics.newImage('mariostandL.png')
player_image[4] = love.graphics.newImage('mariojumpL.png')
--PLAYER
player = {}
player.x = 20
player.y = 480
player.speed = 0.4
player.yvel = 0
player.xvel = 0
player.float = 0.5
player.float_max = 0.5
player.friction = 1
gravity = 800
jump_height = 500
winH, winW = love.graphics.getHeight(), love.graphics.getWidth()
enemy = {}
enemy.image = love.graphics.newImage('enemy.png')
enemy.x = 1150
enemy.y = 480
enemy.speed = 0.2
end
function love.draw()
love.graphics.draw(bckgrnd, 0, 0, 0, 10)
if (player.yvel ~= 0 and face_right == true) then
love.graphics.draw(player_image[2], player.x, player.y, 0, 5)
elseif (player.yvel == 0 and face_right == true) then
love.graphics.draw(player_image[1], player.x, player.y, 0, 5)
elseif (player.yvel ~= 0 and face_right == false) then
love.graphics.draw(player_image[4], player.x, player.y, 0, 5)
elseif (player.yvel == 0 and face_right == false) then
love.graphics.draw(player_image[3], player.x, player.y, 0, 5)
end
love.graphics.draw(enemy.image, enemy.x, enemy.y, 0 ,5)
end
function love.update(dt)
--player movement
if love.keyboard.isDown("right") then
face_right = true
player.x = player.x + player.speed
if player.x >= 1200 then
player.x = 1200
end
end
if love.keyboard.isDown("left") then
face_right = false
player.x = player.x - player.speed
if player.x <= 0 then
player.x = 0
end
end
if love.keyboard.isDown("r") then
player.speed = 0.8
else
player.speed = 0.4
end
if player.yvel ~= 0 then -- in air
player.y = player.y - player.yvel * dt
player.yvel = player.yvel - gravity * dt
end
if player.y > 480 then -- on ground
player.yvel = 0
player.y = 480
end
end
function love.keypressed(key, scancode, isrepeat) -- jump
if key == "space" then
if player.yvel == 0 then
player.yvel = jump_height
end
end
end