Platformer Help/Maybe a tutorial?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Juice_Box
Prole
Posts: 5
Joined: Fri Oct 31, 2014 12:06 am

Platformer Help/Maybe a tutorial?

Post 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

User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Platformer Help/Maybe a tutorial?

Post 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"
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Platformer Help/Maybe a tutorial?

Post by micha »

There was once a platformer tutorial here, but it does not work currently. Maybe it is only temporarily out of order.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Platformer Help/Maybe a tutorial?

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 8 guests