Part 2: Corona SDK: A Simple Race game

In the previous post we created the title screen. In this post we will create the game. In case you missed the previous tutorial:

Part 1: Corona SDK: A Simple Race game.

In case your look for the complete source code, please scroll down in Part 1.

In case your wondering what Part 2 tutorial this is? See below the a small video of the game we are creating:

First create the following global variables by putting them on top of your main.lua file:

local createGame()
local stopGame()
local score = 0
local scorelabel
local timeLimit =3

Now create a function called function createGame(). In this function we will create the game, meaning we set the background, we tell what to do when we stop the game (stopGame()), create the scoring module, and a time-countdown before the game starts.

First we create the background, the wall on the right and a back button to move the user back to the titlescreen. You can see that I use backbutton.xScale and yScale to scale the back button. I did this only because the size of the button was a bit to large. In case you do not have a large back button, you can of course ignore this. The wall needs to be positioned on the far right of the screen, this can be determined by using wall.x = display.contentWidth (with others words get the width of the screen).

function createGame()
		
	local gamebackground = display.newImage("gamebackground.png")

	wall = display.newImage("wall.png")
	wall.x = display.contentWidth 
	local backbutton = display.newImage("back.png")
	backbutton.xScale = 0.4; backbutton.yScale = 0.4
	backbutton.x = 40
	backbutton.y = 40 

Next we need to tell the program what to do, when a user taps the back-button. In that case we want to remove the cars and set them to nil. This way we remove the cars from the memory. We add an addEventListener to call the stopGame() method and direct the user back to the titlescreen.

function stopGame()
	car:removeSelf()
	car = nil 
	createTitleScreen()
	end
		
	backbutton:addEventListener ( "tap", stopGame )

Next we want to create a scorelabel or text on the screen. We need actual two variables. The first one is the text field to display the score (scorelabel) and the second one is called score, this variable holds the actual score value. As you can see both are made global, so we can access them later on. Note that initially, we set the score to 0.

We also need to set the timelimit of the countdown. This is the countdown from 3 till 0, before the game starts. Furthermore, when a user crash his car 3 times into the wall the game is over. So every-time we start the game we also need to reset the number of lives, which I called crashnumber = 3. This is thus the number of crashes the user can make before the game ends.

scorelabel = display.newText( "Score: 0", 0, 0, "Helvetica", 22 )
	scorelabel.x = display.contentWidth - 75
	score = 0	
	timeLimit = 3
	wall.crashnumber = 3
		
	timeLeft = display.newText(timeLimit, cWidth-10, cHeight-30, "Helvetica", 50)

Finally, we need to make the timer work. So we want that the program takes the timelimit which is in this case 3 and count down to zero. When the timer is at zero the game starts:

So we create a local function (we wont use it elsewhere) timerDown(). We take the timelimit and substract 1. We than display this value in the timeleft.text. We need to repeat that 3 times before we createCar() (see part 3). createCar() will be discussed in part 3, but this function calls the cars on the screen. When the countdown reached 0, we also need to remove the countdown from the screen.

We call the timeDown() function via a timer.performWithDelay, which takes 3 parameter. This first one is the number of milliseconds between the 3,2,1, which we set a 1000 (1sec), second we need to set which function needs to be called which is timerDown. Finally we have to repeat this 3 times, which is equal to the variable timeLimit.

local function timerDown()
  	timeLimit = timeLimit-1
   	timeLeft.text = timeLimit
     		if(timeLimit==0)then
       		createCar()
       		timeLeft.isVisible = false
       		timeLimit:removeSelf()
		timeLimit = nil 
     		end
  end
		timer.performWithDelay(1000,timerDown,timeLimit)

The complete createGame() function should now look like this:

function createGame()
		
	local gamebackground = display.newImage("gamebackground.png")

	wall = display.newImage("wall.png")
	wall.x = display.contentWidth 
	local backbutton = display.newImage("back.png")
	backbutton.xScale = 0.4; backbutton.yScale = 0.4
	backbutton.x = 40
	backbutton.y = 40
		
		function stopGame()
	                car:removeSelf()
			car = nil 
			createTitleScreen()
			 end
		
		backbutton:addEventListener ( "tap", stopGame )

		scorelabel = display.newText( "Score: 0", 0, 0, "Helvetica", 22 )
		scorelabel.x = display.contentWidth - 75
		score = 0	
		timeLimit = 3
		wall.crashnumber = 3
		
		timeLeft = display.newText(timeLimit, cWidth-10, cHeight-30, "Helvetica", 50)

		local function timerDown()
  		 timeLimit = timeLimit-1
   		timeLeft.text = timeLimit
     			if(timeLimit==0)then
       				 createCar()
       				 timeLeft.isVisible = false
       				timeLimit:removeSelf()
						timeLimit = nil 
     			end
  end
		timer.performWithDelay(1000,timerDown,timeLimit)
end

Next up, is the creation of the cars and all the functions when the user taps the cars and the scores etc.

Leave a 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>

*