Time for action – drawing the background

In this section, we'll fill the screen with our environment display objects. This includes our background and ground objects, and we can also add physical elements to our ground so that we can designate collision events for it. To draw the background, perform the following steps:

  1. Create a local function called drawBackground():
        local drawBackground = function()
  2. Add in the background image:
          background = display.newImageRect( "bg.png", 480, 320 )
          background.x = 240; background.y = 160
          gameGroup:insert( background )
  3. Add in the ground elements and create the ground physical boundary. Close the function:
          ground = display.newImageRect( "grass.png", 480, 75 )
          ground.x = 240; ground.y = 325
          ground.myName = "ground"
          local groundShape = { -285,-18, 285,-18, 285,18, -285,18}
          physics.addBody( ground, "static", { density=1.0, bounce=0, friction=0.5, shape=groundShape } )
          gameGroup:insert( ground )
        end

The background and ground display objects are placed in the function called drawBackground(). The display.newImageRect()function is used since we are incorporating dynamic scaling on some of our images. The ground display object has a customized physical shape that is not of the same size as the original display object.

Our background object is centered to the dimensions of the device screen area and inserted in gameGroup.

The ground display object is placed near the bottom of the display area. It is assigned a name through ground.myName = "ground". We'll use the name "ground" later on to determine collision events. A customized physical boundary is made for the ground through groundShape. This allows the body of the ground to affect the assigned dimensions of the display object. When physics.addBody() is initialized, we used groundShape as the shape parameter. Next, ground is set to gameGroup as well.

In gaming, the heads-up display (HUD) is the method used to relay information visually to the player. In many games, the common features displayed are health/lives, time, weapons, menus, maps, and so on. This keeps your player alert to what is currently happening during game play. When it comes to tracking your lives, you want to be informed how many are left before your character runs out of chances to continue playing and the game ends.