Here is an example of smooth movement. (use it if you want)

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
User avatar
eqnox
Prole
Posts: 44
Joined: Fri Jul 31, 2015 2:36 pm

Here is an example of smooth movement. (use it if you want)

Post by eqnox »

I know when I first started doing this I didn't really understand it so I thought I might as well help someone else who is in the same position as I was.

To change how things work just edit the values that are in the part where I defined the players attributes.

this is the main.lua

Code: Select all

require "player"

function love.load()
	player.LOAD()
end

function love.update(dt)
	player.UPDATE(dt)
end

function love.draw()
	player.DRAW()
end
this is the player.lua

Code: Select all

player = {}

--this is where we set atributes of the player
function player.load()
	player.x = 10
	player.y = 10
	player.width = 10
	player.height = 10
	player.xvel = 0
	player.yvel = 0
	player.friction = 1
	player.speed = 500
end

--this is where the player is drawn from
function player.draw()
	love.graphics.setColor(255,0,0)
	love.graphics.rectangle("fill",player.x,player.y,player.width,player.height)

end

--this is where the phusics are handled
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))
	player.yvel = player.yvel * (1 - math.min(dt*player.friction, 1))
end

--this is where the movement is handled
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("s") and
	player.yvel < player.speed then
		player.yvel = player.yvel + player.speed * dt
	end

	if love.keyboard.isDown("w") and
	player.yvel > -player.speed then
		player.yvel = player.yvel - player.speed * dt
	end
end

--functions are put here to be easily managaed in the main file
function player.LOAD()
	player.load()
end

function player.UPDATE(dt)
	player.physics(dt)
	player.move(dt)
end

function player.DRAW()
	player.draw()
end
And I will also upload the file containing all of that code and a .love file for the demo.
Attachments
move.love
w, a, s, d for movement
(761 Bytes) Downloaded 433 times
player.lua
(1.5 KiB) Downloaded 306 times
main.lua
(159 Bytes) Downloaded 245 times
TomBebbington
Prole
Posts: 2
Joined: Mon Aug 10, 2015 12:36 pm

Re: Here is an example of smooth movement. (use it if you wa

Post by TomBebbington »

Nice work, it's a great start to have movement!
User avatar
farzher
Prole
Posts: 42
Joined: Fri Jul 31, 2015 5:38 pm

Re: Here is an example of smooth movement. (use it if you wa

Post by farzher »

Code: Select all

function player.DRAW()
   player.draw()
end
Why? Why the DRAW that only calls draw?
butts
User avatar
eqnox
Prole
Posts: 44
Joined: Fri Jul 31, 2015 2:36 pm

Re: Here is an example of smooth movement. (use it if you wa

Post by eqnox »

farzher wrote:

Code: Select all

function player.DRAW()
   player.draw()
end
Why? Why the DRAW that only calls draw?
It only needs one for this program.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests