Declaring Victory! (And Starting Again!)
In last week's article, we used a neat little algorithm to generate random mazes for our game. This was cool, but nothing happens yet when we "finish" the maze! We'll change that this week. We'll allow the game to continue re-generating new mazes when we're finished! You can find all the code for this part on the part-2
branch on the Github repository for this project!
If you're a beginner to Haskell, hopefully this series is helping you learn simple ways to do cool things! If you're a little overwhelmed, try reading our Liftoff Series first!
Goals
Our objectives for this part are pretty simple. We want to make it so that when we reach the "end" location, we get a "victory" message and can restart the game by pressing a key. We'll get a new maze when we do this. There are a few components to this:
- Reaching the end should change a component of our
World
. - When that component changes, we should display a message instead of the maze.
- Pressing "Enter" with the game in this state should start the game again with a new maze.
Sounds pretty simple! Let's get going!
Game Result
We'll start by adding a new type to represent the current "result" of our game. We'll add this piece of state to our World
. As an extra little piece, we'll add a random generator to our state. We'll need this when we re-make the maze:
data GameResult = GameInProgress | GameWon
deriving (Show, Eq)
data World = World
{ playerLocation :: Location
, startLocation :: Location
, endLocation :: Location
, worldBoundaries :: Maze
, worldResult :: GameResult
, worldRandomGenerator :: StdGen
}
Our generation step needs a couple small tweaks. The function itself should now return its final generator as an extra result:
generateRandomMaze :: StdGen -> (Int, Int) -> (Maze, StdGen)
generateRandomMaze gen (numRows, numColumns) =
(currentBoundaries finalState, randomGen finalState)
where
...
finalState = execState dfsSearch initialState
Then in our main
function, we incorporate the new generator and game result into our World
:
main = do
gen <- getStdGen
let (maze, gen') = generateRandomMaze gen (25, 25)
play
windowDisplay
white
20
(World (0, 0) (0, 0) (24, 24) maze GameInProgress gen')
...
Now let's fix our updating function so that it changes the game result if we hit the final location! We'll add a guard here to check for this condition and update accordingly:
updateFunc :: Float -> World -> World
updateFunc _ w
| playerLocation w == endLocation w = w { worldResult = GameWon }
| otherwise = w
We could do this in the eventHandler
but it seems more idiomatic to let the update
function handle it. If we use the event handler, we'll never see our token enter the final square. The game will jump straight to the victory screen. That would be a little odd. Here there's at least a tiny gap.
Displaying Victory!
Now our game will update properly. But we have to respond to this change by changing what the display looks like! This is a quick fix. We'll add a similar guard to our drawingFunc
:
drawingFunc :: (Float, Float) -> Float -> World -> Picture
drawingFunc (xOffset, yOffset) cellSize world
| worldResult world == GameWon =
Translate (-275) 0 $ Scale 0.12 0.25
(Text "Congratulations! You've won!\
\Press enter to restart with a new maze!")
| otherwise = ...
Note that Text
here is the Gloss Picture
constructor, not Data.Text
. We also scale and translate it a bit to make the text appear on the screen. This is all we need to get the victory screen to appear on completion!
Restarting the Game
The last step is that we have to follow through on our process to restart the game if they hit enter! This involves changing our inputHandler
to give us a brand new World
. As with our other functions, we'll add a guard to handle the GameWon
case:
inputHandler :: Event -> World -> World
inputHandler event w
| worldResult w == GameWon = …
| otherwise = case event of
...
We'll want to make a new case section that accounts for the user pressing the "Enter" key. All this section needs to do is call generateRandomMaze
and re-initialize the world!
inputHandler event w
| worldResult w == GameWon = case event of
(EventKey (SpecialKey KeyEnter) Down _ _) ->
let (newMaze, gen') = generateRandomMaze
(worldRandomGenerator w) (25, 25)
in World (0, 0) (0, 0) (24, 24) newMaze GameInProgress gen'
_ -> w
And with that, we're done! We can restart the game and navigate random mazes to our heart's content!
Conclusion
The ability to restart the game is great! But if we want to make our game re-playable instead of random, we'll need some way of storing mazes. In the next part, we'll look at some code for dumping a maze to an output format. We'll also need a way to re-load from this stored representation. This will ultimately allow us to make a true game with saving and loading state.
In preparation for that, you can read our series on Parsing. You'll especially want to acquaint yourself with the Megaparsec library. We go over this in Part 4 of the series!