Page 1 of 1

How do I make my player move repeatedly after I press an arrow key?

Posted: Sat May 14, 2016 4:43 am
by QuerkyBren
Hi, I'm new to the fourms.

I'm getting started In LOVE2D and I want to make my player to keep moving after I press an arrow key (Like Snake but with only one square) Instead of It stop moving after I let go of the arrow key.

This Is the whole source btw:

Code: Select all

function love.load()
hero = {}
hero.x = 0 -- the position of the player
hero.y = 0
hero.speed = 200 --  the speed of the player
l = 0 -- to detect what key the player last pressed/ left
r = 0 -- right
u = 0 -- up
d = 0 -- down
end

function love.update(dt)
     -- controls
if love.keyboard.isDown("left") then
  hero.x = hero.x - hero.speed*dt
	 l = 1
	 r = 0
	 u = 0
	 d = 0
elseif love.keyboard.isDown("right") then
     hero.x = hero.x + hero.speed*dt
	 l = 0
	 r = 1
	 u = 0
	 d = 0
elseif love.keyboard.isDown("up") then
     hero.y = hero.y - hero.speed*dt
	 l = 0
	 r = 0
	 u = 1
	 d = 0
elseif love.keyboard.isDown("down") then
     hero.y = hero.y + hero.speed*dt
	 l = 0
	 r = 0
	 u = 0
	 d = 1
end 
end
 


function love.draw()
	 -- hero/player
	 love.graphics.setColor(175, 25, 134, 255) -- sets colour of player
     love.graphics.rectangle("fill", hero.x, hero.y, 55, 55) -- makes player drawn/visible
	 -- debug en la gamo/ debug in the game
	 love.graphics.setColor(0,0,0,255)
	 love.graphics.newFont(18)
	 love.graphics.setNewFont(18)
     love.graphics.print(hero.x, 1100, 700)
	 love.graphics.print(hero.y, 1100, 650)
	 love.graphics.print("x", 1080, 700)
	 love.graphics.print("y", 1080, 650)
	 love.graphics.print(l, 1100, 450)
	 love.graphics.print("l", 1080, 450)
	 love.graphics.print(r, 1100, 500)
	 love.graphics.print("r", 1080, 500)
	 love.graphics.print(u, 1100, 550)
	 love.graphics.print("u", 1080, 550)
	 love.graphics.print(d, 1100, 600)
	 love.graphics.print("d", 1080, 600)
	 
	 -- background
	 love.graphics.setBackgroundColor(50,200,0) -- so It dosn't look so creepy black
end

Re: How do I make my player move repeatedly after I press an arrow key?

Posted: Sat May 14, 2016 5:42 am
by Tjakka5
You're on the right path; something like this should do.

Code: Select all

lastKey = ""
movement = {
   w = {x= 0, y=-1},
   a = {x=-1, y= 0},
   s = {x= 0, y= 1},
   d = {x= 1, y= 0},
}

player = {
   x = 0,
   y = 0,
   speed = 200,
}

function love.update(dt)
   if movement[lastKey] then
      local dir = movement[lastKey]
      player.x = player.x + dir.x * player.speed * dt
      player.y = player.y + dir.y * player.speed * dt
   end
end

function love.keypressed(key)
   lastKey = key
end

Re: How do I make my player move repeatedly after I press an arrow key?

Posted: Sat May 14, 2016 7:21 am
by QuerkyBren
Tjakka5 wrote:You're on the right path; something like this should do.

Code: Select all

lastKey = ""
movement = {
   w = {x= 0, y=-1},
   a = {x=-1, y= 0},
   s = {x= 0, y= 1},
   d = {x= 1, y= 0},
}

player = {
   x = 0,
   y = 0,
   speed = 200,
}

function love.update(dt)
   if movement[lastKey] then
      local dir = movement[lastKey]
      player.x = player.x + dir.x * player.speed * dt
      player.y = player.y + dir.y * player.speed * dt
   end
end

function love.keypressed(key)
   lastKey = key
end
Thanks a bunch! Also, would you know how to make a square be drawn In a random area on the screen everytime you open the game? Because when I do It Its always drawn In the same area.

Re: How do I make my player move repeatedly after I press an arrow key?

Posted: Sat May 14, 2016 7:49 am
by Sheepolution

Code: Select all

square.x = math.random(0, 700)
square.y = math.random(0, 400)

Re: How do I make my player move repeatedly after I press an arrow key?

Posted: Sat May 14, 2016 8:11 am
by QuerkyBren
Sheepolution wrote:

Code: Select all

square.x = math.random(0, 700)
square.y = math.random(0, 400)
Its appearing In the same spot :(

Re: How do I make my player move repeatedly after I press an arrow key?

Posted: Sat May 14, 2016 8:20 am
by Tjakka5
Use love.math.random(min, max) instead; its seed automatically gets set.

Re: How do I make my player move repeatedly after I press an arrow key?

Posted: Sat May 14, 2016 1:02 pm
by Beelz
The issue is that random really isn't that random... More of a pseudo-random. Try setting a seed:

Code: Select all

--in love.load():
love.math.setRandomSeed(os.time())

Re: How do I make my player move repeatedly after I press an arrow key?

Posted: Sat May 14, 2016 6:01 pm
by pgimeno
Beelz wrote:The issue is that random really isn't that random... More of a pseudo-random. Try setting a seed:

Code: Select all

--in love.load():
love.math.setRandomSeed(os.time())
LÖVE already does that for you, actually. The issue is that QuerkyBren was using math.random instead of [wiki]love.math.random[/wiki]. LÖVE only initializes the latter.

Re: How do I make my player move repeatedly after I press an arrow key?

Posted: Sun May 15, 2016 3:18 am
by QuerkyBren
pgimeno wrote:
Beelz wrote:The issue is that random really isn't that random... More of a pseudo-random. Try setting a seed:

Code: Select all

--in love.load():
love.math.setRandomSeed(os.time())
LÖVE already does that for you, actually. The issue is that QuerkyBren was using math.random instead of [wiki]love.math.random[/wiki]. LÖVE only initializes the latter.
love.math.random worked! Thanks!