Balloon Pop! — Defold Game Tutorial

Project Summary:  

I’ve been really looking forward to putting this game tutorial together.   My grandchildren just love to play these simple games where you see how many balloons you can pop before too many of those balloons escape.   The catch, of course, is that the balloons start to come faster and faster, the more you pop.   So…I thought this would be a great introductory game to highlight use of the Defold engine within a ‘real’ game (well, close to a real game).   I’ll be demonstrating the use of several key features, to include: (a) use of multiple Atlases, (b) use of several Lua Modules, such as timer, hash_utils and animationFunctions, (c) many lessons learned in using multiple collections to represent the levels of the game and (d) multiple Graphic User Interfaces (GUI) supporting main menu, level controls, information panel and exit menu.  I hope you enjoy the tutorial.

 

Game Overview:  

Balloon Pop! is a fun game – primarily targeted at younger children.   The game objective is pretty simple — try to ‘pop’ as many moving balloons as you can before three of them escape off the screen.   The balloons are spawned at random times, with random locations to make it a little more challenging.   Additionally, as you increase from Level 1 to Level2 – the speeds and inter-spawning times get quicker.  My goal was to introduce a relatively simple game that leverages the basic components we introduced in the Enhanced Defold Game Template (prior tutorial).    You’ll find the following elements closely parallel the work accomplished in the Enhanced Template Tutorial:

  • Incorporates Animated Logos and Buttons – using the same animateFunctions Lua Module
  • Uses the basic Main-Menu Structure – including re-skinned GUI Nodes and gui_scripts
  • Custom Rendering and Custom Font techniques are also similar to those conducted within the template.

 

Here’s what the final Balloon Pop Game looks like:

 

The following table summarizes the key features which have been integrated into this Defold Game.

Key Game Design Features

  • RANDOMIZATION FEATURES – Balloon Game Objects invoke randomly selected sprite images and are spawned at random x-positions across the bottom of the screen; spawn times and traverse speeds are also randomly invoked.
  • CURSOR BASED COLLISIONS – A cursor Game Object (unique to each level) is used to trigger user clicks on a Balloon Game Object.
  • SCORE KEEPING – The project includes flag variables which display how many balloons have escaped, and how many have been popped.
  • USE OF LUA MODULES FOR UTILITY CODE – This project incorporates three common Lua Modules: (a) animationFunctions, supporting button, popup and GUI node animations, (b) hash_utils, part of Defold’s DefTable assets, providing manipulation of hash-like tables, and (c) timer, pulled from Britzl’s GitHubGist, which is used to get a callback when a certain amount of time has elapsed.
  • GAME LEVEL MENU – The project presents the user with a main menu, enabling the selection of a specific game level, or exiting from the game.  Game Levels increase in difficulty, due to increased speed and spawning frequency.
  • MULTIPLE CROSS-COLLECTION GUIs – Several GUI components are included (main_menu, level_controls, info_panel and exit_menu).
  • BALLOON POP! USES MANY ENHANCED TEMPLATE FEATURES – The game includes many of the key features highlighted in the previous tutorial (Enhanced Defold Game Template), to include: (a) a Popup Help Panel, (b) animated splashscreen logos, (c) a custom render script, and (d) custom fonts.   Additionally, a canvas resizing function has been added to the index.html file created during the Defold HTML5 Build process — to enable dynamic resizing of the game world when embedded within an iFrame.

 

To keep the length of this tutorial manageable, I’ll be describing the project design and build process across the following steps:

  • High-Level Design Description
  • Project Setup – Highlights
  • Utility Lua Modules
  • Collection Levels/Proxies
  • GUI Structure
  • Script Walkthroughs
  • Final Result

I hope you find this tutorial helpful as you become increasingly familiar with the features and strengths of the Defold Game Engine.

High-Level Design Description:  

For this Balloon Pop! game, I’ll be using a design structure very similar to our Enhanced Template — though, there are some differences which I’ll highlight as we hit them.  The project hierarchy is designed to conform with three distinct collections:

  • main.collection — this level is invoked immediately upon starting the game — it displays both a Studio and Game Logo as SplashScreens, then presents the game main menu.  This collection also includes the components for each of the Graphic User Interfaces (GUI).
  • level1.collection — this level includes all the game components needed to play the initial level of the game.
  • level2.collection — this level includes all the game commponents needed to play the second level of the game.

For a real game, we’d obviously want to have more then simply two levels – but, I thought this would suffice for a simple game tutorial.

Please note, that I’ve tried to minimize the use of Global Variables — relying heavily on inter-collection/inter-component messaging for the exchange of parameter related information.  This approach emphasizes unit integrity within each of the collections, aka, the project is designed in increase cohesion and focuses on loose coupling between scripts/levels.

The project hierarchy is provided in the following graphic which delineates which game components belong to each of the three collections.

 



 

The following tables provide a listing of the Lua Modules, Scripts and Gui_Scripts included within this project.

 



 

Defold Project Setup – Highlights:  

Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Include:

  • Images/Atlases
  • Input Bindings
  • Custom Fonts
  • Custom Render Script
  • Other Topics

Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit.

Collection Levels/Proxies:  

Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Include:

  • User of Collection Property Name
  • Collisions only within the same Collection

Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit.

GUI Structure:  

Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Include:

  • main_menu
  • level_controls
  • info_panel
  • exit_menu

Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit.

Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum.

Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Utility Lua Modules:  

For this Balloon Pop! project, I’ve included three Lua Modules – facilitating reuse of these functions across other projects.  The three Lua Modules are:

  • timer
  • hash_utils
  • animationFunctions

The source code for each of these is provided below.  Please note that I’ve set a 500 pixel limit on the display window for each of these, so you’ll need to scroll them to see the full extent of lua code.

Source Code for animationFunctions.lua

 

Source Code for hash_utils.lua

 

Source Code for timer.lua

 

Script Walkthroughs:  

Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Include:

  • User of Collection Property Name
  • Collisions only within the same Collection

Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit.

Final Result:  

Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Include:

  • User of Collection Property Name
  • Collisions only within the same Collection

Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit.

Following WPBakery Builder Insert Formats

I am message box. Click edit button to change this text.

Key Step 1

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Substep A

Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.  Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.  Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.  Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.  Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

I am message box. Click edit button to change this text.

xxxxxxx:  

This Game Project is focused on enhancing the basic Game Template which was introduced in my initial tutorial (Defold Kickoff).  This revised template includes stubs for each of the major game components you’d expect within a Mobile Game Application – to include: (a) Animated Designer and Game Logos, (b) Animated Graphic User Interface with a set of Level Buttons, (c) Separate Defold Collections for each game level, (d) an Information/Help Panel to provide game instructions, (e) a customized Rendering Script which enables the retrieval of window size specifications, and (f) the use of Lua Modules to support common functions which apply to all game levels.

<div>Icons made by <a href=”http://www.freepik.com” title=”Freepik”>Freepik</a> from <a href=”https://www.flaticon.com/” title=”Flaticon”>www.flaticon.com</a> is licensed by <a href=”http://creativecommons.org/licenses/by/3.0/” title=”Creative Commons BY 3.0″ target=”_blank”>CC 3.0 BY</a></div>

 

Project Design and Build Sequence:  

This Game Project is focused on enhancing the basic Game Template which was introduced in my initial tutorial (Defold Kickoff).

  • Overview of the Game Mechanics
  • Graphic of Architecture (General Design Pattern)

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborumluctus nec ullamcorper mattis, pulvinar dapibus leo.

I am message box. Click edit button to change this text.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.I am message box. Click edit button to change this text.

 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Hey! I am first heading line of Action Box

This is the SubHeading

I am promo text. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Separator with Text

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.