Restart a game
Posted: Tue Aug 19, 2014 3:54 pm
Hi there.
I am new to LÖVE and have been working on a rudimentary Pong implementation.
First I load the game elements (ball, paddles, scoreboard, etc.)
By default, I have a boolean GAME_ACTIVE which is set to true.
While true, the love.draw and love.update in my main.lua draw and update the game elements as applicable.
In my love.keypressed(key) function, I have code to pause/unpause game play when GAME_ACTIVE is true and
code to quit or restart the game when GAME_ACTIVE is false.
Quit works okay with love.event.quit(), but resetting the GAME_ACTIVE to true, does not restart the game. What can I do to reset the game? Restart the engine itself or can I dispose of the game element objects and reinitialize them?
Thanks in advance.
- Sam
I am new to LÖVE and have been working on a rudimentary Pong implementation.
First I load the game elements (ball, paddles, scoreboard, etc.)
Code: Select all
function love.load()
ball = Ball.create(SCREEN_WIDTH / 2 , 250, SCREEN_WIDTH, SCREEN_HEIGHT)
paddle1 = Paddle.create(1, 15, SCREEN_HEIGHT, 'w', 's')
paddle2 = Paddle.create(2, (SCREEN_WIDTH - 35), SCREEN_HEIGHT, 'up', 'down')
scoreboard = Scoreboard.create(((SCREEN_WIDTH / 2) - 20), 25)
announcer = Announcer.create(((SCREEN_WIDTH / 2) - 45), 130)
screentip = Screentip.create(((SCREEN_WIDTH / 2) - 30), 200)
end
While true, the love.draw and love.update in my main.lua draw and update the game elements as applicable.
Code: Select all
function love.draw()
if GAME_ACTIVE then
paddle1:draw()
paddle2:draw()
ball:draw()
scoreboard:draw(SCORE_PLAYER_1, SCORE_PLAYER_2)
end
end
code to quit or restart the game when GAME_ACTIVE is false.
Code: Select all
function love.keypressed(key)
if GAME_ACTIVE and key == 'p' then
if STATE == 'play' then
STATE = 'pause'
else
STATE = 'play'
end
elseif GAME_ACTIVE == false and key == 'n' then
love.event.quit()
elseif GAME_ACTIVE == false and key == 'y' then
GAME_ACTIVE = true
STATE = 'play'
--! TODO
end
end
Thanks in advance.
- Sam