Advertisement
  1. Code
  2. Corona SDK
Code

Create a Brick Breaker Game with the Corona SDK: Collision Detection

by
Difficulty:IntermediateLength:ShortLanguages:
This post is part of a series called Create a Brick Breaker Game with the Corona SDK.
Create a Brick Breaker Game with the Corona SDK: Game Controls

Welcome to the final tutorial in our Brick Breaker game series! In this tutorial, we'll handle the logic for the remaining collisions, check for win and loss states, change levels, and more.


Where We Left Off. . .

Be sure to read part 1 and part 2 of this series to fully understand the code presented in this tutorial.

Step 35: Detect Brick Collisions

When the ball collides with a brick, we will use the same technique applied to the paddle to determine the side the ball will follow:


Step 37: Remove Brick

When a brick is hit, we use the other parameter of the collision event to find out exactly which brick was hit, and then remove it from both the stage and the bricks group:


Step 38: Add Score

Each brick hit will add 100 points to the score. The score will be taken from the score constant and added to the current score as text.


Step 39: Check Bricks

This code checks if there are anymore bricks left on stage and displays a "you won" message if not:


Step 40: Ball Movement

The ball speed is determined by the xSpeed and ySpeed variables. When the update function is executed, the ball starts moving using these values for placement on every frame.


Step 41: Wall Collision

This code checks for wall collisions with the ball, and responds by sending the ball in the opposite direction when necessary:


Step 42: Loss Condition

An if statement is used to check if the paddle misses the ball. If true, an alert is displayed asking the user if they would like to try again.


Step 43: Game Status Message

The Alert Screen shows the player information about the status of the game. It is shown when a win/loss event is reached.

Two parameters are used in this function:

  • t: The alert title
  • m: A short message

Step 44: Restart

The next function checks the game status (win, lose, finished) and performs the corresponding action:


Step 45: Change Level

This function changes updates the level when necessary:


Step 46: Call Main Function

In order to initially start the game, the Main function needs to be called. With the above code in place, we'll do that here:


Step 47: Create the Loading Screen

The Default.png file is an image that will be displayed right when you start the application while the iOS loads the basic data to show the Main Screen. Add this image to your project source folder, it will be automatically added by the Corona compliler.


Step 48: Icon

Using the graphics you created before you can now create a nice and good looking icon. The icon size for the non-retina iPhone icon is 57x57px, but the retina version is 114x114px and the iTunes store requires a 512x512px version. I suggest creating the 512x512 version first and then scaling down for the other sizes.

It doesn't need to have the rounded corners or the transparent glare, iTunes and the iPhone will do that for you.


Step 49: Testing in the Simulator

It is time to do the final test. Open the Corona Simulator, browse to your project folder, and then click open. If everything works as expected, you are ready for the final step!


Step 50: Build

In the Corona Simulator go to File > Build and select your target device. Fill the required data and click build. Wait a few seconds and your app will be ready for device testing and/or submission for distribution!


Conclusion

The final result is a customizable and entertaining game. Try adding your own custom graphics and levels to spice things up!

I hope you liked this tutorial series and find it helpful. Thank you for reading!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.