Gameboy Programming – Scrolling Backgrounds

In this section we are going to take what you know regarding sprites and build a background which scrolls across the screen. There’s not a tremendous amount of new code here

In order to follow along with this tutorial you will need a copy of the Gameboy Map Builder. This can be found here.

Building a Background.

A background is comprised of a series of tiles. these can be created from the Gameboy Tile Maker which we installed in the previous lesson regarding sprites. Here’s the basic tiles I have made for our background.

First Tile

Second Tile

Third Tile

Fourth Tile

Fifth Tile

Sixth Tile

Seventh Tile

Once these simple tile sets are created, we Save them Below is the naming convention I have used. Ensure to save this in the folder location where you are going to write your code.

Creating a Tile Map for the Background

Now we use the new utility which I have introduced into this lesson, The Gameboy Tile Map Builder.

To load the tile set that you have saved go to File->Map Properties. Here use the Filename Browser to open the tiles which you made earlier. When you import the tiles ensure that your width is 20 and height is 18. In order for a scrollable background we need a background which stretched over a few screens.

Once you imported your tile set you can go about using it to create your background. Go ahead and make it as fancy as you would like,

This is my effort..

Exporting your Background

In order to Export the background we go to File->Export To. Ensure that you save the file as a type GBDK and in the folder location where you are going to write your code.

In the Location Format tab, please choose the following settings:

Now its onto the Code!

The Code

Once again, this code is nothing to be afraid of. It contains many of the concepts which we have covered before. Please ensure that the relevant tile and map files are included in the project folder.

We ensure to include our background and tile map code files at the beginning of the code. These were the files which we exported from the tile and map maker.

In order to display the background we use the methods set_bkg_data (and pass in the label for our background tiles, loading in the 7 tiles) and set_bkg_tiles (and pass in the label for our map and the size of the map).

We call the SHOW_BKG and DISPLAY_ON in built functions from the gamboy development kit to display our background on the screen.

We then create a game loop (an infinite loop) to scroll the background by 1 pixel to the right and cause a slight delay to ensure smoothness.

If you have followed along with this tutorial you should see :

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s