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

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
QuerkyBren
Prole
Posts: 4
Joined: Sat May 14, 2016 4:35 am

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

Post 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
LOVE2D NOOB
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

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

Post 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
QuerkyBren
Prole
Posts: 4
Joined: Sat May 14, 2016 4:35 am

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

Post 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.
LOVE2D NOOB
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

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

Post by Sheepolution »

Code: Select all

square.x = math.random(0, 700)
square.y = math.random(0, 400)
QuerkyBren
Prole
Posts: 4
Joined: Sat May 14, 2016 4:35 am

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

Post 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 :(
LOVE2D NOOB
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

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

Post by Tjakka5 »

Use love.math.random(min, max) instead; its seed automatically gets set.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

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

Post 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())

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
QuerkyBren
Prole
Posts: 4
Joined: Sat May 14, 2016 4:35 am

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

Post 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!
LOVE2D NOOB
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 6 guests