Time for action – adding game objects

Let's add in the display objects the player will see while in game play:

  1. After the loadGame() function, we're going to create another function that will display all our game objects on screen. The following lines will display the art assets that were created for this tutorial:
    function addGameScreen()
    
      background = display.newImage("bg.png", 0, 0, true )
      background.x = _W 
      background.y = _H
      
      paddle = display.newImage("paddle.png")
      paddle.x = 240; paddle.y = 300
      paddle.name = "paddle"
      
      ball = display.newImage("ball.png")
      ball.x = 240; ball.y = 290
      ball.name = "ball"
  2. Next, we'll add in the text that will display the score and level number during the game:
      scoreText = display.newText("Score:", 25, 10, "Arial", 14)
      scoreText:setFillColor( 1, 1, 1 )
    
      scoreNum = display.newText("0", 54, 10, "Arial", 14)
      scoreNum: setFillColor( 1, 1, 1 )
    
      levelText = display.newText("Level:", 440, 10, "Arial", 14)
      levelText:setFillColor( 1, 1, 1 )
    
      levelNum = display.newText("1", 470, 10, "Arial", 14)
      levelNum:setFillColor( 1, 1, 1 )
  3. To build the first game level, we're going to call the gameLevel1() function, which will be explained later in this chapter. Don't forget to close the addGameScreen() function with end:
      gameLevel1() 
     
    end

The addGameScreen() function displays all the game objects shown during game play. We have added the background, paddle, and ball display objects from the art assets provided for this chapter.

We have added text for the score and level at the top of the game screen. scoreNum is initially set to 0. In the next chapter, we'll discuss how to update the score number when a brick collision is made. levelNum starts at 1, updates when the level is completed, and moves on to the next one.

We ended the function by calling gameLevel1(), which will be implemented in the next section to start the first level.