Page 1 of 1

Platformer Help/Maybe a tutorial?

Posted: Wed Dec 10, 2014 1:20 am
by Juice_Box
So I've been working on a small platformer from a tutorial I found online. The guy doing the tut left the tutorial before implementing jumping, so I tried to make my own code for it. Long story short, this is screwed up and I need some help. It should be really simple, but this is my first time using Lua (or programming in general) in a while. :crazy: Here's all the code from my player.lua file and my main.lua file. Thanks! :awesome:

Code: Select all

--main.lua
require "player"


function love.load()
	
	
	love.graphics.getBackgroundColor(255, 255, 255)
	
	groundLevel = 600
	gravity = 900
	
	
	--Loading Classes
	player.load()
end
function love.update(dt)
	UPDATE_PLAYER(dt)
end
function love.draw()
	DRAW_PLAYER()
	
end

Code: Select all

--player.lua


player = {}

function player.load()
	player.x = 5
	player.y = 5
	player.xvel = 0
	player.yvel = 0
	player.friction = 7
	player.speed = 700
	player.width = 16
	player.height = 16
	player.jumpHeight = 1500
end
function player.draw()
	love.graphics.setColor(168, 72, 72)
	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
	player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
	if not(love.keyboard.isDown('w')) then
		player.yvel = player.yvel + gravity * dt
	end
end

function player.move(dt)
	if love.keyboard.isDown('d') and
		player.xvel < player.speed then
			player.xvel = player.xvel + player.speed * dt
		end
	if love.keyboard.isDown('a') and
		player.xvel > -player.speed then
			player.xvel = player.xvel - player.speed * dt
		end
	if love.keyboard.isDown('w') and
		player.yvel == 0 then
			player.yvel = -player.jumpHeight * dt
		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.physics(dt)
	player.move(dt)
	player.boundary()
end
function DRAW_PLAYER()
	player.draw()
end


Re: Platformer Help/Maybe a tutorial?

Posted: Wed Dec 10, 2014 4:19 am
by ivan
Not bad, but the names of some of the variables are a little confusing.
"player.speed" is actually "acceleration"
"player.friction" is "damping"

Re: Platformer Help/Maybe a tutorial?

Posted: Wed Dec 10, 2014 7:07 am
by micha
There was once a platformer tutorial here, but it does not work currently. Maybe it is only temporarily out of order.

Re: Platformer Help/Maybe a tutorial?

Posted: Wed Dec 10, 2014 7:48 am
by Jasoco
Look into kikito's Bump library. He has a working platforming engine as an example that I've actually taken and turned into a pretty solid SMB-style platformer.