Restart a game

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
User avatar
uberSamji
Prole
Posts: 3
Joined: Tue Aug 19, 2014 3:33 pm

Restart a game

Post by uberSamji »

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.)

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
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.

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
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.

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
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
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Restart a game

Post by Plu »

Quickest way to reload the game would be to call love.load() again, since that's what you use to set the game's initial state :)
User avatar
uberSamji
Prole
Posts: 3
Joined: Tue Aug 19, 2014 3:33 pm

Re: Restart a game

Post by uberSamji »

Thanks! The following did the trick.

Code: Select all

	elseif GAME_ACTIVE == false and key == 'y' then
		GAME_ACTIVE = true
		STATE = 'play'
		SCORE_PLAYER_1 = 0
		SCORE_PLAYER_2 = 0
		love.load()
	end
Post Reply

Who is online

Users browsing this forum: No registered users and 222 guests