02 February 2016
Corona Cannon – a new sample game
A long time ago, a demo was made for Corona SDK by Jonathan and Biffy Beebe called Ghosts vs. Monsters (GvM). It was a simple clone of a very popular game of that time – Angry Birds.
Updating the game for today
Five years have passed since then. The original GvM was developed for Graphics v1, it only supported 480×320 or 960×640 resolution screens, and the code didn’t follow current best practices.
For instance, there were many global variables, the project didn’t use OOP (Object Oriented Programming practices), and the code wasn’t DRY (Don’t Repeat Yourself). Each level needed a new levelN.lua file, which repeated the entire logic of the game plus level layout.
The project wasn’t perfect, but it was a great example 5 years ago. Director class was the only scene management choice at the time and there was no way to organize projects with sub-directories. Dark times.
After the acquisition of Corona Labs by Perk, and with addition of new hires, it was time that we made a fresh new clone! Or a remake of the clone. Or is it a clone’s clone? Too many clones… Let’s just call it a remake.
The “remake” started as a simple code update to the original game, to make it compatible with modern versions of Corona SDK. As a side note, it was actually my first task at Corona Labs as a new hire. Initially, I started updating the code, but quickly realized it was not enough. Developers would not fully benefit from simple code update. We needed a game that shows best practices. So, I created a new game with similar features, from scratch.
The old graphics weren’t made for high resolutions, so we decided to use free assets by kenney.nl, which are great for prototyping. I also wanted to use Tiled to showcase how to integrate it into a Corona game. I wanted to integrate a simple level editor too. It also needed to be playable on all platforms including OS X, Windows desktop and Apple TV, so controller support was a must.
As for the new game theme, I came up with an idea to fire Corona Labs and Perk logos from a cannon, like cannonballs. We can’t fight pigs, right? No unnecessary animal cruelty… So what can we fight with in Corona? Why, bugs of course! Corona Debugger could have been a cool name for this game, but we settled down on Corona Cannon instead.
During development, some fixes and improvements were also introduced in Corona. The
easing.continuousLoop was fixed, and Apple tvOS remote support was improved, as was desktop support.
Ghost vs. Monsters: 11 files, 1080 blank lines, 771 comment lines, 3487 code lines.
Corona Cannon: 21 files, 320 blank lines, 179 comment lines, 2288 code lines.
With considerably fewer lines of code, Corona Cannon is much more feature rich. Less code lines per file also means it’s much easier for a new developers to understand the project.
To use the demo, you will need to use daily build 2016.2818 or later.
At the root of the project, you will find sub-directories, icons and standard files like
main.lua. Here is the short directory structure with descriptions:
├── classes/ - components of the game, simple OOP approach.
│ ├── ball.lua - cannonball that flies and destroys bugs.
│ ├── block.lua - building blocks of the structures where bugs are hiding.
│ ├── bug.lua - main enemy in the game.
│ ├── cannon.lua - launches balls.
│ ├── end_level_popup.lua - dialog window that is shown at the end of a level.
│ ├── level_editor.lua - simple shortcut based in-game level editor.
│ ├── puff.lua - white or yellow cloud to represent dust or an explosion.
│ ├── shade.lua - dims the screen and doesn’t let touch/tap events through.
│ └── sidebar.lua - pause menu and settings bar.
├── images/ - all in-game images go here.
│ ├── ammo/ - cannonballs.
│ ├── background/ - sprites for close background elements, used in Tiled.
│ ├── background_flat/ - sprites for distant background elements, used in Tiled.
│ ├── blocks/ - building blocks.
│ │ ├── stone/ - stone material.
│ │ └── wood/ - wood material.
│ ├── buttons/ - all the buttons.
│ ├── controls/ - instructions on various controls like a gamepad or a touchsceeen.
│ ├── green_tiles/ - level tiles sprites, used in Tiled.
│ ├── puffs/ - white clouds.
│ └── … - all the other images used in the game.
├── levels/ - 10 levels of the game.
├── libs/ - reusable libraries.
│ ├── controller.lua - gamepads, Apple TV Remote, keyboard.
│ ├── databox.lua - persistent storage of user data.
│ ├── eachframe.lua - enterFrame manager.
│ ├── relayout.lua - reposition elements on the screen on resize event.
│ ├── sounds.lua - sounds and music manager.
│ └── tiled.lua - Tiled importer.
├── maps/ - Tiled maps and their Lua exports.
├── scenes/ - composer scenes.
│ ├── game.lua - main gameplay.
│ ├── level_select.lua - select one level.
│ ├── menu.lua - start screen.
│ └── reload_game.lua - “Loading...” scene, required for restarting a level.
├── sounds/ - all the sounds and music files.
├── build.settings - important platform specific settings.
├── config.lua - extended letterbox scaling, defaults to 640x960.
├── main.lua - entry point for every Corona app.
├── prepare_icons.sh - command line script to generate icons from 1024px files.
└── … - Icons and launchscreens.
Dive into the code starting from main.lua and study the project based on its comments. We will be providing a detailed video that explains the code. Stay tuned for that!
Moving forward, all new demos will be published in our special GitHub directory https://github.com/coronalabs-samples
Corona Cannon, as a demo project, provides great examples and best practices for both novice and experienced developers. I tried to keep the code structure simple, files small and everything clean overall. There’s no spaghetti code, no global variables, no cryptic variable names, and no useless comments.
Grab the source and start working with your own version or download one of the pre-built versions to see what the game is like.
This demo also runs everywhere. It’s published on Google Play and it’s pending review on the Apple stores (iOS, tvOS, and OS X). We will update this post with the links to all stores as they become available.
Source Code – https://github.com/coronalabs-samples/CoronaCannon
Google Play – https://play.google.com/store/apps/details?id=com.coronalabs.coronacannon
iOS – https://itunes.apple.com/us/app/corona-cannon/id1078778611
tvOS – https://itunes.apple.com/us/app/corona-cannon/id1078778611
OS X desktop – https://itunes.apple.com/us/app/corona-cannon/id1078798116
Amazon – http://www.amazon.com/Corona-Labs-Cannon/dp/B01BEG102A
OUYA – https://www.ouya.tv/game/Corona-Cannon/
Windows Desktop – http://developer.coronalabs.com/sites/default/files/Corona%20Cannon.zip
Windows Phone 8 – https://www.microsoft.com/store/apps/9NBLGGH5PWVN
Feel free to discuss this sample game in the forums!