Introduction
Welcome to part 5 of my Love2d tutorial. In this part we are going to add tiles to the game.
Adding tiles
The first thing we need to do is to create three tile pieces. For this i used GIMP and made three simple squares.
Next create a directory called images
and place the three tile pieces there.
The next thing to do is to create a file Tiles.lua
.
Inside this file, the first thing to do is to create a tile map.
This is a 2d array using numbers to represent location of the tiles on the map
- 0 represents empty space
- 1 represents tile 1
- 2 is tile 2
- 3 is tile 3
For the next part, the steps are:
- Create an Icon object to hold icons. it should store:
- icon for the tile created with
love.graphics.newImage()
- width of icon using
getWidth()
- height of icon. Use the function
getHeight()
- icon for the tile created with
- Create Icon list.This is a list to hold the Icon objects.
- Create a function which populates the icon list with the three icons tile1, tile2, and tile3.
- A function to loop through the tile map and print each tile.
- We use a nested for loop to loop through the tile map.
- If the tile isn't zero then we print the corresponding icon from icon_list using the number in Tile_Map.
Modifying main.lua
We now need to make a few changes to main. We need to call our new code and we clean up a few things we previously added.
New stuff to add
Icon_List:populate
we call the populate function in ourlove.load()
function.drawTileMap()
We add this function tolove.draw()
in order to draw the tiles on each update.
Cleaning up older stuff
We now clean up some of our older code to improve our main.lua file.
- we change our player construct function to use an image icon using
love.graphics.newImage()
. - we change our enemy construct function to use an image icon directly.
- we change the values for WIDTH and HEIGHT to accommodate our new tile map.
Updating Characters.lua
We now make a few changes to our Characters.lua file.
o.icon = icon
we set the objects icon to the icon we pass to it. No more callinglove.graphics.newImage()
o.y = y - o.oy
we adjust the y position to account for the y offset.
No comments:
Post a Comment