Advertisement
  1. Code
  2. Mobile Development
  3. Corona

Create a Minigolf Game - Interface Creation

Scroll to top
6 min read

In this tutorial series, I'm going to show you how to create a physics based minigolf game in Corona SDK. You'll learn about physics manipulation, touch controls, and collision detection. Read on!


1. Application Overview

App OverviewApp OverviewApp Overview

Using pre-made graphics, we'll code an entertaining game using Lua and the Corona SDK APIs.

The player will be able to use the touch screen on the device to shoot the ball across the stage. You can modify the parameters in the code to customize the game.


2. Target Device

Target DeviceTarget DeviceTarget Device

The first thing we have to do is select the platform we want to run our app within. This allows us to choose the size for the images we'll use.

The iOS platform has the following characteristics.

  • iPad 1/2/Mini: 1024x768px, 132 ppi
  • iPad Retina: 2048x1536, 264 ppi
  • iPhone/iPod Touch: 320x480px, 163 ppi
  • iPhone/iPod Retina: 960x640px, 326 ppi
  • iPhone 5/iPod Touch: 1136x640, 326 ppi

Android is an open platform, so there are many different devices and resolutions. A few of the more common screen characteristics include the following.

  • Asus Nexus 7 Tablet: 800x1280px, 216 ppi
  • Motorola Droid X: 854x480px, 228 ppi
  • Samsung Galaxy SIII: 720x1280px, 306 ppi

In this tutorial we'll be focusing on the iOS platform with the graphic design, specifically developing for distribution to an iPhone/iPod touch. However, the code presented here should apply to Android development with the Corona SDK as well.


3. Interface

InterfaceInterfaceInterface

A simple and friendly interface will be used. It involves multiple shapes, buttons, and bitmaps, among other things.

The interface graphic resources necessary for this tutorial can be found in the attached download.


4. Export Graphics

Export GraphicsExport GraphicsExport Graphics

Depending on the device you have selected, you may need to export the graphics in the recommended PPI. You can do this in your favorite image editor.

I used the Adjust Size... function in the Preview app on Mac OS X.

Remember to give the images a descriptive name and save them in your project folder.


5. App Configuration

An external file will be used to make the application go fullscreen across devices. This is the config.lua file. This file shows the original screen size and the method used to scale that content in case the app is run in a different screen resolution.

1
2
application =
3
{
4
    content =
5
    {
6
        width = 320,
7
        height = 480,
8
        scale = "letterbox"
9
    },
10
}

6. Main.lua

Let's write the application!

Open your prefered Lua editor (any Text Editor will work, but you won't have syntax highlighting) and prepare to write your awesome app. Remember to save the file as main.lua in your project folder.


7. Code Structure

We'll structure our code as if it were a Class. If you know ActionScript or Java, you should find the structure familiar.

1
2
Necesary Classes
3
4
Variables and Constants
5
6
Declare Functions
7
8
    contructor (Main function)
9
	
10
    class methods (other functions)
11
12
call Main function

8. Hide Status Bar

1
2
display.setStatusBar(display.HiddenStatusBar)

This code hides the status bar. The status bar is the bar on top of the device screen that shows the time, signal, and other indicators.


9. Import Physics

We'll use the Physics library to handle collisions. Use the following code to import it.

1
2
-- Physics
3
4
local physics = require('physics')
5
physics.start()

10. Background

BackgroundBackgroundBackground

A simple graphic is used as the background for the application interface. The following line of code stores it.

1
2
-- Graphics
3
4
-- [Background]
5
6
local bg = display.newImage('bg.png')

11. Title View

Title ViewTitle ViewTitle View

This is the Title View, it will be the first interactive screen to appear in our game. These variables store its components.

1
2
-- [Title View]
3
4
local title
5
local playBtn
6
local creditsBtn
7
local titleView

12. Credits View

Credits ViewCredits ViewCredits View

This view will show the credits and copyright of the game. The following variable will be used to store it.

1
2
-- [CreditsView]
3
4
local creditsView

13. Game Background

Game BackgroundGame BackgroundGame Background

This is the level background. It also adds the information textfields.

1
2
-- Game Background
3
4
local gameBg

14. Instructions Message

Instructions

An instructions message will appear at the start of the game. It will be tweened out after two seconds.

1
2
-- Instructions
3
4
local ins

15. Ball

Ball

The ball graphic. The objective of the game is to put this item in the level hole.

1
2
-- Ball
3
4
local ball

16. Hole

Hole

Stores the hole physics for collision detection.

1
2
-- Hole
3
4
local hole

17. Alert

Alert

This is the alert that will be displayed when you win the game. It will complete the level and end the game.

1
2
-- Alert
3
  
4
local alertView

18. Sounds

Sounds

We'll use sound effects to enhance the feeling of the game. You can find the sound used in this example at Soungle.com using the keyword golf.

1
2
-- Sounds
3
4
local ballHit = audio.loadSound('ball_hit.mp3')
5
local ballHole = audio.loadSound('ball_hole.mp3')

19. Variables

This are the variables we'll use. Read the comments in the code to know more about them.

1
2
-- Variables
3
4
local w1 -- Walls
5
local w2
6
local w3
7
local w4
8
local w5
9
local w6
10
local guide -- Transparent white line that indicates shooting direction and force
11
local scoring -- Stores the scoring alert image

20. Declare Functions

Declare all functions as local at the start.

1
2
-- Functions
3
4
local Main = {}
5
local startButtonListeners = {}
6
local showCredits = {}
7
local hideCredits = {}
8
local showGameView = {}
9
local gameListeners = {}
10
local shoot = {}
11
local onCollision = {}
12
local alert = {}

21. Constructor

Next we'll create the function that will initialize all the game logic.

1
2
function Main()
3
	-- code...
4
end

22. Add Title View

Now we're going to place the TitleView in the stage and call a function that will add the tap listeners to the buttons.

1
2
function Main()
3
	titleBg = display.newImage('titleBg.png', 114, 50)
4
	playBtn = display.newImage('playBtn.png', 218, 177)
5
	creditsBtn = display.newImage('creditsBtn.png', 203, 232)
6
	titleView = display.newGroup(titleBg, playBtn, creditsBtn)
7
	
8
	startButtonListeners('add')
9
end

Next Time...

In this part of the series you've learned the interface and the basic setup of the game. In the next and final part of the series, we'll handle the level creation, collision detection, and the final steps to take prior to release like app testing, creating a start screen, adding an icon and, finally, building the app. Stay tuned for the final part!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
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.