Part 1: Corona SDK: A Simple Race game

In this post I show you how you can make a simple race game with the help of the Corona SDK. In the game cars will drive over a road but at the end of the road there is a roadblock. The objective of the game is to make sure that the cars brake before they reached the wall. When the user taps on the car, the car brakes. The game is over when 3 cars hit the roadblock.

How the game looks like


Some of the elements we will code in this little game are:
- Spawning cars which are randomly driving to the roadblock
- Timer to start the game
- When the user taps the blue car a higher score is given than when the user taps another color.
- Load sound effects to the car taps (braking) and crashing into the wall.

In this first tutorial we will only do a few things to set up our project and define the functions we need. The next tutorial (Part 2) will continue to create the game.

In case you want to jump to all assets and code you scroll down to the bottom of this post.

Let first start to hide the ugly statusbar of the device and create two variables cWidth and cHeight which represent the center of the screen. These will come in handy later one :)

display.setStatusBar(display.HiddenStatusBar)
local cWidth = display.contentCenterX
local cHeight = display.contentCenterY

Later in the game we will set audio effects for when the user taps the car or when the car is not tapped and crashed into the wall. The two audio files can be loaded as 2 variables.

-- loaded audio
local crash = audio.loadSound ("crash.mp3")
local brake = audio.loadSound ("break.mp3")

Now you can pre-define the functions you need in the game. We will need to have a titlescreen to launch the game, a createGame function which will set up the game. Next, we need a createCar function to create the different cars. Next, we also need a function when the car has been touched by the user. A function when the car hits the wall, and a function which keep track of the number of “lives” the player has left.

So create the following functions in your main.lua file.

local function createTitleScreen()
end

local function createGame()
end

local function touchCar(event)
end

local function hitWall(obj)
end

local function lifeCount()
end

To start the game we need to call createTitleScreen. So put this function on the end of your main.lua file

createTitleScreen()

First thing we will code the createTitlescreen. We create a background for the titlescreen (titlescreen.png) and create a text ” click here to start” , which the user can tap to start the game. To add the interaction to the text you need to add an AddEventListener with the type ” tap” which calls the function startPressed. The function startPressed, take the event of the tap, removes the titlescreen and create the game (createGame()). See the code below

local function createTitleScreen()
	
	local titlescreen = display.newImage("titlescreen.png")
	local startgametext = display.newText("Click here to start", 0 , 0, "Helvetica" , 24 )
	startgametext.x = cWidth
	startgametext.y = cHeight +100
	startgametext:setTextColor(0,0,0)


	
	local function startPressed(event)
		display.remove(event.target)
		display.remove(titlescreen)
		startgametext = nil
		createGame()

	end

        startgametext:addEventListener ( "tap", startPressed )
end

You can find the art work, audio and source code via this link:

Click http://www.christianpeeters.com/examplecode/brickwallracer.zip to download the source files.

Go to Part 2: Corona SDK: A Simple Race game

2 Responses

  1. Reese June 14, 2013 / 4:51 am

    Hi. I typed in all of the code on the page, and there were no errors. However, I used my own title screen picture and it did not show up on the corona simulator. Is there something I did wrong? Or is there a problem with the simulator? Also could you tell me when the second part of this lesson will come out?

    Thanks,
    Reese

    • Christian June 14, 2013 / 9:43 am

      Hi Reese,

      Thanks for visiting my blog and this tutorial. The following could maybe help:
      - In case you first run my background, the simulator might need to start your new main.lua. So closing and restarting your simulator might work.
      - In the tutorial the images sit on the same level in the project files as the main.lua hence the simple reference “titlescreen.png”. When you have created a subfolder for example: images your reference should be “images/titlescreen.png”
      - A mistake I often make is forgetting the file extension .png (or .jpeg) depending on your image file extension
      - When you have your titlescreen setting within a function make sure you call createTitleScreen()

      I will try to put part 2 live before the end of the weekend (at the latest monday night).

Leave a Reply to Reese Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*