Page 1 of 1

Pausing Game

Posted: Sat Feb 25, 2012 11:11 pm
by whopunk123
Hi

When I pause the game with to escape key I would text to come up saying "GAME PAUSED" I can do this but it only stays up for like 1 sec and I want it to stay up till the users presses the escape key again

Heres my code so far:

Code: Select all

if key == 'escape' then
      love.graphics.print("THE GAME IS PAUSED", 10, 250, 0, 2, 2)
   end
Thanks

Re: Pausing Game

Posted: Sat Feb 25, 2012 11:31 pm
by Nixola

Code: Select all

--in Keypressed
if key == 'escape' then
  somevariable = not somevariable  --toggles the variable between true and false
end

--in love.draw()
  if somevariable == true then 
    love.graphics.print('THE GAME IS PAUSED', 10, 250, whatever
  end

Re: Pausing Game

Posted: Sat Feb 25, 2012 11:36 pm
by whopunk123
Thanks works great :awesome:

Re: Pausing Game

Posted: Sat Feb 25, 2012 11:45 pm
by coffee
whopunk123 wrote:Thanks works great :awesome:
When you understand well the callback basics of LOVE and how to update and draw things all will be easier

https://love2d.org/wiki/Tutorial:Callback_Functions

Code: Select all

function love.load()
	pause = false
end

function love.update(dt)
	function love.keypressed(key, unicode)
		if key == 'p' then pause = not pause end
	end
end

function love.draw()
	if pause then love.graphics.print("Game is paused",0,0) else 
	love.graphics.print("Game is running",0,0) end
end

Re: Pausing Game

Posted: Sun Feb 26, 2012 11:54 am
by bartbes
I would strongly suggest against using an else like that, wrapping your entire function in an if statement because you want to pause seems highly invasive, it's better to just return from the if case.

Re: Pausing Game

Posted: Sun Feb 26, 2012 2:55 pm
by MarekkPie
bartbes wrote:I would strongly suggest against using an else like that, wrapping your entire function in an if statement because you want to pause seems highly invasive, it's better to just return from the if case.
AKA

Code: Select all

function love.draw()
    if pause then love.graphics.print("Game Paused", 0, 0) return end
    -- Unpaused code
end

Re: Pausing Game

Posted: Sun Feb 26, 2012 3:26 pm
by tentus
Also, this:

Code: Select all

function love.update(dt)
   function love.keypressed(key, unicode)
      if key == 'p' then pause = not pause end
   end
end
is wrong. Do it like this:

Code: Select all

function love.update(dt)
   -- do stuff
end
function love.keypressed(key, unicode)
   if key == 'p' then pause = not pause end
end