E3 – Peg Puzzle – Defold Game Tutorial – Build III

Project Summary: 

For this final tutorial in the Peg Puzzle series, we will now extend the level play from the previous tutorial in Build II into a multi-level game which will highlight the extensibility of our Defold game design developed over the past two tutorials, demonstrate the ease with which we can extend the logic within our scripts to accommodate different geometries across these levels – and in the process, develop a game which poses some real challenges to a player.  This final tutorial highlights a straight-forward process for readily transitioning from a single-level to a multi-level game.

SECTION I: INTRODUCTION

Peg Puzzle – Build III Objectives: 

Our next iteration in building a Peg Puzzle game will be to extend the functions and variable structures within the previous Build II to support multiple game levels, across very different geometries.  The following illustration shows the configuration of the pegs and pegHoles for each of the four levels in this final build.

level_patterns

Several quick observations about the peg geometries that will comprise our game levels:

  • We will leverage the same set of Game Objects to produce each of these levels (tabletop, peg and peghole)
  • There are different numbers of pegHole and peg game objects to be spawned, and they are in different locations within each level
  • The total number of pegHoles and the number of potential hop paths differ between each of these levels

That’s a good starting point for kicking off this description of our approach to expand the variables we’ve used for the number of PegHoles, locations, and hop paths as discussed in Tutorial 2 — to quickly convert to this multi-level design.

Build III Project Files: 

Here’s the full project for Build III within a zipfile.  As I recommend in each of our tutorials, go ahead and download/extract the files into a work folder, then open it up using the Defold Editor (v2) in order to follow along with the walk-through of the structure of this initial build.  

SECTION II: BUILD III DESIGN HIGHLIGHTS

Peg Puzzle – Build III – Major Design Highlights

  • MULTI-DIMENSIONAL LUA TABLES SUPPORTING GAME OBJECT LOCATIONS AND POTENTIAL HOP PATHS, BY LEVEL – Working from the one-dimensional tables used for a single game level – this build expands those tables to 2 dimensions to accommodate using ‘level’ as a key index.
  • SUCCESS BASED TRANSITIONS TO HIGHER LEVELS– The game is now designed to constrain access to higher (more challenging) game levels – dependent upon successful completion of a previous level.

Build III is a close derivative of our Build II project.  All changes to support expansion into a multi-level Peg Puzzle Defold game are going to be constrained to the controller.script and mainMenu.gui_script files.  Importantly, the main.collection structure and mainMenu structures remain the same, the graphicAssets and textAssets atlases remain the same and the render, font and module components also remain the same.   The next section provides a detailed comparison of the new Build III source code as compared with the Build II (single game level) code.

 

Section III: BUILD III SCRIPT COMPONENT FILES

Peg Puzzle — Build II:  Script Enhancements: 

controller.script

The revised controller.script for this final build of the Peg Puzzle Defold Game performs several important primary functions:

  • It sets GLOBAL variables for (1) maximum number of levels, (2) maximum number of locations and (3) maximum number of hop paths
  • It defines a new local variable for the location of the empty pegHole at the beginning of play for each of the four levels
  • The variable phXCoord is expanded to two-dimensions: [level] and [location]; similarly, phYCoord is expanded as well.  Geometries for each of the levels are populated into these Lua Tables.
  • Similarly, startingPos, middlePos and endingPos (which define the potential hop paths that a peg can take) is expanded into two-dimensional Lua Tables as well.
  • All For/Next loops that iterate through these tables have been modified to accommodate the level index reference

The following file comparison highlights each of the line changes made.   PS:  I’m using the online service DiffNow to conduct these script file comparisons – it’s a pretty handy free utility.

Build III                                                                              Build II

 

mainMenu.gui_script

The revised mainMenu.gui_script for this final build of the Peg Puzzle Defold Game performs several important primary functions:

  • A new local variable (remainingPegsToPass) is declared – which defines how many pegs must be remaining on the board to consider the player to have passed that level.  Note – these can be adjusted to either make play easier, or harder.
  • All For/Next loops that iterate through each of the levels have been modified to reflect the global variable NUM_LEVELS
  • Logic has been included to determine if a player has passed a level – aka, is now able to play the next level in the game

The following file comparison highlights each of the line changes made.

Build III                                                                              Build II

That’s it – really not that many modifications required – the most difficult aspect was probably working through each of the x/y coordinates for the peg holes in each of the level scenarios and also determining each of the possible hop paths that would need to be evaluated.

SECTION VI: WRAP-UP

Completion of the Peg Puzzle Game Tutorial – Build III:

Whew – we’ve covered a lot of ground, but hopefully, the structure of the Defold game project and the three step approach made sense.   The Peg Puzzle isn’t quite a market-ready product – but it gets close.

I hope the abbreviated descriptions during this three-part tutorial made it easier to digest all the material.  If you think I should add more detail related to any piece of this tutorial – or if you have a question on how it works — please drop me a note in the comment box below.  I’ll get right back to you.   Also – please let me know if you find these tutorials helpful.  I look forward to your feedback.

Here are the hyperlinks to each of the Peg Puzzle Tutorials:

E1 – The Opening SplashScreens and Level Selection Panel (Build I)

E2 – A Full Game focused on a Single Level – a 10 Location Triangle (Build II)

E3 – The final four-level Peg Puzzle Game (Build III)


All the best – Dave