I would like help with my basic platforming stuff

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Liquid Tungsten
Prole
Posts: 6
Joined: Mon Apr 14, 2014 2:43 am
Location: Virginia,USA

I would like help with my basic platforming stuff

Post by Liquid Tungsten »

Hi everybody-
I am new to this forum.
I am also a love2d/game programming newb, so I want to ask the community for a little help.
I decided to try my hand at a basic platforming game, so I wrote some the following code:
player.lua

Code: Select all

function module.create()
 return {x=750,y=590,velocity=200,velocity2=300,isjumping=false}

end

function module.moveleft(x,dt,movespeed)
 return x - 600*dt
end

function module.moveright(x,dt,movespeed)
 return x + 600*dt
end

function module.jump(y,velocity,velocity2,isjumping,dt)
 if isjumping == true then
  y = y - velocity
  velocity = velocity - dt*600 
 end
 if velocity == 0 and y < 590 then
  y = y + velocity2
  isjumping = false
  end
 if y >= 590 then
  velocity = 20
  isjumping = false
 end
end

function module.draw(x,y)
 love.graphics.rectangle("fill",x,y,10,10)
end

return module
and
main.lua

Code: Select all

local playerfunc=require 'player'
function love.load()
 player={x=750,y=590,velocity=200,velocity2=300,isjumping=false}
end

function love.update(dt)
--movement left and right 
if love.keyboard.isDown("left") then
  player.x = playerfunc.moveleft(player.x,dt,speed)
  end
 if love.keyboard.isDown("right") then
  player.x = playerfunc.moveright(player.x,dt,speed)
  end
playerfunc.jump(player.y,player.velocity,player.velocity2,player.isjumping,dt)
function love.keypressed(key)
--if space was pressed start jumping 
 if key == " " then
  player.isjumping = true
  print("it's true")
  print(isjumping)
 end
end
function love.draw()
 --our hero
 --love.graphics.rectangle("fill",player.x,player.y,10,10)
 playerfunc.draw(player.x,player.y)
end

could any of you give me advice to improve my code?
thanks if you help
thanks if you don't help
thanks if you insult me saying "noob"
i'm soory for swamping you guys in code
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: I would like help with my basic platforming stuff

Post by Robin »

Your playerfunc.moveleft and playerfunc.moveright functions should only need 2 arguments: player and dt, and it doesn't need to return anything. You'd get something like this:

Code: Select all

function module.moveleft(player,dt)
 player.x = player.x - 600*dt
end
And that 600 might be better as a property of player as well, so you can easily change the player speed if needed.

playerfunc.jump doesn't work. You assign to local variables only, and don't return anything. Just pass it 2 arguments as well: player and dt.

Code: Select all

function module.jump(player,dt)
 if player.isjumping then -- no need for == true
  player.y = player.y - player.velocity
  player.velocity = player.velocity - dt*600
 end
 if player.velocity == 0 and player.y < 590 then
  player.y = player.y + player.velocity2
  player.isjumping = false
  end
 if player.y >= 590 then
  player.velocity = 20
  player.isjumping = false
 end
end
Also, like it is now, that second if will pretty much never be true, because it only works if the vertical velocity is exactly zero. You probably want to use something like:

Code: Select all

 if math.abs(player.velocity) < 5 and player.y < 590 then
instead.
Help us help you: attach a .love.
Liquid Tungsten
Prole
Posts: 6
Joined: Mon Apr 14, 2014 2:43 am
Location: Virginia,USA

Re: I would like help with my basic platforming stuff

Post by Liquid Tungsten »

Thanks a lot, Robin. Your help is greatly appreciated, even if I don't quite understand it yet.
TheScriptan
Citizen
Posts: 56
Joined: Wed Feb 27, 2013 7:53 pm

Re: I would like help with my basic platforming stuff

Post by TheScriptan »

OFF-TOPIC: What game do you play? Starcraft? Because you have a prefix "Liquid"
Liquid Tungsten
Prole
Posts: 6
Joined: Mon Apr 14, 2014 2:43 am
Location: Virginia,USA

Re: I would like help with my basic platforming stuff

Post by Liquid Tungsten »

Nah, I mostly play TF2, and some other assorted indie games. I just named myself Liquid Tungsten because liquid tungsten melts at roughly around 3,400 degrees Celsius, and I thought that was a cool fact.
Liquid Tungsten
Prole
Posts: 6
Joined: Mon Apr 14, 2014 2:43 am
Location: Virginia,USA

Re: I would like help with my basic platforming stuff

Post by Liquid Tungsten »

Another off-topic: What's with this whole "Obey" thing? I don't really get it.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: I would like help with my basic platforming stuff

Post by substitute541 »

Liquid Tungsten wrote:Another off-topic: What's with this whole "Obey" thing? I don't really get it.
viewtopic.php?f=3&t=9
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Liquid Tungsten
Prole
Posts: 6
Joined: Mon Apr 14, 2014 2:43 am
Location: Virginia,USA

Re: I would like help with my basic platforming stuff

Post by Liquid Tungsten »

So uh
I messed around with my code some more.
The problem now is I don't really know how to detect when my player has hit the ground.
so
Here is my updated code.
main.lua

Code: Select all

local playerfunc=require 'player'
function love.load()
 player={x=750,y=591,upVelocity=600,isJumping=false,PlayerSpeed=600}
end

function love.update(dt)
--movement left and right 
if love.keyboard.isDown("left") then
  player.x = playerfunc.MoveLeft(player,dt)
  end
 if love.keyboard.isDown("right") then
  player.x = playerfunc.MoveRight(player,dt)
  end
playerfunc.Jump(player,dt)
end
function love.keypressed(key)
--if space was pressed start jumping 
 if key == " " then
  player.isJumping = true
 end
end
function love.draw()
 --our hero
 love.graphics.rectangle("fill",player.x,player.y,10,10)
end
and
player.lua

Code: Select all

player={}
function player.create()
 return {x=750,y=590,upVelocity=200,downVelocity=300,isJumping=false,PlayerSpeed=600}
end

function player.MoveLeft(player,dt)
 return player.x - dt * player.PlayerSpeed
end

function player.MoveRight(player,dt)
 return player.x + dt * player.PlayerSpeed
end

function player.Jump(player,dt)
 if player.isJumping then
  --start jumping
  player.y = player.y - player.upVelocity * (dt/2)
  player.upVelocity = player.upVelocity - (dt/2)*900
  print(player.y)
  print(player.upVelocity)
end
-- if math.abs(player.upVelocity) < 5 and player.y < 590 then
--  player.upVelocity = player.upVelocity + player.upVelocity*(dt/2)
--  player.y = player.y + (dt/2)*player.upVelocity
-- end
 if math.abs(player.y-590) < 1 and player.upVelocity <= 0 and math.abs(player.y-590) > 0 then
  player.upVelocity = 20
  player.isJumping = false
  player.y = 590
  print("executed")
 end
 if player.y == 590 then
  player.y = 590
 end
end

return player
ok
thanks in advance to anyone who might help me
Liquid Tungsten
Prole
Posts: 6
Joined: Mon Apr 14, 2014 2:43 am
Location: Virginia,USA

Re: I would like help with my basic platforming stuff

Post by Liquid Tungsten »

bump
Post Reply

Who is online

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