Mario style platformer physics

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
cfincher29
Prole
Posts: 3
Joined: Sat Feb 20, 2016 4:27 pm

Mario style platformer physics

Post by cfincher29 »

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).

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
Thanks in advance! I'm sorry if this has been covered somewhere before. I searched through a lot of threads before posting this.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Mario style platformer physics

Post by Davidobot »

It would help if you posted a .love file.
Also, right off the bat, I see you're not applying dt to your X movement, try that:

Code: Select all

 player.x = player.x + player.speed *dt
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
cfincher29
Prole
Posts: 3
Joined: Sat Feb 20, 2016 4:27 pm

Re: Mario style platformer physics

Post by cfincher29 »

Davidobot wrote:It would help if you posted a .love file.
Also, right off the bat, I see you're not applying dt to your X movement, try that:

Code: Select all

 player.x = player.x + player.speed *dt
I could've sworn I uploaded the file at the bottom of the post. Sorry if this is spammy, I've never actually used forums other than browsing through them from search engines.
Attachments
gangstermarioTEST.love
(6.45 MiB) Downloaded 142 times
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: Mario style platformer physics

Post by monolifed »

Have you seen Mari0? It is open source (though the non-commercial-use-only term makes it a little less useful)
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Mario style platformer physics

Post by pgimeno »

Your speed is too low.
cfincher29
Prole
Posts: 3
Joined: Sat Feb 20, 2016 4:27 pm

Re: Mario style platformer physics

Post by cfincher29 »

ingsoc451 wrote:Have you seen Mari0? It is open source (though the non-commercial-use-only term makes it a little less useful)
I actually heard about that but I haven't ever tried it. I need too though. Mario and Portal are some of my favorite games.
pgimeno wrote:Your speed is too low
Thank you! I wouldn't have thought to change that.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest