I wanna ask how to stop the game from running in the background while in another gamestate.
I'm still coding my first project - an endless runner (see
Post1 and
Post2). I've only been able to recode again around this time because first semester ended.
Anyways, here's the scenario:
My game has self-coded gamestates. I'm not using any libraries. So I play the game, collision when true, and now I'm on the game over screen. So far so good. However, whenever I try to restart a new game, I don't start a new "game". What I mean by this is that, whenever I press "R" to restart the game, instead of resetting everything as you'd expect it, the game still plays during the game over state. See the gif below.
As you can see, you don't immediately restart a new game and they aren't resetting. The same thing happens when you reach the game over screen, press escape to return to main menu, and then press enter to start a new game.
To be honest, I kind of expected this to happen since I only change gamestates whenever the appropriate key is pressed.
I was wondering how to do that?
My first idea of the implementation was to turn some functions to nil when the state is gameover (or another way I thought of doing this was making the functions nil when collision happened). Something along the lines of this:
Code: Select all
if gamestate == gameover then
function = nil
if key was pressed then
gamestate = game
end
end
However, the problem would the be how to reset everything because if I don't, the functions would still be considered nil and it would return a value nil error.
I would like some help with this. If it's okay, I would prefer not using a library like HUMP for this one. As stated earlier, this is still my very first project, and I personally would like to code everything from scratch for experience and learning.
Here's the full code of what I consider are the concerned files. I have removed the turning of functions to nil on this one.
main.lua
Code: Select all
-- main.lua
require 'menu'
require 'ground'
require 'player'
require 'obstacle'
require 'score'
require 'gameover'
function love.load()
-- Sets the current gamestate to menu.
gamestate = 'menu'
-- Loads all needed assets and variables.
menu_load()
ground_load()
player_load()
obstacle_load()
score_load()
gameover_load()
end
function love.update(dt)
-- If the gamestate is the game, we then go to the game.
if gamestate == 'game' then
ground_update()
player_update(dt)
obstacle_update(dt)
score_update()
end
-- If the gamestate is gameover, we then go to the gameover.
if gamestate == 'gameover' then
gameover_update()
end
end
function love.draw()
-- If gamestate is menu, we go to the menu.
if gamestate == 'menu' then
menu_draw()
end
-- If the gamestate is tgame, we go to the game.
if gamestate == 'game' then
ground_draw()
player_draw()
obstacle_draw()
score_draw()
end
-- If the gamestate is gameover, we go to the gameover screen.
if gamestate == 'gameover' then
gameover_draw()
end
end
function love.keypressed(key)
-- Controls for Menu State.
if gamestate == 'menu' then
-- For starting a new game.
if key == 'return' then
gamestate = 'game'
-- For quitting game.
elseif key == 'escape'then
love.event.quit()
end
end
-- Controls for Game State.
if gamestate == 'game' then
-- For jumping.
if key == 'space' and player.isJumping == 'false' then
player.isJumping = 'true'
if player.y_velocity == 0 then
player.y_velocity = player.jump_height
end
end
-- For dashing.
if player.canDash <= 0 then
if key == 'z' and player.isJumping == 'true' then
player.canDash = player.maxCanDash
player.isDashing = 'true'
player.y_velocity = 0
end
end
-- For dash canceling.
if player.isDashing == 'true' and player.isJumping == 'true' then
if key == 'x' then
player.isDashing = 'false'
player.isDashCanceling = 'true'
player.airTime = 0
end
end
end
-- Controls for Gameover State.
if gamestate == 'gameover' then
-- For restarting.
if key == 'r' then
gamestate = 'game'
end
-- For quitting to menu.
if key == 'escape' then
gamestate = 'menu'
end
end
end
gameover.lua (For the meantime, please disregard the empty functions. Thanks.)
Code: Select all
--gameover.lua
function gameover_load()
end
function gameover_update()
end
function gameover_draw()
love.graphics.print('Game Over!', 0, 0)
love.graphics.print('Your final score is: ' .. score, 0, 20)
love.graphics.print('Press [R] to Restart.', 0, 40)
love.graphics.print('Press [Escape] to Go Back to Menu', 0, 60)
end
I'll also add the .love file for those who want to try it.
Press the associated buttons to navigate the menu. (Not the final way of dealing with the menu).
Space - to jump
Z - to dash
X - to dash cancel
Thanks in advance everyone