How To Make a Breakout Game with Corona

This is a post by Tutorial Team Member Greg Pugh, author of the Colin Turtle children’s eBook app series. You can also find him on Google+. If you like to play video games, you’ve most likely played some variant of the classic game Breakout. The goal of Breakout is to move a paddle on the […] By .

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 7 of this article. Click here to view the first page.

I Know the Status…Gone!

One of the first things you will do in this game is hide the status bar. This game is going to be so amazing that the player won’t care what time it is or how much battery life is left! :]

Paste the following lines of code into main.lua and save.

-- Hide Status Bar
display.setStatusBar(display.HiddenStatusBar);

If the simulator is still running, it should tell you that main.lua has changed, and offer to relaunch the app in the simulator to get your new code up and running. However, you can also navigate to “File > Relaunch” to do this step manually. You’ll need to remember to do this each time you update the code to see your work in action! :]

Comments in Lua are prefixed with “- -“, as seen above. Although it’s tempting to skip putting comments in your code, especially in a rapid-development language like Lua, you’ll be glad you did when you come back to a project weeks or months later! :]

The call to display.setStatusBar() hides the status bar, as shown below:

Abracadabra Status Bar Be Gone!

Abracadabra Status Bar Be Gone!

Did that seem a little too easy? If so, that’s just the power of Corona doing a lot of the heavy lifting for you!

The next section takes you through the basic physics of this game; you’ll see how Corona neatly handles the sticky bits of game physics for you! :]

But I Failed Physics in High School!

In order to get a handle on the physics required for this game, think back to the old school Breakout game, or have a quick read through the Wikipedia article on the game. Breakout consists of a moving ball, a player-controlled paddle to bounce the ball on, some walls to keep the ball in play, and rows of bricks that are destroyed when the ball comes in contact with them.

Does it sound like there might be physics involved? :] It sure does! Well then, you’d better get started coding your complex physics engine!

Paste the following lines of code into main.lua and save:

-- Physics Engine
local physics = require "physics";
physics.start();
physics.setGravity(0, 0);

Okay, you’ve just coded your complex physics engine. :] Believe it or not, that’s it!

The code above simply tells Corona to include the built-in physics engine, and to set the gravity of your game universe to zero in both the X-direction (side-to-side) and the Y-direction (up and down). There’s no gravity in this game — the physics is solely controlled by the bullet ricocheting off of your paddle, the walls, and of course, your zombies!

You’ll program the movement of the bullet a bit later in this tutorial, but for now, revel in the fact that you just added physics to your game in three lines! :]

Note: The code above creates a local variable named physics — hence the “local” keyword. It’s a good practice to use local instances of variables; otherwise, they will be “global” and require manual cleanup!

Note: The code above creates a local variable named physics — hence the “local” keyword. It’s a good practice to use local instances of variables; otherwise, they will be “global” and require manual cleanup!

Games aren’t much fun without sound effects and music, so luckily the starter package provides some for you to use! The next section will get your app humming. :]

Music Makes the Rhythm

In order to make use of the included sound effect files, paste the following lines of code in main.lua and save:

-- Load Sounds
local bgMusic = audio.loadStream("sounds/ZombieBreakTheme.mp3");
local zombieKill = audio.loadSound("sounds/ow.mp3");

These two lines load the two sound effect files into the game so that they’re ready for playback when required. You haven’t actually instructed the files to play, so you won’t hear anything yet when the app is restarted in the simulator.

Even though Corona handles a lot of things for you, you’ll still need a host of variables to store information particular to the operation of your game. The next section shows you how! :]

Varying Variables

There’s a good number of variables that will be used in your game. The first two you’ll set up are _W and _H, which will help when working with on-screen placement of graphics.

Paste the following in main.lua and save:

-- "Constants"
local _W = display.contentWidth / 2;
local _H = display.contentHeight / 2;

_W and _H are technically variables, but in this case, treat them like constant values used to keep track of the device’s screen width and height.

As every device screen will vary in size, it is much more flexible in game development to place objects on the screen using relative placement, as opposed to absolute positioning. When you don’t know what the eventual screen resolution will be, it’s easier to say “10 pixels from the right edge” rather than “pixel 310”.

Knowing the center points rather than the full width and height is also really useful in Corona, as it handles placement based on the center of an object, unlike native iOS, which places objects based on the (0,0) coordinates of the object.

For example, placing an object at (0, 0) in Corona will put the center of the object in the top-left corner, leaving a portion of the object offscreen! :]

Next, you’ll create other variables that will be covered later in the tutorial.

Paste the following in main.lua and save:

-- Variables
local zombies = display.newGroup();
local zombieWidth = 50;
local zombieHeight = 50;
local row;
local column;
local score = 0;
local currentLevel;
local velocityX = 3;
local velocityY = -3;
local gameEvent = "";

A group is exactly what it sounds like: individual objects in your application that you can refer to as a whole. In this case, you created a new group called “zombies”, which will contain…you guessed it, zombies! The benefit to grouping zombies in this manner, as opposed to handling them all separately, will be seen later in the tutorial.

Each zombie will be 50 x 50 pixels in size, and will be placed in rows and columns across the screen. The score will start at 0 and the player will get to change levels as they progress.

The velocity variables velocityX and velocityY will be used to move the bullet on the screen, and the gameEvent variable will be used to keep track of what to do when the player wins or loses.

Now that you have that first set of variables defined, it’s time to get the rest of the variables in your code that manage on-screen activities. Paste the following into main.lua and save:

-- Menu Screen
local titleScreenGroup;
local titleScreen;
local playBtn;

-- Game Screen
local background;
local player;
local zombie;
local bullet;

-- Score/Level Text
local zombieKillText;
local zombieKillNum;
local levelText;
local levelNum;

-- textBoxGroup
local textBoxGroup;
local textBox;
local conditionDisplay;
local messageText;

The variables used above should be fairly self explanatory; the comments above each block indicate the purpose and function of the variables in that set.